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:
๐ Printing Text + Variables Together
Use the + operator (concatenation):
Output:
๐ข Printing Multiple Variables in One Statement
Output:
๐ฏ Printing Numbers in Expressions
Java will perform mathematical operations first if parentheses are not used carefully.
Output:
๐งช Printing Variables Using print()
Output:
๐ Printing Variables Using printf() (Formatted Output)
Output:
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 |
