Saturday, March 19, 2022

Java Declare Multiple Variables

 

Declare Many Variables

To declare more than one variable of the same type, you can use a comma-separated list:

Example

Instead of writing:

int a =5;

int b= 6;

int c=7;

System.out.println(a+b+c);

You can simply write:

int a =5, b= 6,c=7;

System.out.println(a+b+c);

Example

public class Main {

  public static void main(String[] args) {

    int a=5,b=6,c=7;

    System.out.println(a+b+c);

  }

}

One Value to Multiple Variables

You can also assign the same value to multiple variables in one line:

Example

int a, b, c;

a= b = c = 25;

System.out.println(a + b + c);


No comments:

Post a Comment

Did you find this article helpful?
😁 😀

Java identifers

  Identifiers All Java variables must be identified with unique names . For example -different people have different names These unique ...