Java Tutorial/Operators/Operators

Материал из Java эксперт
Перейти к: навигация, поиск

Operator Precedence

Operators with a higher precedence are executed before those of a lower precedence.

Operators on the same line have the same precedence:

Operator Precedence GroupAssociativityOperator Precedence(), [], postfix ++, postfix --leftHighestunary +, unary -, prefix ++, prefix --, ~, !right(type), newleft*, /, %left+, -left<<, >>, >>>left< ,<= , >, >=, instanceof==, !=&left^left|left&&left||left?:left=, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=rightlowest


Six categories of operators

In Java, there are six categories of operators.

  1. Unary operators
  2. Arithmetic operators
  3. Relational and conditional operators
  4. Shift and logical operators
  5. Assignment operators
  6. Other operators



   <source lang="java">

Precedence Operator Description Association 1 ++,-- Postincrement, Postdecrement R -> L 2 ++,-- Preincrement, Predecrement R -> L

              +,-          Unary plus, unary minus              R -> L
              ~            Bitwise compliment                   R -> L
              !            Boolean NOT                          R -> L

3 new Create object R -> L

              (type)       Type cast                            R -> L

4 *,/,% Multiplication, division, remainder L -> R 5 +,- Addition, subtraction L -> R

              +            String concatenation                 L -> R

6 <<, >>, >>> Left shift, right shift, unsigned right shift L -> R 7 <, <=, >, >= L -> R

              instanceof   Type comparison                      L -> R

8 ==, != Value equality and inequality L -> R

              ==, !=       Reference equality and inequality    L -> R

9 & Boolean AND L -> R

              &            Bitwise AND                          L -> R

10 ^ Boolean XOR L -> R

              ^            Bitwise XOR                          L -> R

11 | Boolean OR L -> R

              |            Bitwise OR                           L -> R

12 && Conditional AND L -> R 13 || Conditional OR L -> R 14  ?: Conditional Ternary Operator L -> R 15 =,+=,-=, Assignment Operators R -> L

              *=,/ =,%=,
              &=,^=, |=, 
              <<=, >> =, 
              >>>=</source>
   
  
 
  



Tests all the operators on all the primitive data types to show which ones are accepted by the Java compiler

   <source lang="java">

public class MainClass {

