Printing Program Output in Java

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”.


public class Sample {

	public static void main(String[] args) {
		
		System.out.println("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.


public class Sample {

	public static void main(String[] args) {
		
		System.out.println("My name is Peter");
		System.out.println("I am learning Java");
		System.out.println("I like Coffee");

	}

}

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:


public class Sample {

	public static void main(String[] args) {

		
		String str = "I Like Coffee";// String variable
		int count = 2; //int 
		double cost = 99.00;//double 

		//Print all data on console
		System.out.println(str);
		System.out.println(count);
		System.out.println(cost);

	}

}

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.


public class Sample {

	public static void main(String[] args) {

		
		String str = " Coffee!!!";
		int count = 100;	
		
		System.out.println(count+str);	

	}
}

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.


public class Sample {

	public static void main(String[] args) {		
		
		int num = 100;	
		
		System.out.println("value = "+ 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.

System.out.println("value = "+ num
System.out.println("value = "   +            num);

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

public class Sample {

	public static void main(String[] args) {		
		
		int num = 10;
		String str = "Today";
	
		System.out.println("I will practice");	

	}
}

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

SOLUTIONS

Solution-1:

public class Sample {

	public static void main(String[] args) {		

		System.out.println("I am going to be expert in" + "java programming" + " in few days");

	}
}

Solution-2:

public class Sample {

	public static void main(String[] args) {		

		int num = 10;
		String str = "Today";
	
		System.out.println("I will practice" + " "+num + " "+str);	


	}
}

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

Related Posts