In java, we use System.out.println() statement to display a message, string or data on the screen. It displays the argument that we pass to it.
Let’s understand each part of this statement:
- System: It is a final class defined in the java.lang package.
- out: It is an instance of PrintStream type and its access specifiers are public and final
- println(): It is a method of PrintStream class.
As we know that we need to create the object of a class to invoke its method. Here instead of creating the object of the PrintStream class we use System.out, it internally creates the object of PrintStream class so we can use the statement System.out.println() to invoke println() method.
Example 1: Displaying Strings using System.out.println()
Here we are displaying three Strings using System.out.println() statements.
class JavaExample { public static void main(String[] args) { System.out.println("Welcome"); System.out.println("To"); System.out.println("Beginnersbook"); } }
Output: Since we are using println() method, the strings are displayed in new lines, if there is a need to display the strings in a single line, use print() method instead.
Welcome To Beginnersbook
Example 2: Displaying the variable values using println() method
In the above example, we simply passed the strings that we wanted to be displayed as output. However, we can do so much more with the println() method. We can pass the variable name in this method and it can read and display the value of the passed variable as output.
class JavaExample { public static void main(String[] args) { int a = 10, b = 20; System.out.print("The product of "); System.out.println(a + " and " + b + " is: " + a*b); } }
Output:
The product of 10 and 20 is: 200
Overloading of println() method
We learned in Overloading in Java that we can have more than one methods with the same name but different signatures.
The println() method belongs to the PrintStream class and this class has total 10 overloaded variants of println() method. The overloaded methods are invoked based on the arguments passed by the user.
Some of the overloaded methods are as follows. If user passes int to the println() method, it invokes the overloaded method with int parameter and if the passed parameter is of String type, it invokes String variant and so on.
System.out.println() System.out.println(int) System.out.println(double) System.out.println(boolean) System.out.println(String) System.out.println(char)
Let’s take an example:
class JavaExample { public static void main(String[] args) { int num = 10; char ch = 'A'; boolean bool = false; String s = "Beginnersbook"; System.out.println(num); System.out.println(ch); System.out.println(bool); System.out.println(s); } }
Output:
10 A false Beginnersbook
Difference between print() and println() methods
System.out.print(): This method displays the result on the screen and the cursor remains at the end of the the printed line. The next printing starts in the same line where the cursor is at. This method will only work when there is at least one parameter passed to it else it will throw an error.
System.out.println(): Like print() method, this method also displays the result on the screen based on the parameter passed to it. However unlike print() method, in this method the cursor jumps to the next line after displaying the result, which means the next printing starts in the new line. This method doesn’t throw an error if no parameters are passed in it.
Let’s take an example to see this in action.
class JavaExample { public static void main(String[] args) { System.out.println("print() statements:"); System.out.print("This "); System.out.print("is "); System.out.print("just "); System.out.print("a "); System.out.print("String "); System.out.println();//for new line System.out.println("println() statements:"); System.out.println("This "); System.out.println("is "); System.out.println("just "); System.out.println("a "); System.out.println("String "); } }
Output:
print() statements: This is just a String println() statements: This is just a StringLearn Java Programming: Java Tutorial
Leave a Reply