Print String to Console Output

To print a String to console output, you can use System.out.print() 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.

Example 1 – Print a String to Console Output in Java

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

/**
 * An example java program to print a string to system console output
 */
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

Example 2 – Multiple System.out.print() functions

In the following program, we have multiple Syste.out.print() functions.

PrintString.java

/**
 * Java Example Program, to print String to console in 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.

Example 3 – Multiple System.out.println() functions

In the following program, we have multiple Syste.out.println() functions to demonstrate on how to print to console in new lines.

PrintString.java

/**
 * Java Example Program, to print String to console in new lines in 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.

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