wellnwill Marathahalli Bangalore wellnwill Marathahalli Bangalore

Assignment operator

  • Assignment operator is used for assigning a value to a variable.
  • There are three types of assignment operator --
    1. Simple Assignment
    2. Chained Assignment
    3. Compound Assignment

Simple Assignment

Example - int x = 10;

class Test56 {
  public static void main(String args[]) {
   int x =100;
   int y =x-20;
   System.out.println(x);
   System.out.println(y);
 }
}

// O/P - 100,80
class Test57 {
  public static void main(String args[]) {
    int x =100;
    int y =20;
    boolean b= x>y;
    System.out.println(b);
  }
}

// O/P - true
 class Test58 {
   public static void main(String args[]) {
    double x =100;
    float f=x;
    System.out.println(f);
  }
}

// O/P - CompileTimeException
       - incompatible type, possible lossy of conversion from double to float.

Chained Assignment --

Example - int x, y, z; x = y = z = 10;

class Test59 {
 public static void main(String args[]) {
   int x,y,z;
   x=y=z=10;
   System.out.println(x);
   System.out.println(y);
   System.out.println(z);
 }
}

// O/P - 10,10,10

Compound Assignment --

Assignment operator mixed with other operator is known as compound assignment. Example - int a=10; a+=9; -> a=a+9;

Operator Name of Operator how to use Same as
+= Addition assignment a+=2; a=a+2;
-= Subtraction assignment a-=2; a=a-2;
*= Multiplication assignment a*=2; a=a*2;
/= Division assignment a/=2; a=a/2;
%= Remainder assignment a%=2; a=a%2;
/= Remainder assignment a%=2; a=a%2;
class Test60 {
  public static void main(String args[]) {
    int x=10;
    x+=20;
    System.out.println(x);
    x-=10;
    System.out.println(x);
    x*=10;
    System.out.println(x);
    x/=10;
    System.out.println(x);
 }
}

// O/P - 30,20,200,20
class Test64 {
  public static void main(String args[]) {
    int a,b,c;
    a=b=c=2;
    a+=b-=c*=2;    // a=a+b,b=b-c,c=c*2
    System.out.println(c);
    System.out.println(b);
    System.out.println(a);
 }
}

// O/P->  4,-2,0



For compound assignment, type casting will be performed internally by jvm.

class Test61 {
   public static void main(String args[]) {
     byte b=10;
     b=b+10;
     System.out.println(b);
   }
} 
//  O/P- CompileTimeError
- incompatible type, possible lossy conversion from int to byte.
class Test62 
{
  public static void main(String args[])
   {
    byte b=10;
    b++;  // implicit type casting //b=b+1;
   //byte b=(byte)(b+1)
  System.out.println(b);
 }
}O/P-11

  • Float and Double classes contain NaN constant for holding the undefined results.
  • (multiply), / (division), % (modulus) have same level for expression execution but from left to right.
  • + (add), - (subtract) have same level for expression execution but after * (multiply), / (division), % (modulus) operator.

class Test63 
{
  public static void main(String args[])
   {
    byte b=10;
    b+=1 ; //implicit type casting//b=b+1;
          //byte b=(byte)(b+1)
    System.out.println(b);
   }
}

// O/P -  11
Footer with Map

Top