Java attend the road (5) ------------ road ahead will be long Come

First, the operator

1.1 Arithmetic Operators

/ * 
Operator: symbol particular operation, for example: + 
expressions: to link the expression using operators called the expression, for example: 20 + 5, and if; a + b 

four arithmetic operations: 
Add: + 
Less: - 
multiplication: * 
except: / 


modulo (take the remainder):% 

the resulting expression is calculated first, and then print out the result. 
Dividend / divisor = quotient ... remainder 

for expression is an integer, starting with a divisible, integer integer first day, the result is still an integer, but only business, do not look at the remainder. 
Only for an integer division, the modulo calculation only more than the number of significance. 

Note: 
    1, once the operations which have different types of data, then the result will be a large range of data types that. 
* / 
Public  class Demo04Operator {
     public  static  void main (String [] args) {
         // can perform mathematical operations between two constants 
        System.out.println (20 is + 30); // 50 
        
        // between two variables can perform mathematical operations 
        int A = 20 is ;
        int B = 30 ; 
        System.out.println (A - B); // -10 
        
        // can be mixed between the variables and constants used 
        System.out.println (A * 10); // 200 is 
        
        int X = 10 ;
         int . 3 = Y ; 
        
        int RESULT1 = X / Y; 
        System.out.println (RESULT1); // . 3 
        
        int result2 X% = Y; 
        System.out.println (result2); // remainder, modulus,. 1 
        
        // int + --- Double> Double Double + ---> Double 
        Double result3 = X + 2.5 ; 
        System.out.println (result3); // 12.5
    }
}

1.2 Arithmetic operators - a plurality of plus

/ * 
Four operations among the plus sign "+" There are three common usage; 

1, the value for it, and that is a wig. 
2. char for character types, before calculating, char will be promoted to become int, and then calculate, 
3, for a string String (first letter capitalized, not keyword), the plus sign on behalf of string concatenation operator . 
Any time data and music ning string connection, the result will become the string 
* / 
public  class Demo05Plus {
     public  static  void main (String [] args) {
         // Variables of type string using substantially
         // Data type variable name = data values; 
        String str1 = "the Hello" ; 
        System.out.println (str1); // the Hello 
        
        System.out.println ( "the Hello" + "World"); // the HelloWorld 
        
        String str2 = "the Java" ;
        // String + int ---> String
        System.out.println(str2 + 20);// Java20
        
        //优先级问题
        //String + int + int
        //String       + int
        //String
        System.out.println(str2 + 20 + 30);// Java2030
        
        System.out.println(str2 + (20 + 30));// Java20
    }
}

1.3 Arithmetic Operators - from the Operational

/ * 
Increment operator: ++ 
decrement operators: - 

basic meaning: a digital make up a variable, or a variable let down a number 1 
using the format: written before the variable name, or write after the variable name, For example: ++ num, num ++ may be 
used: 
    1, alone: any other operations and do not mix themselves as a separate step. 
    2, in combination: mixing and other operations, such as mixing with an assignment, or mixed with a printing operation, and the like. 
Use difference: 
    1, when used alone, front and rear ++ ++ no difference, that is: ++ num; and num ++; is exactly the same. 
    2, mixing time, there are significant differences [] 
        A, if it is before [++], then the variable immediately immediately [+1], and then use the results hold, the first increase after [with] 
        B, if it is attached later. ++], it would have been the first to use variable values, and then let the variable [+1], [plus] after the first use 
        
Note: 
    only use variables to increment, decrement operators, constants can not be changed, it can not use. 
* / 
Public  class Demo06Operator {
     public  static  void main (String [] args) {
         int num1 = 10 ;
        System.out.println (num1); // 10 
        ++ num1; // used alone, the front ++ 
        System.out.println (num1); // . 11 
        num1 ++; // used alone, after ++ 
        the System.out. the println (num1); // 12 is 
        System.out.println ( "=================" ); 
        
        // mixed with the printing operation time 
        int num2 = 20 is ;
         // mixed use, first ++, variables immediately immediately become 21, and then print the results of 21 
        System.out.println (+ num2); // 21 
        System.out.println (num2); // 21 
        System.out.println ( "====================" ); 
        
        int= 30 num3 ;
         // mix, the first ++ variable to use the original 30, and then let the resulting variable +1 31 is 
        System.out.println (num3 ++); // 30 
        System.out.println (num3); // 31 is 
        System.out.println ( "====================" ); 
        
        int Num4 = 40 ;
         int RESULT1 = --num4; // mixed, pre - - variable -1 immediately immediately become 39, then the results 39 to result1 variables 
        System.out.println (result1); // 39 
        System.out.println (Num4); // 39 
        System.out.println ( " ==================== " ); 
        
        int Num5 = 50 ;
         //Mixed use, after - first the original figures 50 to result2, then I become myself again -1 49 
        int result2 = num5-- ; 
        System.out.println (result2); // 50 
        System.out.println (Num5); // 49 
        System.out.println ( "====================" ); 
        
        int X = 10 ;
         int Y = 20 is ;
         // . 11 = 31 is 20 is + 
        int result3 = X + ++-y - ; 
        System.out.println (result3); // 31 is 
        System.out.println (X); // . 11 
        System.out.println (Y); // 19 
        
        // 30 ++; //Error writing: Constant can not use ++ or - 
    } 
}

Guess you like

Origin www.cnblogs.com/sgzslg/p/12142705.html