Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts
Java operator

Java operator

Java operator

One of the most basic uses of a computer is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:
  • Arithmetic operator
  • Relational operator
  • Bit operator
  • Logical operators
  • Assignment operator
  • Other operators

Arithmetic operator

Arithmetic operators are used in mathematical expressions, and their roles are the same as those in mathematics. The following table lists all the arithmetic operators.
The example in the table assumes that the value of the integer variable A is 10 and the value of the variable B is 20:
Operatordescriptionexample
+Add - add the value on both sides of the operatorA + B is equal to 30
-Subtract - left operand minus the right operandA - B is equal to -10
*Multiplication - multiplication operator on both sides of the valueA * B is equal to 200
/Divide - left operand divided by the right operandB / A is equal to 2
%The remainder of the modulo-left operand except the right operandB% A is equal to 0
++Increment: The value of the operand is incremented by oneB ++ or ++ B is equal to 21 (see below for details)
-The value of the operand is reduced by 1B - or - B is equal to 19 (see below for details)

Examples

The following simple example program demonstrates the arithmetic operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

Examples

public class the Test { public static void main ( String args [ ] ) { int A = 10 ; int B = 20 is ; int C = 25 ; int D = 25 ; the System . OUT . the println ( " A + B = " + ( A + B ) ) ; System . Out .the println ( " A - B = " + ( A - B ) ) ; the System . OUT . the println ( " A * B = " + ( A * B ) ) ; the System . OUT . the println ( " B / A = " + ( B / a ) ) ; System . Out . Println ( "B% A = " + ( B % A ) ) ; the System . OUT . the println ( " C% A = " + ( C % A ) ) ; the System . OUT . the println ( " A ++ = " + ( A ++ ) ) ; System . Out . Println ( " a-- = "+ ( A - ) ) ; // View the difference between d ++ and ++ d System . Out . Println ( " d ++ = " + ( d ++ ) ) ; System . Out . Println ( " ++ d = " + ( ++ d ) ) ; } }

Running examples »
The above example compiles the result as follows:
A + b = 30 
a - b = - 10 
a * b = 200 
b / a = 2 
b % a = 0 
c % a = 5 
a ++ = 10 
a - = 11 
d ++ = 25 ++ d    = 27                  
 

Self - incrementing operator

1, self-increment (++) The self-decrementing (-) operator is a special arithmetic operator that requires two operands to be manipulated in the arithmetic operator, and the auto-increment operator is an operand The

Examples

Public class selfAddMinus { public static void main ( String [ ] args ) { int a = 3 ; // define a variable; Int b = ++ a ; // self-incrementing operations Int c = 3 ; int d = - c ; // self-decrement operation System . Out . Println ( " value after self-incrementing is equal to " + b ) ; System . Out . Println ( " value after subtraction is equal to " + d ) ; } }
The result is:
The value after the self-increasing operation is equal to 4 and the value after subtraction is equal to 2
Analysis:
  • Int b = ++ a; The split operation is: a = a + 1 = 4; b = a = 4, the final result is b = 4, a = 4
  • Int d = --c; split operation process: c = c-1 = 2; d = c = 2, the final result is d = 2, c = 2
2, the prefix from the self-subtraction (++ a, - a): first self-increment or subtraction operation, and then the expression operation.
3, the suffix from the self-subtraction (a ++, a--): first expression operation , and then self-increment or self-reduction Example:

Examples

Public class selfAddMinus { public static void main ( String [ ] args ) { int a = 5 ; // define a variable; Int b = 5 ; int x = 2 * ++ a ; int y = 2 * b ++; System . Out . Println ( " self-increment operator prefix operation a = " + a + " , x = " + x ) ; System . Out . Println ( " since the addition operator suffix operation b = " + b + " , y = "+ Y ) ; } }
The result is:
Self-increment operator prefix operation a = 6 , x = 12 after the operator operator post- operation b = 6 , y = 10

Relational operator

The following table is a relational operator supported by Java
The value of the instance integer variable A in the table is 10 and the value of the variable B is 20:
Operatordescriptionexample
==Check if the values ​​of the two operands are equal, and if they are equal, the condition is true.(A == B) is false (not true).
! =Check if the values ​​of the two operands are equal, and if the values ​​are not equal, the condition is true.(A! = B) is true.
Check whether the value of the left operand is greater than the value of the right operand, and if so the condition is true.(A> B) is not true.
Check whether the value of the left operand is less than the value of the right operand, and if so the condition is true.(A <B) is true.
> =Check whether the value of the left operand is greater than or equal to the value of the right operand, and if so the condition is true.(A> = B) is false.
<=Check whether the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true.(A <= B) is true.

Examples

The following simple example program demonstrates the relational operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

Test.java File code:

Public class Test { public static void main ( String args [ ] ) { int a = 10 ; int b = 20 ; System . Out . Println ( " a == b = " + ( a == b ) ) ; System . Out . Println ( " a! = B = " + ( a !(A < b ) ) ; System . Out . Println ( " a> b = " + ( a > b ) ) ; System . Out . Println ( " a <b = " + ( a < b ) ) ; System . Out . Println ( " B> = a = " + ( b >= A ) ) ; System . Out . Println ( " b <= a = " + ( b <= a ) ) ; } }
The above example compiles the result as follows:
A == b = false 
a ! = B = true 
a > b = false 
a < b = true 
b > = a = true 
b <= a = false      

Bit operator

Java defines bitwise operators for types such as integer (int), long (long), short (short), character (char), and byte (byte).
The bitwise operator acts on all bits and operates bit by bit. Assume that a = 60, b = 13; their binary format representation would be as follows:
A = 0011 1100 
B = 0000 1101 ----------------- 
A & b = 0000 1100 
A | B = 0011 1101 
A ^ B = 0011 0001 ~ A = 1100 0011    
      
  
The following table lists the basic operations of the bitwise operators, assuming that the value of the integer variable A is 60 and the value of the variable B is 13:
Operatordescriptionexample
&If the corresponding bit is 1, the result is 1, otherwise 0(A & B), get 12, that is 0000 1100
|If the corresponding bit is 0, the result is 0, otherwise 1(A | B) to get 61, that is, 0011 1101
^If the corresponding bit value is the same, the result is 0, otherwise 1(A ^ B) to get 49, that is, 0011 0001
~The bitwise complement operator turns each bit of the operand, ie 0 becomes 1 into 0.(~ A) yields -61, i.e. 1100 0011
<< Bitwise left shift operator. Left Operand Bitwise The number of bits specified by the right operand.A << 2 gets 240, that is, 1111 0000
>> Bitwise right shift operator. The left operand bitwise right-shift the number of bits specified by the right operand.A >> 2 get 15 ie 1111
>>> Bitwise right shift padding operator. The value of the left operand is shifted right by the number of bits specified by the right operand, and the resulting space is filled with zeros.A >>> 2 get 15 that is 0000 1111

Examples

The following simple example program demonstrates the bitwise operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

Test.java File code:

public class the Test { public static void main ( String args [ ] ) { int A = 60 ; / * 60 = 0011 1100 is * / int B = 13 is ; / * 13 is = 0000 1101 * / int C = 0 ;      C = A & B ;       / * 12 = 0000 1100 * / System . Out . Println ( " a & b = "+ C ) ;      C = A | B ;       / * 61 is = 0011 1101 * / the System . OUT . The println ( " A | B = " + C ) ;      C = A ^ B ;       / * 49 = 0011 0001 * / the System . Out . Println ( " a ^ b = " + c ) ;      c = ~ a ;          / * -61 = 1100 is 0011 * / the System . OUT . The println ( " ~ = A " + C ) ;      C = A << 2 ;     / * 240 = 1111 0000 * / the System . OUT . The println ( " A << 2 = " + C ) ;      c = a >> 2 ;     / * 15 = 1111 * / System . Out . Println ( " a >> 2 = " + c ) ; c = a >>> 2 ; / * 15 = 0000 1111 * / System . Out . Println ( " a > 2 = " + c ) ; } }+ C ) ; c = a >>> 2 ; / * 15 = 0000 1111 * / System . Out . Println ( " a >>> 2 = " + c ) ; } }+ C ) ;       c = a >>> 2 ;     / * 15 = 0000 1111 * / System . Out . Println ( " a >>> 2 = " + c ) ; } }2 = " + c ) ; } }2 = " + c ) ;   } }                                                       
The above example compiles the result as follows:
A & b = 12 
a | b = 61 
a ^ b = 49 ~ a = - 61 
a << 2 = 240 
a >> 15 
a >>> 15   
      

