Java Output
In Java, output is displayed to the console using the System.out.print
methods. These methods allow you to print text, numbers, and other data types.
Key Topics
1. Print Text
You can print text using System.out.println()
or System.out.print()
methods by enclosing the text within double quotes.
public class PrintText {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
2. Print Numbers
You can print numbers directly or through variables.
public class PrintNumbers {
public static void main(String[] args) {
System.out.println(100);
int number = 200;
System.out.println(number);
}
}
Output:
100
200
200
3. println() vs print()
The println()
method prints the output and moves the cursor to a new line, while the print()
method prints the output without advancing to a new line.
public class PrintDemo {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.print("World!");
System.out.println(" ");
System.out.println("This is on a new line.");
}
}
Output:
Hello, World!
This is on a new line.
This is on a new line.
Key Takeaways
- Use
System.out.println()
to print with a newline. - Use
System.out.print()
to print without a newline. - You can print text, numbers, and variables using these methods.