  public static void main(String[] a){
     boolTest(true, false);
     charTest("x", "y");
     byteTest((byte)0, (byte)1);
     shortTest((short)0, (short)1);
     intTest(1, 2);
     longTest(11L, 22L);
     floatTest(1.1F, 2.2F);
     doubleTest(1.1, 2.2);
  }
 // To accept the results of a boolean test:
 static void f(boolean b) {
    System.out.println("f:"+b);  
 }
 static void boolTest(boolean x, boolean y) {
   // Arithmetic operators:
   //! x = x * y;
   //! x = x / y;
   //! x = x % y;
   //! x = x + y;
   //! x = x - y;
   //! x++;
   //! x--;
   //! x = +y;
   //! x = -y;
   // Relational and logical:
   //! f(x > y);
   //! f(x >= y);
   //! f(x < y);
   //! f(x <= y);
   f(x == y);
   f(x != y);
   f(!y);
   x = x && y;
   x = x || y;
   // Bitwise operators:
   //! x = ~y;
   x = x & y;
   x = x | y;
   x = x ^ y;
   //! x = x << 1;
   //! x = x >> 1;
   //! x = x >>> 1;
   // Compound assignment:
   //! x += y;
   //! x -= y;
   //! x *= y;
   //! x /= y;
   //! x %= y;
   //! x <<= 1;
   //! x >>= 1;
   //! x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! char c = (char)x;
   //! byte B = (byte)x;
   //! short s = (short)x;
   //! int i = (int)x;
   //! long l = (long)x;
   //! float f = (float)x;
   //! double d = (double)x;
 }
 static void charTest(char x, char y) {
   // Arithmetic operators:
   x = (char)(x * y);
   x = (char)(x / y);
   x = (char)(x % y);
   x = (char)(x + y);
   x = (char)(x - y);
   x++;
   x--;
   x = (char)+y;
   x = (char)-y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   x= (char)~y;
   x = (char)(x & y);
   x  = (char)(x | y);
   x = (char)(x ^ y);
   x = (char)(x << 1);
   x = (char)(x >> 1);
   x = (char)(x >>> 1);
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   x <<= 1;
   x >>= 1;
   x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   byte B = (byte)x;
   short s = (short)x;
   int i = (int)x;
   long l = (long)x;
   float f = (float)x;
   double d = (double)x;
 }
 static void byteTest(byte x, byte y) {
   // Arithmetic operators:
   x = (byte)(x* y);
   x = (byte)(x / y);
   x = (byte)(x % y);
   x = (byte)(x + y);
   x = (byte)(x - y);
   x++;
   x--;
   x = (byte)+ y;
   x = (byte)- y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   x = (byte)~y;
   x = (byte)(x & y);
   x = (byte)(x | y);
   x = (byte)(x ^ y);
   x = (byte)(x << 1);
   x = (byte)(x >> 1);
   x = (byte)(x >>> 1);
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   x <<= 1;
   x >>= 1;
   x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   short s = (short)x;
   int i = (int)x;
   long l = (long)x;
   float f = (float)x;
   double d = (double)x;
 }
 static void shortTest(short x, short y) {
   // Arithmetic operators:
   x = (short)(x * y);
   x = (short)(x / y);
   x = (short)(x % y);
   x = (short)(x + y);
   x = (short)(x - y);
   x++;
   x--;
   x = (short)+y;
   x = (short)-y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   x = (short)~y;
   x = (short)(x & y);
   x = (short)(x | y);
   x = (short)(x ^ y);
   x = (short)(x << 1);
   x = (short)(x >> 1);
   x = (short)(x >>> 1);
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   x <<= 1;
   x >>= 1;
   x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   byte B = (byte)x;
   int i = (int)x;
   long l = (long)x;
   float f = (float)x;
   double d = (double)x;
 }
 static void intTest(int x, int y) {
   // Arithmetic operators:
   x = x * y;
   x = x / y;
   x = x % y;
   x = x + y;
   x = x - y;
   x++;
   x--;
   x = +y;
   x = -y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   x = ~y;
   x = x & y;
   x = x | y;
   x = x ^ y;
   x = x << 1;
   x = x >> 1;
   x = x >>> 1;
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   x <<= 1;
   x >>= 1;
   x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   byte B = (byte)x;
   short s = (short)x;
   long l = (long)x;
   float f = (float)x;
   double d = (double)x;
 }
 static void longTest(long x, long y) {
   // Arithmetic operators:
   x = x * y;
   x = x / y;
   x = x % y;
   x = x + y;
   x = x - y;
   x++;
   x--;
   x = +y;
   x = -y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   x = ~y;
   x = x & y;
   x = x | y;
   x = x ^ y;
   x = x << 1;
   x = x >> 1;
   x = x >>> 1;
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   x <<= 1;
   x >>= 1;
   x >>>= 1;
   x &= y;
   x ^= y;
   x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   byte B = (byte)x;
   short s = (short)x;
   int i = (int)x;
   float f = (float)x;
   double d = (double)x;
 }
 static void floatTest(float x, float y) {
   // Arithmetic operators:
   x = x * y;
   x = x / y;
   x = x % y;
   x = x + y;
   x = x - y;
   x++;
   x--;
   x = +y;
   x = -y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   //! x = ~y;
   //! x = x & y;
   //! x = x | y;
   //! x = x ^ y;
   //! x = x << 1;
   //! x = x >> 1;
   //! x = x >>> 1;
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   //! x <<= 1;
   //! x >>= 1;
   //! x >>>= 1;
   //! x &= y;
   //! x ^= y;
   //! x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   byte B = (byte)x;
   short s = (short)x;
   int i = (int)x;
   long l = (long)x;
   double d = (double)x;
 }
 static void doubleTest(double x, double y) {
   // Arithmetic operators:
   x = x * y;
   x = x / y;
   x = x % y;
   x = x + y;
   x = x - y;
   x++;
   x--;
   x = +y;
   x = -y;
   // Relational and logical:
   f(x > y);
   f(x >= y);
   f(x < y);
   f(x <= y);
   f(x == y);
   f(x != y);
   //! f(!x);
   //! f(x && y);
   //! f(x || y);
   // Bitwise operators:
   //! x = ~y;
   //! x = x & y;
   //! x = x | y;
   //! x = x ^ y;
   //! x = x << 1;
   //! x = x >> 1;
   //! x = x >>> 1;
   // Compound assignment:
   x += y;
   x -= y;
   x *= y;
   x /= y;
   x %= y;
   //! x <<= 1;
   //! x >>= 1;
   //! x >>>= 1;
   //! x &= y;
   //! x ^= y;
   //! x |= y;
   // Casting:
   //! boolean b = (boolean)x;
   char c = (char)x;
   byte B = (byte)x;
   short s = (short)x;
   int i = (int)x;
   long l = (long)x;
   float f = (float)x;
 }

}</source>





The op= Operators

count += 5 has the same effect as the statement: count = count + 5;



   <source lang="java">

public class MainClass {

 public static void main(String[] arg) {
   int count = 1;
   count += 5;
   System.out.println(count);
   count = count + 5;
   System.out.println(count);
 }

}</source>



6
11

The complete set of op= operators:

  1. +=
  2. -=
  3. *=
  4. /=
  5. %=
  6. <<=
  7. >>=
  8. >>>=
  9. &=
  10. |=
  11. ^=


The ternary operator (The Conditional Operator): result = value>conditionValue ? result1 : result2

   <source lang="java">

if(value > conditionValue){

 result = result1;

}else{

 result = result2;

}

logical_expression ? expression1 : expression2</source>
   
  
 
  
A
B