最适合网络开发者的网站
Java 编程语言。初学者课程

尿素

Java 声明多个变量


Declare Many Variables

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

例子

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);
Try it Yourself »

One Value to Multiple Variables

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

例子

int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
Try it Yourself »

Test Yourself With Exercises

Exercise:

Fill in the missing parts to create three variables of the same type, using a comma-separated list:

 x = 5 y = 6 z = 50;