User output in java with examples

Syntax:-

System.out.println("statement");

or

System.out.print("statement");

println and print are used to display statement inside() braces.

System.out is used because we using computers for displaying messages.

Similarly System.in used for getting data from keyboard.

Difference between prinln and print:-

println used to display statement inside () braces and Cursor is moved to new line.
but in print cursor does not move to new line.

see example to get idea

class disp{
public static void main(String arg[]){
System.out.println("hey");
System.out.println("How are you");
}
}

Output
hey
How are you

Here How are you is moved to new line because previous println is used display a message .

class group{
public static void main(String arg[]){
System.out.print("hey");
System.out.println("How are you");
}
}

output
heyHow are you

Here hey and How are you is displayed in same line because previous print is used to display a message.

class aboutcode{
public static void main(String arg[]){
System.out.print("hey");
System.out.println("How are you");
System.out.print("I am fine");
}
}

Output
heyHow are you
I am fine


Display Variable Value

Syntax:-

System.out.print(variable_name);
or
System.out.println(variable_name);

Here displaying only variable value.

Example:-
class numberdisp{
public static void main(String arg[]){
int num;
num = 10;
System.out.println(num);
System.out.println(num+1);
System.out.print(num);
}
}

output
10
11
10

syntax for displaying variable value along with statement.

Syntax:-

for print
System.out.print("statement"+variable_name);

System.out.print(variable_name+"statement");

System.out.print("statement"+variable_name+"statement");

for println
System.out.println("statement"+variable_name);

System.out.println(variable_name+"statement");

System.out.println("statement"+variable_name+"statement"));
 
+variable_name is used when variable is to display after statement
variable_name+ is used when variable is to display before statement
+variable_name+ is used when variable is to display between statement


Example:-

class group{

public static void main(String arg[]){

int num1, sum=0,num2;

num1 = 10;
num2= 5;
sum=num1+num2;

System.out.println("The value of sum is:"+sum);

System.out.println("The value of num is:"+num1+1);

System.out.println("The value of num is:"+2);

System.out.print(num1+" is added to " +num2+ " Than sum is: "+sum);

}
}

Output:-
The value of sum is:15
The value of num is:101
The value of num is:2
10 is added to 5 Than sum is: 15

Note:-
# In variable_name place we can use constant value.
# System is not equal to system, S much be in capital latter.
# Statement much be be inside "".
# Do not forget to write semicolon (;) end of the print statement.


Previous Code:-
Comment Line

List Code:-
Java codes

Next Code:-
User Input

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top