Java Variables

Multiple Variables

Declare Many Variables

If you want to declare many variables of the same type, you can use a list separated by commas:

Example

Instead of writing:

				
					int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);
				
			

You can simply write:

				
					int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
				
			

One Value to Multiple Variables

Additionally, you can set the same value for several variables in a single line:

Example

				
					int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
				
			
Share this Doc

Multiple Variables

Or copy link

Explore Topic