Assignment Operators
There are three types of assignment operators.
1-: Simple assignment ( int x=10)
2-: Chained assignment (int a,b,c,d; a=b=c=d=e=f=10);
3-:Compund assignment (a+=10)
Chained Assignment
we can't perform chained assignments directly at the time of declaration.
/*
a=b=c=d=20;
System.out.print(a+" "+b+" "+c+" "+d);
System.out.print(a+" "+b+" "+c+" "+d);//compile time error
/*
compile time error1
cannot find symbol
Symbol: variable b
location class x;
compile time error2
cannot find symbol
Symbol: variable c
location class x;
compile time error3
cannot find symbol
Symbol: variable d
location class x;
*/
a=b=c=d=20;//is valid
*/
Compound Assignment Operator
Sometimes assignment operators mixed with some other operator such type of assignment operators are called compound operator
the following are all possible compound assignments in java.
Example-:
/*
int a=10;
a+=20;
System.out.print(a);//20
*/
in the case of compound assignments operator, internally typeCasting will be performed automatically.
Example -:
/*
x=x+1;
System.out.print(x);
x++;
System.out.print(x);
byte x=10;
x+=1;
System.out.print(x);
*/
Example-:
a=b=c=d=20;
a+=b-=c*=d/=2;
System.out.print(a+" "+b+" "+c+" "+d);
- we can perform nesting of conditional operators also.
- After creating an object constructor will be executed to perform the initialization of an object henc constructor is not for the creation of an object and it is for the initialization of an object.
- In java we have only new keywords but not delete keyword because the destruction of a useless object is the responsibility of the garbage collector.