In this Java tutorial, you will learn how to print a String to console output using System.out.print() or System.out.println() functions, with examples.

Print String to Console Output

To print a String to console output, you can use System.out.print() function or System.out.println() function.

If you have started with Java programming, System.out.print() is one of the statements that you see very often.

The only notable difference between System.out.print() and System.out.println() is that, println() appends a new line character to the string you provide as argument to the function.

Developers usually print to console when they are developing console applications, or if they would like to check some intermediary values of their program.

In this tutorial, we shall learn to print a String to console using Java.

Examples

ADVERTISEMENT

1. Print a string to console output

Following is a very basic Java program. It has a class and main method. In the main method, we call a function print() that prints a string to console.

PrintString.java

public class PrintString {
	public static void main(String[] args) {
		System.out.print("Hello World !");
	}
}

Run the above Java program, from command prompt or in an IDE. In the console window, you would see the following printed out.

Output

Hello World !

Following is the screenshot of console window with the program is run in Eclipse IDE.

Print a String to console output in Java - Java Examples - Java Tutorials - www.tutorialkart.com
Print a String to console output in Java

2. Multiple print statements

In the following program, we have multiple print() functions to print multiple strings to console output.

PrintString.java

public class PrintString {
	public static void main(String[] args) {
		System.out.print("Hello World!");
		System.out.print("Welcome to www.tutorialkart.com.");
	}
}

Run the program.

Output

Hello World!Welcome to www.tutorialkart.com.

Please note that the two strings have been printed to console as they are concatenated.

If you would like to print strings in a new line, you System.out.println(). Following example demonstrates the same.

3. Print strings in new lines

In the following program, we have used println() function to print each of the given strings in new lines.

PrintString.java

public class PrintString {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		System.out.println("Welcome to www.tutorialkart.com.");
	}
}

Run the program.

Output

Hello World!
Welcome to www.tutorialkart.com.

The strings have been printed to new lines.

Insight into System.out.println(String message) method

In the context of printing something to console, System class provides a means to access standard output through one of its fields, out.

‘out’ field is a Stream (to be specific, its a PrintStream), which is declared public static and final. Hence, one can use ‘out’ directly without any initialization.

And PrintStream.print(String s) prints the string. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

By default, when running the program through a command prompt or any IDE like eclipse, console is the output.

Conclusion

In this Java Tutorial, we learned to print a String to console output in Java programming language.