Assignment Operator

The assignment operator is the single equal sign, =. It has this general form:


var = expression;

type of var must be compatible with the type of expression. The assignment operator allows you to create a chain of assignments.

 
public class Main {
  public static void main(String[] argv) {
    int x, y, z;
    x = y = z = 100; // set x, y, and z to 100
    System.out.println("x is " + x);
    System.out.println("y is " + y);
    System.out.println("z is " + z);
  }
}

The output:


x is 100
y is 100
z is 100
Home 
  Java Book 
    Language Basics  

Variables:
  1. Variable stores value in a Java program
  2. Assignment Operator
  3. Dynamic Initialization
  4. Scope and Lifetime of Variables