Java Print Variables

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()


Output:

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 Code Meaning
%d Integer
%f Floating-point number
%.2f Floating number with 2 decimals
%s String
%c Character

๐Ÿง  Example Program


 


โญ Summary

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

CodeCapsule

Sanjit Sinha โ€” Web Developer | PHP โ€ข Laravel โ€ข CodeIgniter โ€ข MySQL โ€ข Bootstrap Founder, CodeCapsule โ€” Student projects & practical coding guides. Email: info@codecapsule.in โ€ข Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *