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