Printing Program Output in Java

(Last Updated On: February 23, 2019)

Learn how to print program output in java on console screen using system.out.println method. You will learn how to print Strings, int and double variables etc on console.

Here is a simple java program that prints output as “I love Java Programming”.

 

Output:
I Love Java Programming

Notice that string “I Love Java programming” is in double quote.

 

 

Printing Multiple Strings on Console

Here is another example of printing multiple strings in java program.

Output:
My name is Peter
I am learning Java
I like Coffee

 

Printing variables on Console

You can print value of int, double and String variables in java program on console screen.

Java code:

Output:
I Like Coffee
2
99.0

 

Printing multiple values in one line on Console

You can print multiple types of data by appending them using + symbol in system.out.println method in one line.

Java code example:

In below program an int variable and a string variable are appended in system.out.println method using + symbol.

Output:
100 Coffee!!!

 

WARNING:

Beginners make mistake of using common symbol (,) instead of (+ )symbol . For example, System.out.println(count,str);

 

One more example below on console output in java,

that is appending a hard code string “value = “ and an int value of variable num.

 

Output:
value= 100

NOTE: Having space before or after + symbol, does not affect the output. For example, in above program, both statements given below produces same output.

 

 

 

Exercises on Printing Output on Console in Java

 

Exercise 1: Print below 3 strings using a single system.out.printl method. No need to create String variables for them.

“I am going to be expert in”
“java programming”
“in few days”

 

Exercise 2: Use below program. Print the output as given below in one line. You don’t have to write any extra system.out.println method.

Output:

I will practice 10 programs today

NOTE: Did you find output as “I will practice10Today”? Use space between all values using string “ “.

 

SOLUTIONS

Solution-1:

Solution-2:

Notice: spaces has been used as a string value i.e. ” ” to keep values string , num and str separate.