Java Basics-Operators (03)

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators

Concept:

Operator: is the symbol that operates on constants and variables.

Expressions: Java-syntactically-compliant formulas that are concatenated with operators, and expressions that are joined by different operators are expressions of different types.

Operator Classifications:

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

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

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

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

Ternary operators

Arithmetic operators

Dividing integers can only get integers. to get a decimal, you must have a floating-point number to participate in the Operation .

The character participates in the addition operation (the pure numeric value for the addition operation, which contains the string for the character connection, char->int->string), is actually the character stored in the computer data values (ASCII code table) To participate in the operation.

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

Arithmetic operator + +--a small problem. is calculated from left to right and then left-assigned value

int x = 4; int x2 = 4; int y = (x--) + (--x) + (x*10);      // The 4+2+20=26 (x--) value is 4,x 3, and then the (--x) value is 2,x 2; int y2 = (--x2) + (x2--) + (x2*10);  // The 3+3=26 (--x2) value is 3,x2 3, and then the (x2--) value is 3,x2 2; // x = 2,y = 26,y2 =

Arithmetic operator + +--the sinkhole in

int a=1,b=0;    b=a++;     // The first assignment and then the self-addition operation of this procedure results in, b=1,a=2   int a=1;    A=a++;    // a first assigns the value to oneself is 1, then A and then adds 1 This procedure result to be, a=1, very strange has the wood to have the???

Here is the knowledge of the operating system, simply say: Our usual variables and objects are in memory open a piece of memory. And the operation is only CPU-capable. The hole in this topic is a value assigned to the self-added moment of the operation only in the CPU, the result of the calculation exists in the register, is not assigned to an in-memory variable.

It can also be understood as: The assignment operator is to store the results of the CPU registers in memory;

Assignment operations

Basic Replication operators: =

Extended assignment Operator: + = = *=/= ...

+ = Take the left and right data to do +, then assign the result to the left

Note: The extended assignment operator, which implicitly enforces type conversions, forces the type to the left of the type

For example: variable a,a+=10; equivalent to a= (data type of a) (A+10);

Relational operators

==,!=,>,<,>=,<=

The result of the relational operator is a Boolean type.

logical operators

The logical operator is the one used to connect the relationship expression

&,|,!, ^ (XOR or the same false, different true, can be understood as a couple relationship, the same sex is not a couple, sex is different from the couple)

&& (as with & results), | | (As with | results)

Ternary operators

Relationship expressions? Expression 1: Expression 2

A. Evaluates the value of the relationship expression to see if it is true or false B. If true, expression 1 is the result; if false, Expression 2 is the result

 Shorts = 30; inti = 50; //s = i+s; //error cannot convert from int to shorts + = i;//+=the assignment operator has the display type conversion function, which is equivalent to s= (short) s=i; !!!System.out.println ("s=" +s);// the        intx = 0; inty = 0; intz = 0; Booleanb; A= (x>0) & (y++>1);//False&falseSystem.out.println ("a=" +a);//falseSystem.out.println ("y=" +y);//1b = (x>0) && (z++>1);//false first False then the second one does not operateSystem.out.println ("b=" +b);//falseSystem.out.println ("z=" +z);//so here's 0 instead of 1.A = (x>0) | (y++>1);//False|falseSystem.out.println ("a=" +a);//falseSystem.out.println ("y=" +y);//2b = (x>0) | | (z++) >1;//false| | False short-circuit operation, the first is true when the second one does not operateSystem.out.println ("b=" +b);//falseSystem.out.println ("z=" +z);//1

Java Basics-Operators (03)

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.