Java Print Variables
Display Variables
The println() method is often used to display variables.
To combine both text and a variable, use the + character:
String name = "John";
System.out.println("Hello " + name);You can also use the + character to add a variable to another variable:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):
int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + yFrom the example above, you can expect:
xstores the value5ystores the value6- Then we use the
println()method to display the value ofx + y, which is11.