Java Operators and expressions

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators float double

1. Arithmetic operators and arithmetic expressions
(1) add minus operator +,-. such as 2+39,908.98-23.
The addition and subtraction operator is the binocular operator, which is the operator that joins the two operands. The combination of the plus and minus operators is from left to right. For example: 2+3-8, calculate 2+3 First, and then subtract the resulting result by 8. The operand of the add and subtract operator is an integer or float data, and the decrement operator has a priority of level 4.
(2) multiply, divide, and seek the remainder operator *,/,%. such as 2*39 908.98/23.
The *,/,% operator is the binocular operator, which is an operator that joins two operands. The *,/,% operator is bound from left to right, for example, 2*3/8, first calculating 2*3, and then dividing the resulting result by 8. The operand of the multiplication operator is an integer or float data. The precedence of the *,/,% operator is level 3.
A formula that conforms to the rules of Java syntax, such as x+2*y-30+3* (Y+5), that is concatenated with arithmetic symbols and parentheses.

2 self-increment, decrement operator + +,--
Self-increment, the decrement operator is the monocular operator, which can be placed before the operand or after the action element. Action Element must
Must be an integer or floating-point variable. The function is to increase the value of the variable by 1 or minus 1, as
++x,--x means that the value of x is added minus 1 before using X.
X++,x-indicates that the value of x is added minus 1 after using X.
Roughly, ++x and X + + are the equivalent of x=x+1. But the difference between ++x and X + + is that ++x is performing x=x+1 and then using the value of X., and X + + is the first to perform x=x+1 with the value of X. If the original value of X is 5, then
The value for Y=++x y is 6.
For y=x++ y, the value is 5, and the value of X becomes 6.

3 precision of arithmetic mixing operations
The order in which precision is arranged from "bottom" to "high" is
byte short int long float double
Java preserves the precision of the result by the highest precision of the operands on either side of the operator, for example
5/2 of the result is 2, to get 2.5, you must write 5.0/2 or 5.0F/2.
The precision of the result of char data and integer data operations is int. For example
BYTE x=7;
So
' B ' +x;
The result is an int, so the following notation is incorrect,
Char ch= ' B ' +x;
should be written
Char ch= (char) (' B ' +x);

4 relational operators and relational expressions
Relational operators are used to compare the relationships of two values. The result of the relational operator is a Boolean, and when the operator corresponds to a relationship, the result of the operation is true, otherwise false. For example, the result of 10<9 is that the result of False,5>1 is true,3!= The result of 5 is that the result of TRUE,10>20-17 is true because the level of the arithmetic operator is higher than the relational operator, 10>20-17 phase
When at 10> 20-17, the result is of course true.
A variable or expression of a numeric value can form a relational expression through a relational operator. For example, 4>8, (x+y) >80.

5 logical operators and logical expressions
Logical operators include &&,| |,!. where &&,| | is a two-mesh operator that implements logic and, logically, or a single-mesh operator that implements a logical non-. The operand of a logical operator must be a Boolean data that can be used to concatenate a relational expression.

For example , the result of 2>8&&9>2 is true for false,2>8| |9>2. Because the level of the relational operator is higher than the &&,| | Level, 2>8&&8>2 is equivalent to 2>8 && 9>2.
logical operators "&&" and "| |" Also known as a short-circuit logical operator, because when the value of OP1 is false, the "&&" operator no longer calculates the value of OP2 when it is operating, and it directly concludes that the result of OP1&&OP2 is false. When the value of OP1 is true, the "| |" The operation symbol does not calculate the value of the OP2 at the time of operation, and the result of OP1|OP2 is true. For example, X
The initial value is 1, then after the following logical comparison operation,
((Y=1) ==0)) && ((x=6) ==6));
The value of x is still 1. After the following logical comparison operation,
((Y=1) ==1)) && ((x=6) ==6));
The value of X will change to 6.

6 assignment operators and assignment expressions
Assignment operator =.
The assignment operator is the binocular operator, and the action element on the left must be a variable, not a constant or an expression. Set X is an integer variable, y is a Boolean variable, x=20 and y = True are the correct assignment expressions, the assignment operator has a lower priority and is level 14. Combine direction right to left. The value of an assignment expression is the value of the left variable of "=". Note Do not confuse the assignment operator "=" with the equals operator "= =".

