Java provides several methods to display output:
1. System.out.print()
Prints text without moving to the next line.
java
System.out.print("Hello, ");
System.out.print("World!");
Output: Hello, World!
2. System.out.println()
Prints text and moves to the next line.
java
System.out.println("Hello, World!");
System.out.println("Java is fun!");
Output:
kotlin
Hello, World!
Java is fun!
3. System.out.printf()
Formats and prints output using placeholders.
java
System.out.printf("Name: %s, Age: %d\n", "Alice", 25);
Output: Name: Alice, Age: 25
4. Printing Variables
java
int number = 42;
System.out.println("Number: " + number);
Output: Number: 42
Key Notes:
print()
: No newline.println()
: Adds a newline.printf()
: For formatted output.- Escape characters like
\n
(new line) and\t
(tab) enhance formatting.
This is how you print effectively in Java!