Operators

Precedence of Java Operators
 
 
 
[ ] ( )      
++ -- ! ~  
* / %    
+ - << >> >>>
< > <= >=  
= = !=      
&        
^        
|        
&&        
||        
?:        
+=    

 

 Associativity of Java Operators

almost all - from left to right
=  += ...     from right to left

Example:

z = p * r % q + w / x - y          ==>    z =((((p * r) % q) + (w / x)) - y)
   6    1    2     4    3    5

z = a + b + k * (c = d = 0)         ==>   z = ((a + b) +( k* ((c = (d = 0)) )))
   6    1    5    4     3    2

Data Types:

There are two data types available in Java −

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.

byte

short

int

long

float

double

boolean

char

The type of the result of the operation is of the type of operands with one exception - if the type of the result must be byte or short it is converted to int

If the operands  are of a different type, a preliminary conversion of the type of operands is automatically performed in such a way that there is no loss of information. The arrows in the figure below shows the possible conversions.  The arrows with a dashed lines show possible loss of precision.

public class Oper {
    public static void main(String arg[]){
        int i1=4, i2 = 5;
        byte b=127,b1=1;
        double f1 =5.0;
        System.out.println("b:"+b+"\tb+b1:"+(b+b1)+"\t++b:"+ ++b);  // byte converted to int
        System.out.println("i1/i2:"+(i1/i2)+ "\ti1/f1:"+(i1/f1));               /* i1/i2 - without conversion - result 0
                                                                                                           i1/f1 - i1 is converted in double - result 0.8   */
    }
}


Explicit conversion (caste).

public class Oper {
    public static void main(String arg[]){
        int in;
        long ln=123L;
        double db = 34.5;
        in = ln;        //type mismatch
        in = db;        //type mismatch
        in = (int)ln;    //ok
        in = (int)db;    //ok
    }
}

Almost all operations can be used only with primitive types. Only the operations '=', '= =', and '! =' Can be used with all objects and the "String" class supports the operations '+' and '+ ='.

Featurs:

The '+' operator is used with String objects for concatenation. If an expression begins with a String object, all subsequent operands are converted to "String" (in recent versions, this conversion is performed even if the String object is not the first operand):

        int x = 0, y = 1, z = 2;
        String sString = "x, y, z ";
        System.out.println(sString + x + y + z);   //  x,y,z 012
        System.out.println(sString + (x + y + z));  //  x,y,z 3

  Examples:
public class Operators {
    public static void main(String[]arg){
        int in;           // casting
        long ln=123L;
        double db = 34.5;
        //in = ln;        //type mismatch
        //in = db;        //type mismatch
        in = (int)ln;    //ok
        in = (int)db;    //ok
        /*****************************************************/
        String s, s1="Pijo ", s2="loves ",s3= "apples ";   //concatenation
        s= s1+s2;
        System.out.println(s);
        s+=s3;                  // s=s+s3
        System.out.println(s);
        /*****************************************************/
        int i1=4, i2 = 5;      // division
        double f1 =5.0;
        System.out.println("1: "+i1/i2+"\t" +i1/f1);
        /*****************************************************/
        int k, m=3,z=4,p;      // assignment
        p = k =(m = 2) *(z = 4);
        System.out.println("2: "+k+"\t" + m+"\t" + z+"\t" + p+"\t");
        //(p = k) = 2;       //error:  (p = k) is not variable
        /*****************************************************/
        k=m=3;               //increment
        System.out.println("3: "+  ++k +"\t"+ m++);
        System.out.println("4: "+ k +"\t" + m);
        //k= (m++)++;    // error: m++  is not variable
        /*****************************************************/
        System.out.println("11%4:\t"+11%4);   // modulo
        System.out.println("23%6:\t"+23%6);
        System.out.println("-11%3:\t "+ -11%3);
        /*****************************************************/
        System.out.println("25>11:"+(25>11)+ "\t17<=8: "+(17<=8));  //relations
        System.out.println("25==11:"+(25==11)+ "\t 25!=11: "+(25!=11));
        /*****************************************************/
        boolean b1=true,b2=true,b3=false,b4=false;        // booleans
        System.out.println("true && true: "+(b1&&b2)+ "\t true && false: "+(b1&&b3));
        System.out.println("true || false: "+(b1||b3)+ "\t false || false: "+(b3 || b4));
        System.out.println("!true: "+!b1+"\t !false: "+!b3);
        /*****************************************************/
        int a=4, b=9;                                                   // ternary
        System.out.println("ternary:"+ (a>b?a:b)+ "\t"+ (a<b?a:b));      
    }
}