by @kodeazy

Java How to assign multiple variables same value by not repeatedly adding value to each variable?

Home » java » Java How to assign multiple variables same value by not repeatedly adding value to each variable?
  • In this blog we will discuss on how to assign multiple variables same value in java with out assigning the value to each variable.
  • If suppose I have three integer variables as below.

    int x,y,z;
  • I want to give all the three variables same value.
  • Instead of assigning value to each variable we can create non-primitive data type for all the three variables and assign.
  • Below is the example syntax.

    Integer x1,x2,x3;
    x1=x2=x3=2;
  • Below is the sample program of initalizing multiple variables with same value.

    public class Main {
    public static void main(String[] args) {
    Integer x1,x2,x3;
    x1=x2=x3=2;
    System.out.println("x1:"+x1+" x2 :"+x2+" x3:"+x3);
    }
    }

    Output:

    x1:2 x2 :2 x3:2