JAVA VARIABLES
Declaring multiple Java variables
When several variables share a data type, Java lets you declare them in one statement. Names and initial values are separated by commas, and the entire declaration ends with one semicolon.
Separate declarations
You can declare each variable on a separate line:
int jonasTaskai = 5;
int onaTaskai = 6;
int rutaTaskai = 50;
System.out.println(jonasTaskai + onaTaskai + rutaTaskai);61
- The first line creates a number box named
jonasTaskaiand puts the number inside it:5. - On the second line,
onaTaskaireceives6, and on the third line,rutaTaskaireceives50. - On the last line, the
+operators add the contents of all three boxes:5 + 6 + 50 = 61. - The method
println()displays the calculated number 61 in the console.
Declaring on one line
The same variables of the same type can be written more briefly:
int jonasTaskai = 5, onaTaskai = 6, rutaTaskai = 50;
System.out.println(jonasTaskai + onaTaskai + rutaTaskai);61
- Java sees the type
intand knows that all variables listed on this line will store integers. - Commas separate three different boxes: Jonas’s, Ona’s and Rūta’s points.
- Although the declaration takes only one line, each variable still has its own value.
println()adds5 + 6 + 50, so the console again displays 61.- One semicolon ends the entire combined declaration.
Only variables of the same type can be combined this way. int and String variables would need separate declarations.
Declaring without initial values
You can first declare several variables and assign different values to them later:
int jonasAmzius, onaAmzius, rutaAmzius;
jonasAmzius = 10;
onaAmzius = 11;
rutaAmzius = 12;
System.out.println(jonasAmzius + ", " + onaAmzius + ", " + rutaAmzius);10, 11, 12
- The first line creates three
intboxes, but they do not yet hold the ages we want to assign. - The next three lines assign each person an age: Jonas
10, Ona11, Rūta12. - On the last line, the numbers are joined with the text
", ". This time, the comma and space are simply characters displayed in the console. - Java therefore displays all three values in a clear sequence: 10, 11, 12.
println() or used in a calculation.
One value for multiple variables
You can assign the same value to several variables of compatible types in one chain:
int jonasStartas, onaStartas, rutaStartas;
jonasStartas = onaStartas = rutaStartas = 50;
System.out.println(jonasStartas + onaStartas + rutaStartas);150
- The first line prepares three boxes for the starting points of Jonas, Ona and Rūta.
- Java processes an assignment chain from right to left: first,
50receivesrutaStartas. - The same value is passed to
onaStartas, then tojonasStartas. Each now has50points. - On the last line, Java calculates
50 + 50 + 50 = 150. println()displays the total 150.
Shorter code or clearer code?
Declaring multiple variables on one line is concise, but it is not always easiest to read. Choose based on how the variables are related and how long the declaration is.
- One line works well for a few closely related variables with short names.
- Use separate lines when names or initial expressions are long.
- Separate declarations make it easier to add a comment to a particular variable.
- What matters most is consistent code that the team can easily understand.
Next, learn where declared variables are accessible and when they go out of scope: Java local variables.