Logical operators

The following table lists the basic operations of the logical operators, assuming that the Boolean variable A is true and the variable B is false
Operatordescriptionexample
&&Called logical and operator. The condition is true if and only if both operands are true.(A && B) is false.
| |Called a logical OR operator. If any of the two operands is true, the condition is true.(A | | B) is true.
TheCalled logical non-operator. Used to invert the logical state of an operand. If the condition is true, the logical non-operator will get false.The (A && B) is true.

Examples

The following simple example program demonstrates the logical operators. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

Examples

public class the Test { public static void main ( String args [ ] ) { Boolean A = to true ; Boolean B = to false ; the System . OUT . the println ( " A && B = " + ( A && B ) ) ; the System . OUT . the println ( " A || b = "+ ( A || b ) ) ; System . Out . Println ( " ! (A && b) = " +! ( A && b ) ) ; } }
The above example compiles the result as follows:
A && b = false 
a || b = true ! ( A && b ) = true  
  

Short circuit logic operator

When using the logical operator, the result is true if both operands are true, but when the first operation is false, the result must be false, and then the second An operation.

Examples

public class LuoJi { public static void main ( String [ ] args ) { int A = . 5 ; // define a variable; Boolean b = ( a < 4 ) && ( a ++ < 10 ) ; System . Out . Println ( "result of using short-circuit logical operators " + b ) ; System . Out . Println ( " a result is " + A ) ; } }
The result is:
The results using the logical operator is shorted to false 
A result is 5
Analysis: the program uses the short-circuit logic operator (&&), first determine the result of a <4 is false, then the result of b must be false, so no longer perform a second operation a ++ <10 judgment, so a value For 5.

Assignment operator

The following is the Java language support assignment operator:
Operatordescriptionexample
=A simple assignment operator that assigns the value of the right operand to the left operandC = A + B will assign the value of A + B to C
+ =The addition and assignment operators assign the left operand and the right operand to the left operandC + = A is equivalent to C = C + A
- =Subtracts the assignment operator, which subtracts the left operand and the right operand to the left operandC - = A is equivalent to C = C -
 A
* =Multiplication and assignment operators that assign the left operand and the right operand to the left operandC * = A is equivalent to C = C * A
/ =In addition to the assignment operator, it assigns the left operand to the right operand to the left operandC / = A is equivalent to C = C / A
(%) =Modulo and assignment operators that take the left operand and the right operand to the left operandC% = A is equivalent to C = C% A
<< =Left shift assignment operatorC << = 2 is equivalent to C = C << 2
>> =Right shift assignment operatorC >> = 2 is equivalent to C = C >> 2
& =Bitwise and assignment operatorsC & = 2 is equivalent to C = C & 2
^ =Bitwise Exclusive or Assignment OperatorC ^ = 2 is equivalent to C = C ^ 2
| =Bitwise or assignment operatorC = 2 is equivalent to C = C | 2

Examples

The simple example program demonstrates the assignment operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:

Test.java File code:

public class the Test { public static void main ( String args [ ] ) { int A = 10 ;      int B = 20 is ;      int C = 0 ;      C = A + B ;      the System . OUT . the println ( " C = A + B = " + C ) ;      c + = a ;      System .OUT . the println ( " C + = A = " + C ) ;      C - = A ;      the System . OUT . the println ( " C - = A = " + C ) ;      C * = A ;      the System . OUT . the println ( " C * = A = " + c ) ;      a = 10 ;      c = 15 ;      C / = A ;      the System . OUT . the println ( " C / = A = " + C ) ;      A = 10 ;      C = 15 ;      C % = A ;      the System . OUT . the println ( " C% = A = " + C ) ;      C << = 2 ;      System . Out .the println ( " C = 2 << = " + C ) ;      C >> = 2 ;      the System . OUT . the println ( " C = 2 >> = " + C ) ;      C >> = 2 ;      the System . OUT . the println ( " C >> = a = " + c ) ;      c & = a ;      The System . OUT . The println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . The println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . The println ( " C | = a = " + c ) ; } }OUT . the println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . the println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . the println ( " C | = A = " + c ) ; } }OUT . the println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . the println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . the println ( " C | = A = " + c ) ; } }the println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . the println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . the println ( " C | = A = " + c ) ; } }the println ( " C & = 2 = " + C ) ; C ^ = A ; the System . OUT . the println ( " C ^ = A = " + C ) ; C | = A ; the System . OUT . the println ( " C | = A = " + c ) ; } }+ C ) ; c ^ = a ; System . Out . Println ( " c ^ = a = " + c ) ; c | = a ; System . Out . Println ( " c | = a = " + c ) ; } }+ C ) ;      c ^ = a ;      System . Out . Println ( " c ^ = a = " + c ) ; c | = a ; System . Out . Println ( " c | = a = " + c ) ; } }Println ( " c ^ = a = " + c ) ; c | = a ; System . Out . Println ( " c | = a = " + c ) ; } }Println ( " c ^ = a = " + c ) ;      c | = a ;      System . Out . Println ( " c | = a = " + c ) ; } }+ C ) ; } }+ C ) ;   } }         
The above example compiles the result as follows:
C = a + b = 30 
c + = a   = 40 
c - = a = 30 
c * = a = 300 
c / = a = 1 
c % = a   = 5 
c << = 2 = 20 
c >> = 2 = 5 
c >> = 2 = 1 
c &= A   = 0 
c ^ = a    = 10 
c | = a    = 10                  

