Leilin Peng Share: Java operator

  One of the most basic use of a computer is to perform mathematical operations, as a computer language, Java provides a rich set of operators to manipulate variables. We can put the operator into the following groups:

  Arithmetic Operators

  Relational Operators

  Bitwise Operators

  Logical Operators

  Assignment Operators

  Other Operators

  Arithmetic Operators

  Arithmetic operators used in a mathematical expression, function and their role in the mathematics of the same. The following table lists all the arithmetic operators.

  Examples of the variables in the table given by an integer value of 10 A, the value of variable B 20:

  Operator Description Example

  + Addition - both operators value addition operation A + B is equal to 30

  - subtraction - subtracting the left and right operand Operand A - B is -10

  * Multiplication - multiplication operator sides equal to the value of A * B 200

  / Division - left operand is divided by the right operand B / A is equal to 2

  % Modulus - except left and right operand operation remainder is equal to 0 B% A

  ++ increment - value of the operation increases. IB is equal to 21 + +

  - Decrement - reducing the value of the operand. IB - - equal to 19

  Examples

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

  public class Test {

  public static void main(String args[]) {

  int a = 10;

  int b = 20;

  int c = 25;

  int d = 25;

  System.out.println("a + b = " + (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) );

  System.out.println("c % a = " + (c % a) );

  System.out.println("a++ = " + (a++) );

  System.out.println("a-- = " + (a--) );

  // Check a different d ++ and ++ d of

  System.out.println("d++ = " + (d++) );

  System.out.println("++d = " + (++d) );

  }

  }

  The above examples compiled results are as follows:

  a + b = 30

  a - b = -10

  a * b = 200

  b / a = 2

  b % a = 0

  c % a = 5

  a++ = 10

  b-- = 11

  d++ = 25

  ++d = 27

  Relational Operators

  The following table shows the supported Java relational operators

  Is 10, the table variable B Example A value of the integer variable 20:

  Operator Description Example

  == Check if both operands are equal, it is equal if the condition is true. (A == B) is false (not true).

  ! = Check if both operands are equal, if the values ​​are not equal then the condition is true. (A! = B) is true.

  > Check the value of the left operand is greater than right operand value, then if the condition is true. (A> B) is not true.

  <Check the value of the left operand is less than right operand value, then if the condition is true. (A <b) is true. <P = "">

  > = Check left operand value is greater than or equal to right operand, then if the condition is true. (A> = B) is false.

  <= Check value of the left operand is less than or equal to right operand, if it is then the condition is true. (A <= B) is true.

  Examples

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

  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 != 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 examples compiled results are as follows:

  a == b = false

  a != b = true

  a > b = false

  a < b = true

  b >= a = true

  b <= a = false

  Bitwise Operators

  Java defines bitwise operators, applied to integer type (int), long integer (Long), short integer (Short), char (char), and byte (byte) type and the like.

  Bitwise operators acting on all the bits, and bitwise operations. Suppose a = 60, and b = 13; they will be represented in binary format as follows:

  A = 0011 1100

  B = 0000 1101

  -----------------

  A&b = 0000 1100

  A | B = 0011 1101

  ^ B = 0011 0001

  ~A= 1100 0011

  The following table lists the basic operations of bitwise operators, assuming an integer value of the variable A and the variable B is 60 13:

  Operator Description Example

  & Bitwise AND operator, if and only if this bit is a bit in both operands are non-zero only when the result is 1. (A & B), to give 12, i.e., 0000 1100

  | Bitwise OR operator, a bit as long as the two operands have a non-zero result when the bit is set to 1. (A | B) to give 61, i.e., 00,111,101

  ^ Bitwise XOR operator, a bit different when two operands the result of the bit is 1. (A ^ B) to give 49, i.e., 00,110,001

  ~ Bitwise complement operator of each reversing operation. (~ A) -60 obtained, i.e., 11,000,011

  << bitwise left shift operator. Bitwise left left operand right operand the specified number of digits. A << 2 to give 240, i.e. 11,110,000

  >> bitwise right shift operator. The left operand by the right operand bits specify the right number of digits. I.e., obtain 15 A >> 2 1111

  >>> zero padding bit right shift operator. The median value of the left operand specified by the operand right RIGHT movement resulting vacancy is filled with zeros. I.e., obtain 15 A >>> 2 0000 1111

  Examples

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

  public class Test {

  public static void main(String args[]) {

  int a = 60; /* 60 = 0011 1100 */

  int b = 13; /* 13 = 0000 1101 */

  int c = 0;

  c = a & b; /* 12 = 0000 1100 */

  System.out.println("a & b = " + c );

  c = a | b; /* 61 = 0011 1101 */

  System.out.println("a | b = " + c );

  c = a ^ b; /* 49 = 0011 0001 */

  System.out.println("a ^ b = " + c );

  c = ~a; /*-61 = 1100 0011 */

  System.out.println("~a = " + c );

  c = a << 2; /* 240 = 1111 0000 */

  System.out.println("a << 2 = " + c );

  c = a >> 2; /* 215 = 1111 */

  System.out.println("a >> 2 = " + c );

  c = a >>> 2; /* 215 = 0000 1111 */

  System.out.println("a >>> 2 = " + c );

  }

  }

  The above examples compiled results are 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 operation logic operators, assuming A Boolean variable is true, the variable B is false

  Operator Description Example

  && called logical AND operator. If and only if both operands are true, the condition will be true. (A && B) is false.

  | | Called a logical OR operator. If either of the operands is true either condition is true. (A | | B) is true.

  ! Called logical NOT operator. Inverts the logic state of the operand. If the condition is true, the logic NOT operator will give false. ! (A && B) is true.

  Examples

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

  public class Test {

  public static void main(String args[]) {

  boolean a = true;

  boolean b = false;

  System.out.println("a && b = " + (a&&b));

  System.out.println("a || b = " + (a||b) );

  System.out.println("!(a && b) = " + !(a && b));

  }

  }

  The above examples compiled results are as follows:

  a && b = false

  a || b = true

  !(a && b) = true

  Assignment Operators

  Here is the Java language to support the assignment operator:

  Operator Description Example

  = Simple assignment operator, the value assigned to the left and right operand Operand C = A + B A B will be assigned a value obtained + C

  + = Assignment operator and add it to the left and right operands operand assigned to the left operand sum C + = A is equivalent to C = C + A

  - = subtraction and assignment operator that the left and right operand operand subtraction assigned to the left operand C - = A is equivalent to the C = C -

  A

  * = Multiplication assignment operator and its operand to the left and right operand multiplied assigned to the left operand C * = A is equivalent to C = C * A

  / = Assignment operator and in addition, it is the left and right operands operand assigned to the divided left operand C / = A is equivalent to C = C / A

  (%) = Modulo assignment operator and its operand to the left and right operand after assignment to the left operand modulo C% = A is equivalent to the C = C% A

  << = left shift assignment operator C << = 2 is equivalent to the C = C << 2

  >> = right shift assignment operator C >> = 2 is equivalent to the C = C >> 2

  = & Bitwise AND assignment operator C & = 2 is equivalent to the C = C & 2

  ^ = Bitwise XOR assignment operator C ^ = 2 is equivalent to the C = C ^ 2

  | = Bitwise assignment operator or C | = 2 is equivalent to the C = C | 2

  Examples

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

  public class Test {

  public static void main(String args[]) {

  int a = 10;

  int b = 20;

  int c = 0;

  c = a + b;

  System.out.println("c = a + b = " + c );

  c += a ;

  System.out.println("c += a = " + c );

  c -= a ;

  System.out.println("c -= a = " + c );

  c *= a ;

  System.out.println("c *= a = " + c );

  a = 10;

  c = 15;

  c /= a ;

  System.out.println("c /= a = " + c );

  a = 10;

  c = 15;

  c %= a ;

  System.out.println("c %= a = " + c );

  c <<= 2 ;

  System.out.println("c <<= 2 = " + c );

  c >>= 2 ;

  System.out.println("c >>= 2 = " + c );

  c >>= 2 ;

  System.out.println("c >>= a = " + c );

  c &= a ;

  System.out.println("c &= 2 = " + c );

  c ^= a ;

  System.out.println("c ^= a = " + c );

  c |= a ;

  System.out.println("c |= a = " + c );

  }

  }

  The above examples compiled results are 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

  The conditional operator (? :)

  Conditional operator is also called ternary operator. The operator has three operands, and needs to determine the Boolean expressions. This operator is primarily to determine which value should be assigned to the variable.

  variable x = (expression) ? value if true : value if false

  Examples

  public class Test {

  public static void main(String args[]){

  int a , b;

  a = 10;

  b = (a == 1) ? 20: 30;

  System.out.println( "Value of b is : " + b );

  b = (a == 10) ? 20: 30;

  System.out.println( "Value of b is : " + b );

  }

  }

  The above examples compiled results are as follows:

  Value of b is : 30

  Value of b is : 20

  Operators instanceOf

  The operator is used to operate an object instance, to check whether the object is of a particular type (class type or interface type).

  instanceof operator using the following format:

  ( Object reference variable ) instanceOf (class/interface type)

  If the object refers to the operator the left variable, the operator is right or interface class (class / interface) of an object, then the result is true.

  Below is an example:

  String name = 'James';

  boolean result = name instanceOf String; // name is due Strine type, it returns true

  If comparisons are compatible with the right type, the operator still returns true.

  See the examples below:

  class Vehicle {}

  public class Car extends Vehicle {

  public static void main(String args[]){

  Vehicle a = new Car();

  boolean result = a instanceof Car;

  System.out.println( result);

  }

  }

  The above examples compiled results are as follows:

  true

  Java operator precedence

  When more than one operator appears in an expression, after who should and who does? This relates to the priority of the operators of the problem. In an expression more than one operator, the operator precedence may lead to the conclusion that different results vary widely.

  For example, (1 + 3) + (3 + 2) * 2, if the expression is the highest priority number is calculated by adding the answer is 18, if the highest priority in accordance with the multiplication, the answer is 14.

  Again, x = 7 + 3 * 2; wherein x obtained 13 instead of 20 because the multiplication operator has higher precedence than the addition operator, so the first 3 * 2 is calculated to give 6, and 7 together.

  The table top having the highest priority in the operator table, the bottom of the lowest priority in the table.

  Category correlation operator

  Suffix () []. (Dot operator) left to right

  One yuan + + -! - right to left

  Multiplicative * /% left to right

  Additive + - left to right

  >>> >> << shift left to right

  Relations >> = << = Left to Right

  Equal ==! = Left to right

  Bitwise AND & Left to Right

  Bitwise XOR ^ left to right

  Bitwise OR | Left to Right

  Logical AND && left to right

  Logical OR | | Left to Right

  Conditions:? From right to left

  Assignment = + = - = * = / =% = >> = << = & = ^ = | = right to left

  Comma, left to right

  (Editor: Leilin Peng Source: Network | invasion deleted)

Guess you like

Origin www.cnblogs.com/pengpeng1208/p/12598204.html