Java Variables

Print Variables

Display Variables

Displaying variables is a common usage for the println() function.

Use the + symbol to merge text with a variable:

Example

				
					String name = "John";
System.out.println("Hello " + name);
				
			

Additionally, you can add a variable to another variable by using the + symbol:

Example

				
					String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);
				
			

The + symbol functions as a mathematical operator for numeric values (note that we are using int (integer) variables here):

Example

				
					int x = 5;
int y = 6;
System.out.println(x + y); // Print the value of x + y
				
			

From the above example, you may anticipate:

  • x stores the value 5
  • y stores the value 6
  • Then we use the println() method to display the value of x + y, which is 11
Share this Doc

Print Variables

Or copy link

Explore Topic