7 bitwise operator
We know that integer data is represented in memory in 2-binary form, For example, a variable of type int has 32 bits in memory, 4 bytes for int data 7, 2 for the
00000000 00000000 00000000 00000111
, the highest bit on the left is the sign bit, the highest bit is 0 for positive number, 1 indicates a negative number. Negative numbers are in complement notation, for example-8 is
111111111 111111111 1111111 11111000
So we can perform bitwise operations on integer data, for example, The bits corresponding to two integer data are calculated to get a new integer data
1 bitwise-and "operator The
& is a binocular operator that evaluates to two integer data, a, a, a and a bitwise, and the result of the operation is an integer data C. The algorithm is if a, b two data corresponding bit is 1, then the bit of C is 1, otherwise 0. If the precision of B is higher than a, then the accuracy of C is the same as B

2 " bitwise OR "operator
" | is the binocular operator. For two integer data, a, a bitwise operation, the result is an integer data C. The algorithm is if a, b two data corresponding bit is 0, then the bit of C is 0, otherwise 1. If the precision of B is higher than a, then the precision of the result C is the same as B.
3 "bitwise NOT" operator
"~" is the monocular operator. A bitwise operation of an integer data, The result of the operation is an integer data C. The algorithm is if a corresponds to a bit is 0, then the bit of C is 1, otherwise it is 1.
Bitwise XOR Operator
4 "^" is the binocular operator. Performs a bitwise operation on two integer data, a, B, The result of the operation is an integer data C. The algorithm is if a, b two data corresponding bit is the same, then the bit of C is 0, otherwise 1. If the precision of B is higher than a, then the accuracy of C is the same as B.
is known by the XOR algorithm
A^a=0,
A^0=a.
Therefore, if c=a^b, then a=c^b, that is, the same number of logarithm a two "XOR" the result of the operation is a number a. In the following example 1, using the nature of the "XOR" operation, Encrypt several characters and output the ciphertext before decrypting.

example 1
class Example1
{public static void main (String args[])
{char a1= ' ten ', a2= ' point ', a3= ' in ', a4= ' attack ' ;
Char secret= ' 8 ';
A1= (char) (A1^secret); a2= (char) (A2^secret);
A3= (char) (A3^secret); a4= (char) (A4^secret);
System.out.println ("ciphertext:" +A1+A2+A3+A4);
A1= (char) (A1^secret); a2= (char) (A2^secret);
A3= (char) (A3^secret); a4= (char) (A4^secret);
System.out.println ("Original:" +A1+A2+A3+A4);
}
}

Bitwise operators can also manipulate logical data, the law is
When a, B is true, A&b is true, otherwise a&b is false.
When a, B is false, A|b is false, otherwise a|b is true.
When a is true, ~a is false when a is false, ~a is true.
Bitwise operators when manipulating logical data, with logical operators &&,| |, the difference is that the bitwise operator calculates the result of the operation after calculating A and B. For example, the initial value of X is 1, then after the following logical comparison operation,
((Y=1) ==0)) && ((x=6) ==6));
The value of x is still 1, but after the following bitwise operation,
((Y=1) ==0)) & ((x=6) ==6));
The value of x will be 6.
Bitwise operators can also manipulate character data, but the result of the operation is an int type of data, such as
Char x= ' A ', y= ' B ';
So
X^y,x&y,x^y, X
The result is the int type.
The following example shows the difference between a short-circuit logic operation and a bit operation.

Example 2
Class Example2
{public static void main (String args[])
{int x,y=10;
if (((x=0) ==0) | | ((y=20) ==20))
{SYSTEM.OUT.PRINTLN ("The value of y now is:" +y);
}
int a,b=10;
if (((a=0) ==0) | ( (b=20) ==20))
{System.out.println ("Now the value of B is:" +b);
}
}
}

8 instanceof operator
The operator is the binocular operator, and the action element on the left is a class to the right of an object. The result of the operator operation is true if the object on the left is the object created by the right, otherwise false.

9 Operator Overview
The Java expression is a Java-compliant formula that is concatenated with operators. The precedence of an operator determines the order in which operations are performed in an expression. For example, x<y&&!z corresponds to (x<y) && (!z), There is no need to memorize the priority level of the operation symbol, when writing a program, you can use the parentheses operation symbol to achieve the order of operations you want, so as not to produce difficult to read or ambiguous calculation Order. The binding of operators determines the order of operators of the same level, for example, the binding of add and subtract is left to right, 8-5 +3 equals 8-5 +3. The binding of the logical no operator is right to left and x equals! (!x). Table 3.4 is the precedence and binding of all Java operators, some of which we do not introduce, see related books.


Java Operators and expressions

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.