Print in java | System.out.println in java

How to print in java :-

 We use System.out.println() in java to print the statements on to the console. Lets not worry about the System.out for now will explain it later. 

There are three methods used to print something on to the console. They are

  • print()
  • println()
  • printf()
System.out.println in java
Print in java



Lets discuss the 3 listed one by one :-

print() :- 

This method is used to print some text on to the console in java. print() method is the overloaded method of the printStream class. When the string or text is printed on to the console the cursor remains on the same line which means that if we print another text than the text will print on the same line of the previos print.

Example :- System.out.print("Hello World!");

println() :-

This method is an enhanced version of print() method which is also used to print the some text on to the console and it is a overloaded method of the printStream class. When the string or text is displayed on to the screen than the cursor moves on to the next line which means that if we print another text than the text will be displayed on the next line.

Example :- System.out.println("Hello World!");

The major difference between the print() method and println() method is that the print() method retains the cursor on the same line after printing printing something but the println() method will move the cursor on to the next line after the println() statement.

printf() :-

This method is used if we want to print the formatted string on to the console. printf() method is the overloaded method of the printStream class.

There is a problem with these methods that is, before using it we have to create an object of printStream class then these methods can be used.

PrintStream p = new PrintStream(); 
p.println();
It is an invalid way of calling method.

Instead java provides an alternative way of creating an instance of class  PrintStream class which is System.out that is Standart Output Stream.

System.out.println();

Where, 
System:- which is a class that belongs to built in library package of java.lang.package.
out:- out is the instance of an System class of type PrintStream which is present in java.io.PrintStream.
println():- This is the method of PrintStream class which is used to print text on to the console.

Program to print Hello world in java :- 


 
public class Main
{
	public static void main(String[] args) {
	    
		System.out.println("Hello World!");
		
	}
}

Output:-
Hello World
Another Example :-

public class Main
{
	public static void main(String[] args) {
	    
		System.out.println("Welcome to Learnjavamars");
        System.out.println("Initialize variables next");
		
		// decalaring few variables
		int a = 100;
		int b = 45;
		float c = 33.33f;
		
		// printing the values of the variables
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
        
        System.out.println("End of program");
		
	}
}

Output:-
Welcome to Learnjavamars
Initialize variables next
100
45
33.33
End of program

Comments

Popular posts from this blog

Data types in java | java tutorial for beginners

Variables in java

BASIC STRUCTURE OF A JAVA PROGRAM