Java Print Variables

Java Tutorial

Java Print Variables

In Java, you can print variables using different methods like print(), println(), and printf(). Printing variables helps display stored values on the screen.


Printing Variables Using println()

21

 Printing Text + Variables Together

Use the + operator (concatenation):


 

Output:

Name: Rahul

Age: 20


 Printing Multiple Variables in One Statement


 

Output:

Vipul is 21 years old and lives in Surat.

Printing Numbers in Expressions

Java will perform mathematical operations first if parentheses are not used carefully.


 

Output:

12

Sum: 57


Sum: 12


Printing Variables Using print()

Output:

Current year: 2025

Printing Variables Using printf() (Formatted Output)


 

Output:

Name: Aman, Age: 22, Marks: 88.76

Common Format Specifiers:

Format CodeMeaning
%dInteger
%fFloating-point number
%.2fFloating number with 2 decimals
%sString
%cCharacter

 Example Program


 


 Summary

MethodUse
println()Prints variable + moves to new line
print()Prints without new line
printf()Prints formatted values

You may also like...