Overloads of println() method
As we know, Method Overloading in Java allows different methods to have the same name, but different signatures or parameters where each signature can differ by the number of input parameters or type of input parameters or both. From the use of println() we observed that it is a single method of PrintStream class that allows the users to print various types of elements by accepting different type and number of parameters.
For example:
System.out.println(),
System.out.println(int),
System.out.println(double),
System.out.println(string),
System.out.println(character),
etc.
PrintStream has around 10 different overloads of println() method that are invoked based on the type of parameters passed by the user.
Example:
Java
// Java code to illustrate method // overloading in println() import java.io.*; // Driver Class class PrintLN { // main function public static void main(String[] args) { // Declaring different datatypes int num = 10 ; char ch = 'G' ; String str = "w3wiki" ; double d = 10.2 ; float f = 13 .5f; boolean bool = true ; // Various overloads of println() method System.out.println(); System.out.println(num); System.out.println(ch); System.out.println(str); System.out.println(d); System.out.println(f); System.out.println(bool); System.out.println( "Hello" ); } } |
10 G w3wiki 10.2 13.5 true Hello
System.out.println in Java
Java System.out.println() is used to print an argument that is passed to it.
Parts of System.out.println()
The statement can be broken into 3 parts which can be understood separately:
- System: It is a final class defined in the java.lang package.
- out: This is an instance of PrintStream type, which is a public and static member field of the System class.
- println(): As all instances of the PrintStream class have a public method println(), we can invoke the same on out as well. This is an upgraded version of print(). It prints any argument passed to it and adds a new line to the output. We can assume that System.out represents the Standard Output Stream.
Syntax:
System.out.println(parameter)
Parameters: The parameter might be anything that the user wishes to print on the output screen.
Contact Us