Learning java together [6] --- Operators

Source: Internet
Author: User

When several variables are involved in the operation, the result type depends on the variable type that represents the largest range in these variables. For example, if the involved variable contains an integer, a double-precision floating point, or a short integer, the final result type is double.
 
Int a = 1;
Int B = 2;
Double c = (double) a/B;
 
In the above Code, both a and B are integer, but a is converted into an anonymous variable through the (double) a conversion. The type of the variable is double. However, note that a is still of the int type rather than the double type. In this way, (double) a/B is of the double type divided by the int type, and the result is of the double type.
 
Modulo operator: %; is actually a problem of finding the remainder!
 
The result symbol of the modulo operation is always the same as that of the divisor. This rule is very important.
 
Example: int a = 5;
Int B =-3;
Int c = a % B;
Then the output value of c is 2;
 
If int a =-5;
The value of c output in the preceding example is-2;
 
Relational operators: greater than (>), less than (<), equal to (=), not equal (! =), Greater than or equal to (> =), less than or equal to (<=); the results of relational operations are boolean values.
 
Logical operators: two operators are important. The logical operators also return a boolean value.
Logic and: Use & to indicate that the logic is a binary operator (that is, an operator with two operands). The logic and result are true only when both operands are true, results In other cases are false. Logic and representation are and meaning.
Logic or: Use | indicates that the logic is also a dual-Objective operator. The result is false only when both operands are false. In other cases, the result is true. Logical or represents the meaning of or, a meaning of choice.
 
Short-circuit features of logical operators (this is important !)
1. Logic and: if the first operand is false, the result must be false. Therefore, the logic and subsequent operations are not executed, that is, a short circuit occurs;
2. Logic or: if the first operand is true, the result must be true, so the logic or subsequent operations will not be executed, that is, a short circuit will occur.
 
For example, analyze the following code:
Public class Operator2
{
Public static void main (String [] args)
{
Int a = 1;
Int B = 2;
Int c = 3;
Int d = 4;
Int f = 5;
 
Boolean e = (a> B) & (f = c) <d );
 
System. out. println (e );
System. out. println (f );
}
}
When we determine that a> B is false, the operations following & do not need to be executed. Therefore, the value of e is false, the value of f is defined at the beginning. Because & is not followed, f = c is not executed. This is a short circuit, which is very important, it is also easy to ignore. This must be clearly understood.
 
Auto-increment and auto-increment operations on variables:
A ++ and ++:
Int a = 3;
Int B = a ++;
 
At this time, the output value B is 3, while the output value a is 4;
 
Int a = 3;
Int B = ++;
At this time, the output value B is 4, while the output value a is 4;
 
When ++ is in the beginning, the variable is first Auto-incrementing, and then the value after auto-incrementing is assigned to the variable on the left;
In addition, ++ first assigns the variable's value to the variable on the left, and then performs auto-increment.
So the difference is here.
Author: "Li Yuan Cao"

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.