In This Article, You Will Learn about Java Output.
Before moving ahead, let’s know a bit about Java Syntax.
Table of Contents
Java Output
Let’s recap first the last lesson where we got the program’s output called “Hello World.”
public class Main{
public static void main(String[] args){
System.out.println("Hello World");
}
}
There is no restriction in Java to print output; you can print output as many as you want by using the println() method. Each println() method will add a new line for printing output.
Example: Add the multiple println() method to print more than one line’s output.
public class Main{
public static void main(String[] args){
System.out.println("Hello World");
System.out.println("Hello Java");
System.out.println("I am learning programming language");
System.out.println("I love programming");
}
}
Java allows us to perform various kinds of action with println() method.
Example: Perform the mathematical operation.
public class Main{
public static void main(String[] args){
System.out.println(1+1);
System.out.println(10/2);
System.out.println(25-5);
}
}
Note: Don’t use quotes (“”) inside the println() method to print numbers. Later in the upcoming lesson, you will learn about using quotations in Java.
The Print() Method
In Java, there is also a print() method like you have seen in Python.
The print() method is as same as the println() method, but the only difference is that it does not add a new line at the end of each line, i.e., the output for each line will be printed together.
Example: Use the print() method to print the output.
public class Main{
public static void main(String[] args){
System.out.print("Hello World ");
System.out.print("I am excited to learn Java ");
System.out.print("because I love ");
System.out.print("programming language very much.");
}
}
Note: We added an extra space (after “Hello World” above) to better understand each line.
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: