Skip to main content

Assignment Operators

3.1 Simple Assignment Operator (=)โ€‹

  • It is a binary operator.
  • It is used to assign the value to a variable.
<operand1> = <operand2>

Important Pointsโ€‹

  • Operand1 must be a variable.
  • Operand2 can be a variable, value, or expression.
  • The variable, value, or expression must be assignment compatible type.

Type Compatibility (Widening Conversion)โ€‹

byte โ†’ short โ†’ int โ†’ long โ†’ float โ†’ double
โ†‘
char

Lab98.javaโ€‹

class Lab98 {

public static void main(String[] args) {
10 = 29;
}
}

Lab99.javaโ€‹

class Lab99 {

public static void main(String[] args) {
int a = 123;
int b = 12 + 23;

System.out.println(a);
System.out.println(b);
}
}

Lab100.javaโ€‹

class Lab100 {

public static void main(String[] args) {
int a = 12 < 23;
int b = 123L;
long c = 123.45;
}
}

Lab101.javaโ€‹

class Lab101 {

public static void main(String[] args) {
boolean b = 12 < 23;
long a = 123L;
double d = 123.45;

System.out.println(b);
System.out.println(a);
System.out.println(d);
}
}

2. Type Castingโ€‹

<OPERAND1> = <OPERAND2>

Key Pointsโ€‹

  • Here OPERAND2 is the source, OPERAND1 is the destination, and we are assigning source to destination.
  • Usually, source type must be same as destination type.
  • When source type is not same as destination type, then source type must be converted to destination type.
  • This process of converting one type to another is called Type Casting.

Types of Type Castingโ€‹

  1. Implicit Casting
  2. Explicit Casting

1) Implicit Castingโ€‹

  • When type casting happens automatically by the compiler, it is called Implicit Casting.

2) Explicit Castingโ€‹

  • When type casting is done manually by the programmer, it is called Explicit Casting.

Syntaxโ€‹

<destType><destVar>= (<destType>)<sourceValue>;

Exampleโ€‹

int a=10;
byte b= (byte)a;

Types of Conversionsโ€‹

  • There are two types of conversions:
    • Widening
    • Narrowing

Widening (Safe Conversion)โ€‹

  • Widening is the process of converting lower type to higher type.
  • This is a safe conversion.

Exampleโ€‹

byte b = 10;

int a = b; // VALID โ€“ Implicit Casting

int a = (int) b; // VALID โ€“ Explicit Casting
  • Here you are casting byte to int.
  • byte range: 128 to 127
  • int range: 2147483648 to 2147483647
  • Any value in byte will fit into int.
  • Hence, no data loss occurs, so it is considered safe.

Narrowing (Unsafe Conversion)โ€‹

  • Narrowing is the process of converting higher type to lower type.

Exampleโ€‹

int a=300;

byte b=a;// INVALID โ€“ Implicit Casting
byte b= (byte)a;// VALID โ€“ Explicit Casting
  • Here you are casting int to byte.
  • Range of byte is much smaller than int.

What happens when converting 300 to byte?โ€‹

  • Result becomes 44
  • Data is lost โ†’ hence conversion is unsafe

Binary Representation Explanationโ€‹

int a = 300; // int uses 32 bits

Binary (32-bit representation):

00000000 00000000 00000001 00101100
byte b1= (byte)a;// byte uses 8 bits

Only last 8 bits are considered:

00101100

Value Calculationโ€‹

0 ร— 2โท + 0 ร— 2โถ + 1 ร— 2โต + 0 ร— 2โด + 1 ร— 2ยณ + 1 ร— 2ยฒ + 0 ร— 2ยน + 0 ร— 2โฐ
= 32 + 8 + 4 = 44

Another Exampleโ€‹

double val=300.9898;
byte b2= (byte)val;
  • Result:
b2 = 44

Lab102.javaโ€‹

class Lab102 {

public static void main(String[] args) {
byte b = 10;
short s = b;
int a = s;
long x = a;
float f = x;
double d = f;

System.out.println(b);
System.out.println(s);
System.out.println(a);
System.out.println(x);
System.out.println(f);
System.out.println(d);
}
}

Lab103.javaโ€‹

class Lab103 {

public static void main(String[] args) {
int a = 65;
char ch = (char) a;
byte b = (byte) a;

System.out.println(a);
System.out.println(ch);
System.out.println(b);
}
}

Lab104.javaโ€‹

class Lab104 {

public static void main(String[] args) {
float f = 1234.567F;
int a = (int) f;

System.out.println(f);
System.out.println(a);
}
}

Lab105.javaโ€‹

class Lab105 {

public static void main(String[] args) {
int a = 300;
byte b = (byte) a;

System.out.println(a);
System.out.println(b);
}
}

Lab106.javaโ€‹

class Lab106 {

public static void main(String[] args) {
int a = -1;
char ch = (char) a;

System.out.println(a);
System.out.println(ch);

int b = ch;
System.out.println(b);
}
}

Lab107.javaโ€‹

class Lab107 {

public static void main(String[] args) {
byte b1 = 12;
byte b2 = 23;
byte b3 = b1 + b2;

System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}

Lab108.javaโ€‹

class Lab108 {

public static void main(String[] args) {
byte b1 = 12;
byte b2 = 23;
byte b3 = (byte) (b1 + b2);

System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}
}

Lab109.javaโ€‹

class Lab109 {

public static void main(String[] args) {
boolean b = 1;
String str = 99;
int x = "JavaWorld";
}
}

Lab110.javaโ€‹

class Lab110 {

public static void main(String[] args) {
boolean b = (boolean) 1;
String str = (String) 99;
int x = (int) "JavaWorld";
}
}

3.3. Compound Assignment Operatorsโ€‹

  • Compound assignment operators provide a shorter syntax for performing operations and assigning the result of an arithmetic operator or bitwise operator.

Example:

int a=90;
a+=9;// => a = a + 9;
  • In the above expression two tasks are happening:
    1. Performs the Arithmetic operation on two operands.
    2. Assigns the result to the first operand.
  • There are 11 compound assignment operators as shown below:
OperatorDescription
+=Add right operand to left operand and assign the result to left operand.
-=Subtract right operand from left operand and assign the result to left operand.
*=Multiply right operand to left operand and assign the result to left operand.
/=Divide right operand to left operand and assign the result to left operand.
%=Calculate modulus using two operands and assign the result to left operand.
&=Assigns the result of the Bitwise AND.
&#124;=Assigns the result of the Bitwise OR.
^=Assigns the result of the Bitwise XOR.
<<=Assigns the result of the signed left shift operator.
>>=Assigns the result of the signed right shift operator.
>>>=Assigns the result of the unsigned right shift operator.

Lab111.javaโ€‹

class Lab111 {

public static void main(String[] args) {
int a = 90;
System.out.println(a);
a += 9;
System.out.println(a);
}
}

Lab112.javaโ€‹

class Lab112 {

public static void main(String[] args) {
byte b = 90;
b = b + 9;
System.out.println(b);
}
}

Lab113.javaโ€‹

class Lab113 {

public static void main(String[] args) {
byte b = 90;
b += 9;
System.out.println(b);
}
}

Lab114.javaโ€‹

class Lab114 {

public static void main(String[] args) {
int a = 10;
a *= 3 + 4;
System.out.println(a);
}
}

Lab115.javaโ€‹

class Lab115 {

public static void main(String[] args) {
int a = 10;
a = a * 3 + 4;
System.out.println(a);
}
}

Lab116.javaโ€‹

class Lab116 {

public static void main(String[] args) {
int a = 10;
a = a * (3 + 4);
System.out.println(a);
}
}