In this Java tutorial, you will learn how to print a String to console output using System.out.print() and System.out.println(). The examples show single-line output, new-line output, variables, escape sequences, and the reason Java does not use console.log().

Java syntax to print a String to console output

To print a String to console output in Java, pass the String as an argument to System.out.print() or System.out.println().

</>
Copy
System.out.print("Hello World!");
System.out.println("Hello World!");

Use print() when you want the next output to continue on the same line. Use println() when you want Java to end the current line after printing the value.

Difference between System.out.print() and System.out.println() in Java

The main difference is the line break. System.out.print() prints only the text you provide. System.out.println() prints the text and then moves the cursor to the next line.

Java statementWhat it doesWhen to use it
System.out.print("Text")Prints Text without adding a new line.When you want more output on the same line.
System.out.println("Text")Prints Text and then adds a line break.When each message should appear on a separate line.

Neither method adds extra spaces automatically. If you need a space between two printed strings, include the space inside the String or print it separately.

Java program to print one String using System.out.print()

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

</>
Copy
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

Print multiple Java strings on the same console line

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

PrintString.java

</>
Copy
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 want a blank space between the two messages, add the space inside one of the strings.

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

Output

Hello World! Welcome to www.tutorialkart.com.

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

Print each Java String on a new console line

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

PrintString.java

</>
Copy
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.

Print Java String variables and values together

You can print a String variable directly. You can also join a String with numbers, booleans, or other values using the + operator. Java converts the non-String values to text for this output.

PrintStringVariable.java

</>
Copy
public class PrintStringVariable {
	public static void main(String[] args) {
		String course = "Java";
		int lessonsCompleted = 3;

		System.out.println("Course: " + course);
		System.out.println("Lessons completed: " + lessonsCompleted);
	}
}

Output

Course: Java
Lessons completed: 3

For simple console messages, this form is easy to read. For larger formatted output, System.out.printf() is also available, but print() and println() are enough for basic String output.

Print quotes, tabs, and line breaks inside a Java String

Some characters must be written with escape sequences inside a Java String. For example, use \n for a line break, \t for a tab, and \" for a double quote character inside the String.

PrintEscapedString.java

</>
Copy
public class PrintEscapedString {
	public static void main(String[] args) {
		System.out.println("Name:\tJava");
		System.out.println("Message:\nWelcome");
		System.out.println("Quote: \"Hello\"");
	}
}

Output

Name:	Java
Message:
Welcome
Quote: "Hello"

Run a Java console output program from command prompt

If your file is named PrintString.java, compile and run it from the folder that contains the file.

</>
Copy
javac PrintString.java
java PrintString

The class name in the command must match the public class name in the program. In this tutorial, the public class is PrintString, so the run command is java PrintString.

What System.out.println(String message) does in Java

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

The out field is a stream. To be specific, it is a PrintStream. It is declared as a public static field, so you can access it as System.out without creating an object of the System class.

PrintStream.print(String s) prints the string. PrintStream.println(String s) prints the string and then terminates the current line. 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.

Common mistakes when printing a String to Java console output

  • Expecting print() to add a new line: print() does not move to the next line. Use println() for that.
  • Forgetting spaces between printed values: Java prints the exact text you provide. Add spaces in the String when needed.
  • Using console.log() in Java: console.log() is common in JavaScript, not Java. In Java, use System.out.println() for simple console output.
  • Using a file name that does not match the public class: If the class is public class PrintString, save the file as PrintString.java.
  • Confusing compile errors with output errors: Console output appears only after the program compiles and runs successfully.

Quick reference for Java String console output

TaskJava code
Print a String without a new lineSystem.out.print("Hello");
Print a String with a new lineSystem.out.println("Hello");
Print an empty lineSystem.out.println();
Print a String variableSystem.out.println(name);
Print text and a variable togetherSystem.out.println("Name: " + name);

FAQs on printing a String to console output in Java

How do you print text in console in Java?

Use System.out.print("Text") to print text on the current line, or System.out.println("Text") to print text and then move to the next line.

How do you print console log in Java?

For a beginner-level console log message in Java, use System.out.println("message"). Java does not use JavaScript-style console.log() syntax.

Why does Java print two strings together without a space?

System.out.print() does not add spaces or new lines automatically. If you print "Hello" and then "World", the output becomes HelloWorld. Add a space in the String, such as "Hello ".

Can System.out.println() print numbers and boolean values?

Yes. System.out.println() can print strings, numbers, characters, booleans, and object values. For example, System.out.println(25) prints 25.

What is the difference between print, println, and printf in Java?

print() prints without a new line. println() prints with a new line. printf() prints formatted output using format specifiers.

QA checklist for Java String console output examples

  • The Java file name matches the public class name used in the example.
  • The output block shows exactly what print() or println() produces.
  • Spaces and line breaks are visible in the expected console output.
  • The tutorial distinguishes Java System.out.println() from JavaScript console.log().
  • Each command-line example uses javac before java.

Summary of printing a Java String to console output

In this Java Tutorial, we learned to print a String to console output in Java programming language. Use System.out.print() when the next output should continue on the same line, and use System.out.println() when the next output should begin on a new line.