Skip to main content

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];

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...