Conditional operator (? :)

The conditional operator is also called a ternary operator. The operator has three operands and needs to determine the value of the boolean expression. The operator's main thing is to decide which value should be assigned to the variable.
Variable x = ( expression ) ? Value if true : value if false     

Examples

Test.java File code:

public class the Test { public static void main ( String args [ ] ) { int a , b ; a = 10 ; // if the establishment of a equal to 1, b is set to 20, 30 or b = ( a == . 1 ) ? 20 : 30 ; the System . OUT . the println ( " the Value of b IS: " + b ) ; // if the establishment of a equal to 10, b is set to 20, 30 or B = ( a == 10 ) ? 20 : 30 ; System . Out . Println ( " Value of b is: " + b ) ; } }
The above example compiles the result as follows:
Value of b is : 30 Value of b is : 20  
  

Instanceof operator

This operator is used to manipulate an object instance and check whether the object is a specific type (class type or interface type).
The instanceof operator uses the following format:
( Object reference variable ) instanceof ( class / interface type )    
If the operator on the left side of the operator refers to an object on the right side of the operator or an interface (class / interface), the result is true.
Here is an example:
String name = "James" ; boolean result = name instanceof String ; // Returns true if name is of type String 
  
The operator still returns true if the object being compared is compatible with the right type.
Look at the following example:
class Vehicle { } public class Car the extends Vehicle { public static void main ( String args [ ] ) { Vehicle A = new new Car ( ) ; Boolean Result = A the instanceof Car ; the System . OUT . the println ( Result ) ; } }
The above example compiles the result as follows:
True

Java operator precedence

When multiple operators appear in an expression, who is the first? This involves the priority of the operator. In a multi-operator expression, the difference in operator precedence results in a very different result.
For example, (1 + 3) + (3 + 2) * 2, the expression is given the highest priority by the plus sign, the answer is 18, if the multiplication in accordance with the highest priority, the answer is 14.
In other words, x = 7 + 3 * 2; where x gets 13, not 20, because the multiplication operator has a higher priority than the addition operator, so calculate 3 * 2 to get 6 and then add 7.
The following table has the highest priority of the operator in the top of the table, the lowest priority at the bottom of the table.
categoryOperatorRelevance
suffix() []. (Dot operator)Left to right
One yuan+ + -! ~From right to left
Multiplication * /%Left to right
Additive + -Left to right
Shift >> >>> << Left to right
relationship >> = << = Left to right
equal ==! =Left to right
By bit with&Left to right
Bitwise exclusive OR^Left to right
By bit or|Left to right
Logic and&&Left to right
Logical or| |Left to right
conditionThe :From right to left
Assignment= + = - = * = / =% = >> = << = & = ^ = | =From right to left
comma,Left to right

Popular Posts