Java Print Variables - Using println() and Concatenation
Learn how to print variables in Java using the println()
method. Combine text with variables efficiently using the +
operator for output in your Java programs.
Java Print Variables
The println()
method in Java is used to display variables.
To combine text and a variable, use the +
character:
Example:
String name = "John";
System.out.println("Hello " + name);
Syntax
String name = "John";
System.out.println("Hello " + name);
Output
Hello John
You can also concatenate variables together using the +
operator:
Example:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);
Syntax
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);
Output
John Doe
For numeric values, the +
character functions as a mathematical operator:
Example:
int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y
Syntax
int x = 5;
int y = 6;
System.out.println(x + y);
Output
11