Blog

Concept of println Method

Java / Java Tutorials

Concept of println Method

Concept of println Method

→  Suppose we write the following code:

class Test
{
 public static void main(String [ ] args)
 {
  System.out.println(“Hello User”);
  System.out.println(“Welcome To Java”);
 }
}

→ Now let’s understand the overview of writing System.out.println().which a beginner can easily understand by the words meaning that it will print a message on the console.
     Syntax: System.out.println(“ Hello World ”);
System is a predefined class .
out is an object reference (not object).
println( ) is a method.
→ Together all three are used for displaying text on console.
→ Now when we will run it by typing on CMD or Terminal :java Test
→ The output will be:

Hello User
Welcome To Java

Did you notice something ?
The line “Welcome To Java” automatically got displayed on second line . Why?
Because the method println( ) implicitly adds a new line at the end after displaying the message
→ Concept 1:In case we do not want the newline effect then we can use another method called print( ).

So if we write:
System.out.print(“Hello User”);
System.out.print(“Welcome To Java”);
Then the output would be:
Hello UserWelcome To Java

→ Concept 2: What would be the output of the following code:
System.out.print(“Hello User”);
System.out.println(“Welcome To Java”);
Output:

Hello UserWelcome To Java
This is because the method println( ) method puts a newline after the message not before it.

→ Concept 3: What would be the output of the following code:

System.out.println(“Hello User”);
System.out.print(“Welcome To Java”);

Output:

Hello User
Welcome To Java

→ Concept 4: What would be the output of the following code:

System.out.print (“Hello User”);
System.out.println( );
System.out.print (“Welcome To Java”);

Output:

Hello User
Welcome To Java

This is because if we call the method println( ) without
arguments it just generates a newline on console.
→ Concept 5: What would be the output of the following code:

System.out.print (“Hello User”);
System.out.print( );
System.out.print (“Welcome To Java”);

Output:

Syntax Error!

This is because the method print( ) always requires
arguments
, so we cannot call it without
arguments.
For more Concept building program Click this → Java Programming Examples

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare