Assignment Operators

Assignment Operators

                                                             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.

/*

int a,b,c,d;
a=b=c=d=20;
System.out.print(a+" "+b+" "+c+" "+d);
Output-:20 20 20 20


int a=b=c=d=20;
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;

*/

int a,b,c,d;
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.

                                            

>>>= unsigned right shift operator
>>= Right shift operator
<<=Left Shift Operator

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 -:

/*

byte x=10;
x=x+1;
System.out.print(x);
Ans -: compile time error
 Max(int,byte,int) 
max=int
compile time error-:loss of precision found int but required byte 


byte x=10;
x++;
System.out.print(x);
Ans-: x++ is convert into   x=(byte)(x+1) type cast internally so output is 11


byte x=10;
x+=1;
System.out.print(x);
Ans-: x+=1 is convert into   x=(byte)(x+1) type cast internally so output is 11

*/

Example-:

/*
int a,b,c,d;
a=b=c=d=20;
a+=b-=c*=d/=2;
System.out.print(a+" "+b+" "+c+" "+d);

Output is-: a =-160,
b=-180
c=200
d=10

*/

                                                    Conditional Operator(?:)
The only possible ternary operator in java is the conditional operator.
 
Syntax-:
           (condition)?first statement:Second statement;
 if the condition is true will be executed first statement otherwise execute the Second value.
Example-:
/*
int x=(10<20)? 30:40;
Output is-: 30
*/
  • we can perform nesting of conditional operators also.
Example-:
/*
int x=(10>20)?30:((20>30)?40:50);
Output is-: 50
*/

                                                        new Operator

we can use new operator to create an object.
Example
Test t=new Test();
Note-:
  •  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.

                                                                []Operators

we can use this operator to declare and create arrays.
example-:
int[] arr=new int[10];

Previous Post Next Post

Most Recent