Generates newline(\n) in Java


In this topic, we will understand how to generates newline in Java?
Let's understand with an example:-
What is the output of the above code?


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.
If we don't want the newline effect then we can use another method
called 
print( ), so we write:-
Output:-

Important questions:-
1. What would be the output of the following code?
Output:-
        
Hello User
        Welcome To Java

Explanation:-
This is because if we call the method println( ) without arguments it just generates a new line on console.

2. What would be the output of the following code?
Output:-
        
Syntax Error!
Explanation:-
This is because the method print( ) always requires arguments, so we can not call it without arguments.


3. What would be the output of the following code?
Output:-
        Hello
        Naman     Tiwari

Explanation:-
This is because Java also support escape sequences like \n,\t,\r etc.

Post a Comment

0 Comments