Java assignment operator

Assignment Operator in Java :

  1. “=” is simple assignment operator in Java Programming.
  2. “Assignment Operator” is binary Operator as it operates on two operands.
  3. It have two values associated with it i.e Left Value and Right Value.
  4. It is used to Assign Literal Value to the Variable.

Variable May be -

  • Variable of Primitive Data Type
  • Array Variable
  • Object

Syntax and Example of Assignment Operator

int javaProgramming;  //Default Assignment 0
 int distance = 10;    //Assign 10
 int price = 1990;     //Assign 1990

Ways of Assignment :

Way 1 : Declare and Assign

class AssignmentDemo {
    public static void main (String[] args){
        int ivar;
        ivar = 10;
        System.out.println(ivar);
    }
}
  • Firstly we are going to declare variable of primitive data type.
  • After declaring variable will have default value inside it (i.e 0).
  • 10 is assigned to Variable “ivar“.

Way 2 : Declare and Assign in Single Statement

class AssignmentDemo {
    public static void main (String[] args){
        int ivar = 10;
        System.out.println(ivar);
    }
}
[468x60]

Way 3 : Assigning Value to Object

class AssignmentObjectDemo {
    public static void main (String[] args){
        String s1 = "Pritesh";
        System.out.println(s1);
    }
}
[468x60]
  • S1 is object of String and we are going to assign String “Pritesh” to Object s1.

Way 4 : Assignment & Arithmetic Operation

class AssignmentAndArithmetic {
    public static void main (String[] args){
        int num1 = 10;
        int num2 = 20;
        int num3 = num1 + num2;
        System.out.println(num1);
    }
}