编程知识 cdmana.com

Java Basics - operators (03)

Concept :

Operator : It's the symbol that operates on constants and variables .

expression : Connected by operators java The formula of grammar , Expressions connected by different operators are different types of expressions .

Operator classification :

Arithmetic operator (+  -  *  /  %  ++  --)

Assignment operator (= += -= *= /=)

Relational operator (==,!=,>,>=,<,<=)

Logical operators (&,|,!,^,&&,||)

Ternary operator

Arithmetic operator

Division of integers can only get integers . To get a decimal , You have to have floating-point numbers .

Characters participate in addition operations ( The pure numerical operation is addition , A string is a character connection ,char->int->string), It's actually the data value of the character stored in the computer (ASCII clock ) To participate in the calculation

'A'    65
'a'    97
'0'    48int a =10;char c = 'A';
System.out.println(a+c);    // The result is 75

  Arithmetic operator ++  -- The little problems in the process . It's calculated from left to right , And then assign to the left

int x = 4;int x2 = 4;int y = (x--)+(--x)+(x*10);      //4+2+20=26 (x--) The value is 4,x by 3; then (--x) The value is 2,x by 2;int y2 = (--x2)+(x2--)+(x2*10);  //3+3=26 (--x2) The value is 3,x2 by 3; then (x2--) The value is 3,x2 by 2;System.out.println("x = " + x + ",y = " + y+ ",y2 = " + y2); //x = 2,y = 26,y2 = 26

Arithmetic operator ++ -- The pit in the sky

int a=1,b=0;    
b=a++;     // First assign value, then add one by oneself. The result of this program is , b=1,a=2   

int a=1;    
a=a++;    //a Give yourself the value first 1, then a add 1 The result of this procedure is , a=1, It's strange that there are trees and ???

This involves knowledge of the operating system , Just briefly : Our usual variables and objects are opened in memory . And the operation is just cpu Capable operation . The hole in this topic is a The operation of adding a value to itself is only in cpu in , The result of the calculation is stored in the register , Not assigned to variables in memory .

It can be understood as := The assignment operator is to put cpu The results in the register are stored in memory ;

The assignment operation

Basic copy operators :=

Extended assignment operators :+= -= *= /=...

+= Take the data on the left and the data on the right +, Then assign the result to the left

Be careful : Extended assignment operators , Implied cast , The forced type is the type on the left

for example : Variable a,a+=10; amount to a=(a Data type of )(a+10);

Relational operator

==,!=,>,

The result of the relational operator is boolean type .

Logical operators

Logical operators are used to connect relational expressions

&,|,!,^( If they are different or the same false, The difference is true, It can be understood as a relationship , The same sex is not a couple , The difference between the sexes is the couple )

&&( and & Results the same ),||( and | Results the same )

Ternary operator

Relationship expression ? expression 1: expression 2

A. Calculate the value of the relationship expression , Look yes true still false    B. If it is true, expression 1 It's the result ; If it is false, expression 2 It's the result

Ternary operators also return values .

 

// Give according to judgment result assignment  result = n>=90?'A':(n<60?'C':'B');// Don't write it  n>=90?result='A':(n<60?result='C':result='B');

 

        short s = 30;        int i = 50;        //s = i+s;  // Report errors  cannot convert from int to short
        s += i;    //+= The assignment operator has display type conversion function , amount to s=(short)s=i;!!!
        System.out.println("s="+s);   //80
        int x = 0;        int y = 0;        int z = 0;        boolean a,b;
        a = (x>0) & (y++>1); //false&false
        System.out.println("a="+a); //false
        System.out.println("y="+y); //1
        b = (x>0) && (z++>1);  //false first false So the second one doesn't work 
        System.out.println("b="+b);  //false
        System.out.println("z="+z);  // So here is 0, instead of 1
        a = (x>0) | (y++>1);    //false|false
        System.out.println("a="+a);   //false
        System.out.println("y="+y);   //2
        b = (x>0) || (z++)>1;    //false||false Short-circuit operation , The first is true The second one doesn't operate 
        System.out.println("b="+b);   //false
        System.out.println("z="+z);   //1

版权声明
本文为[Rain lane in autumn night]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/05/20210531232015273N.html

Scroll to Top