Operators (also called operators) in the Java language are very similar in style and functionality to C and C + +. This chapter introduces some of the main operators in the Java language, including arithmetic operators, relational operators, logical operators, bitwise operators, and other operators.
6.1 Arithmetic operators
The arithmetic operators in Java are primarily used to organize arithmetic operations of numeric type data, which can be divided into unary and two-tuple operators, depending on the operand of the participating operation.
6.1.11-dollar operator
There are 3 arithmetic unary operations, namely-, + + and –. See table 6-1 for specific instructions.
Table 6-1 unary arithmetic operations
operator |
name |
Description |
Example |
- |
take the inverse symbol |
negation operation |
b =-a |
++ |
add a |
First take the value plus one, or first add the value again |
a++ or ++a |
– |
|
|
a– or –a |
In table 6-1,-A is a negation of a, a++ or a – is a plus or minus one after the expression is finished. The ++a or –a is a plus one or minus one, then the expression operation.
The sample code is as follows:
int a = 12; System.out.println (-a); ①int B = a++; ②system.out.println (b); b = ++a; ③system.out.println (b); 1234567
The output results are as follows:
-1212141234
The above code ① line is-A, the A variable is reversed, the result output is-12. The first line of ② code is to assign a value to the B variable plus one, that is, the first assignment after + +, so the output is 12. The ③ line code is to add a plus one, and then assign a to the B variable, that is, the first + + after the assignment, so the output is 14.
6.1.22-dollar operator
Binary operators include: + 、-、 *,/, and%, which are valid for numeric type data, see table 6-2 for details.
Table 6-2 Two-dollar arithmetic operations
operator |
name |
Description |
Example |
+ |
Add |
The and of a plus B can also be used for string types, strings join operations |
A + b |
- |
Reducing |
Find the difference between a minus B |
A-B |
* |
By |
The product of a multiplied by B |
A * b |
/ |
Except |
The quotient of a divided by B |
A/b |
% |
Take surplus |
To find the remainder of a divided by B |
A% b |
The sample code is as follows:
Declares a character-type variable char charnum = ' A '; // declares an integer-type variable int intresult = charnum + 1; ①system.out.println (intresult);intresult = intresult - 1; System.out.println (intresult);intresult = intresult * 2; System.out.println (intresult);intresult = intresult / 2; System.out.println (intresult); Intresult = intresult + 8;intresult = intresult % 7; System.out.println (Intresult); System.out.println ("-------");// declares a floating-point variable double doubleresult = 10.0; System.out.println (doubleresult);d oubleresult = doubleresult - 1; System.out.println (doubleresult);d oubleresult = doubleresult * 2; System.out.println (doubleresult);d oubleresult = doubleresult / 2; System.out.println (doubleresult);d Oubleresult = doubleresult + 8;doubleresult = doubleresult % 7; System.out.println (Doubleresult); 12345678910111213141516171819202122232425262728293031323334353637
The output results are as follows:
6665130653-------10.09.018.09.03.0123456789101112
In the above example, the numeric type data is two-dollar operation, where code ① will be the character type variable charnum and the integer type of the addition operation, participate in the operation of the character (' A ') of the Unicode encoding 65. The other code is relatively simple no longer repeat.
6.1.3 Arithmetic assignment operator
The arithmetic assignment operator is a shorthand, typically used for changes in the variables themselves, as described in table 6-3.
Table 6-3 arithmetic Assignment characters
operator |
name |
Example |
+= |
Add Value |
A + = B, a + = B+3 |
-= |
Reduced assignment value |
A-= b |
*= |
Multiply Assign Value |
A *= b |
/= |
In addition to assigning values |
A/= b |
%= |
Assignment of residual value |
A%= b |
The sample code is as follows:
int a = 1;int b = 2;a += b; // equivalent to a = a + bsystem.out.println (a);a += b + 3; // equivalent to a = a + b + 3system.out.println (a);a -= b; // equivalent to a = a - bsystem.out.println (a);a *= b; // equivalent to a=a*bsystem.out.println (a); a /= b; / / equivalent to a=a/bsystem.out.println (a);a %= b; // equivalent to a=a %bsystem.out.println (a); 123456789101112131415161718
The output results are as follows:
38612601234567
The above examples respectively for the integral type of + =,-=, *=,/= and%= operations, the specific statements are not described.
6.2 Relational operators
A relational operation is an operation that compares two expression-size relationships, which results in Boolean-type data, which is true or false. There are 6 types of relational operators: = =,! =, >, <, >=, and <=, see table 6-4 for details.
Table 6-4 Relational operators
[Image upload failed ...] (image-24a409-1510533942975)]
* * prompt = = and! = can be applied to base data types and reference types. When used for reference type comparisons, the comparison is whether two references point to the same object, but in most cases the actual development process is comparing the content of the object to the same object, rather than comparing it.
**
The sample code is as follows:
int value1 = 1;int value2 = 2;if (value1 == value2) { system.out.println ("Value1 == value2");} if (value1 != value2) { system.out.println ("value1 != Value2 ");} if (value1 > value2) { system.out.println ("value1 > value2 ");} if (value1 < value2) { system.out.println ("value1 < value2 ");} if (value1 <= value2) { system.out.println ("value1 <= value2 ");} 1234567891011121314151617181920212223
The results of the run program output are as follows:
Value1! = value2value1 < value2value1 <= value21234
6.3 Logical operators
The logical operator is to operate on a Boolean variable, and the result is a Boolean type, as described in table 6-5.
Table 6-5 logical operators
[Image upload failed ...] (image-6280e7-1510533942975)]
Tips Short circuit with (&&) and short circuit or (| | Can be used to optimize the calculation method, thereby improving efficiency. In the actual programming, the use of short-circuit and short-circuit or should be given priority.
The sample code is as follows:
int i = 0;int a = 10;int b = 9;if ((a > b) | | (i == 1) { ① System.out.println ("or arithmetic is true");} else { system.out.println ("or operation is false");} if (a < b && (i == 1)) { ② system.out.println ("and arithmetic for true");} else { system.out.println ("with arithmetic for false");} if ((a > b) | | (a++ == --b) { ③ system.out.pRintln ("a = " + a); system.out.println ("b = " + &NBSP;B);} 1234567891011121314151617181920
The above code runs the output as follows:
Or the operation is true and the operation is false 123
Where the ① line of code for short-circuit calculation, because (a > B) is true, the subsequent expression (i = = 1) is no longer evaluated, the output is true. Similarly, the ② Line code is also shorted, since (a < b) is false, the subsequent expression (i = = 1) is no longer evaluated and the result of the output is false.
The code in line ③ in the conditional expression doped with + + and-operation, because (a > B) is true, the subsequent expression (a++ ==–b) is no longer evaluated, so the last is a = ten, B = 9. If you put a short circuit or (| | ) to Logical OR (|), the result of the output is a = one and B = 8.
Companion video
Http://edu.51cto.com/topic/1246.html
Supporting source code
http://www.51work6.com/book/java1.php
and the free version of this book corresponds to a fee version:
Enter Baidu to read ebook
Access to Turing Community ebook
This article is from the "Dongsheng" blog, please be sure to keep this source http://tonyguan.blog.51cto.com/701759/1981143
The 6th operator of Java from small white to Daniel Lite (top)