Java Base language operators

Source: Internet
Author: User
Tags bitwise bitwise operators

/*use of the + +,--operator: Used alone: placed before and after the operand.            (This usage is common to us) participate in the operation using: Put in front of the operand, the first increment or decrement, and then participate in the operation.                After the operand, it participates in the operation, then increases or is self-reduced. Function: The variable is self-increment 1 or self-minus 1. */ Public classTEXT3 { Public Static voidMain (string[] args) {//define two variables            intx = 3; inty = 4; //Use alone//x + +; //y--;++x; --y; System.out.println ("x:" +x+ ", Y:" +y);//x + + or ++x, when used alone, the output value is the same, here is 4;y--or--y, when used alone, the output value is the same, here is 3System.out.println ("-------------------"); //participate in operations using            intA = 3; intb = 4; //int c = a++; //int d = b--;                        intE = + +A; intf =--b; System.out.println ("A:" +a);//4System.out.println ("B:" +b);//3//System.out.println ("C:" +c);//3//System.out.println ("D:" +d);//4System.out.println ("E:" +e);//4System.out.println ("F:" +f);//3        }}
/*+ +,--Exercises the first question: int a = 10;    int B = 10;    int c = 10;    A = b++;    c =--a;    b = ++a;    A = c--;    Please calculate the value of A,b,c for the second question: int x = 4;    int y = (x + +) + (++x) + (X*10); Please calculate the value of x, Y, respectively*/ Public classTEXT3 { Public Static voidMain (string[] args) {intA = 10; intb = 10; intc = 10; A= b++;//a=10,b=11,c=10c =--a;//a=9,b=11,c=9b = ++a;//a=10,b=10,c=9A = c--;//a=9,b=10,c=8System.out.println ("A:" +a);//9System.out.println ("B:" +b);//TenSystem.out.println ("C:" +c);//8System.out.println ("--------------"); intx = 4; inty = (x + +) + (++x) + (x*10); //4+6+60//x=5,6System.out.println ("x:" +x);//6System.out.println ("y:" +y);// -    }}
/*face Test (possibility): short s=1;s = s+1;        Short s=1;s+=1;                The above two code has no problem, if there is, there is a problem.            Why is there a problem with the second wood?                        The extended assignment operator actually implies a forced type conversion.            s + = 1;            is not equivalent to S = s + 1; Instead, it is equivalent to S = (the data type of s) (S + 1);*/ Public classTEXT4 { Public Static voidMain (string[] args) {//Short s = 1; //s = s + 1; //System.out.println (s);//loss accuracy,                 Shorts = 1; S+ = 1;//seems to be s = s + 1;System.out.println (s);//2    }}
/*bitwise operators: &,|,^,~ <<,>>,>>> Note: To do bit operations, the first thing to do is to convert the data into binary. */ Public classTEXT4 { Public Static voidMain (string[] args) {//&,|,^,~                intA = 3; intb = 4; System.out.println (3 & 4);//0System.out.println (3 | 4);//7System.out.println (3 ^ 4);//7SYSTEM.OUT.PRINTLN (~);//-4    }}/*    Analysis: Because it is a bitwise operation, we must first convert the data into binary. 3 binary: 11 00000000 00000000 00000000 00000011 4 binary: 00000000 00000000 00000000 00000100 & Bit with        Operation: There are 0 0. 00000000 00000000 00000000 00000011 &00000000 00000000 00000000 00000100--------------------------------        ---00000000 00000000 00000000 00000000 The result is: 0 | bit or operation: 1 is 1.        00000000 00000000 00000000 00000011 |00000000 00000000 00000000 00000100-----------------------------------        00000000 00000000 00000000 00000111 The result is: 7 ^ bitwise XOR: The same is 0, the difference is 1. 00000000 00000000 00000000 00000011 &00000000 00000000 00000000 00000100-------------------------------- ---00000000 00000000 00000000 00000111 The result is: 7 ~ bitwise inverse Operator: 0 change to 0 00000000 00000000 00000000 00000011 ~11111111 11111111 11111111 11111100 (complement) complement: 11111111 11111111 11111111 11111100 Anti-code: 1111 1111 11111111 11111111 11111011       Original code: 10000000 00000000 00000000 00000100 The result is:-4 */
/*interview questions: Please realize the exchange of two integer variables Note: In the course of lectures later, I did not explicitly specify the type of data, the default int type. */ Public classTEXT4 { Public Static voidMain (string[] args) {intA = 10; intb = 20; System.out.println ("A:" +a+ ", B:" +b); //Mode 1: Use third-party variables (in development)        /*int c = A;        A = b;        b = C;        System.out.println ("A:" +a+ ", B:" +b);        System.out.println ("------------"); */                //Method 2: Use a different or realization (interview)//Left: A,b,a//Right: a ^ b        /*A = a ^ b; b = a ^ b; A ^ b ^ b = a A = a ^ b;        a^b^a=bsystem.out.println("a:"+a+",b:"+b); */                //Method 3: Adding variables to the procedure        /*A = a + b;//a=30 B = A-c;//b=10 a = a-B;//a=20 System.out.println ("A:" +a+ ", B:" +b); */                //Mode 4: One sentence to take care ofb = (a+b)-(A=B);//b=30-20=10,a=20System.out.println ("A:" +a+ ", B:" +b); }}

Java Base language operators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.