Java
Console Output
System.out.println
Color Printing
Programming Tips

How to print color in console using System.out.println?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

System.out.println() does not have a Java-specific “print this in red” feature. It only writes characters to standard output, and whether those characters become colored text depends on the terminal that receives them.

In practice, Java console color is usually done with ANSI escape sequences. If the terminal understands ANSI codes, the text appears colored. If it does not, you will just see plain text or raw escape characters.

How ANSI Color Works

ANSI escape sequences are short control strings that tell a compatible terminal to change formatting. For text color, the usual pattern is:

  • start with an escape prefix such as \u001B[
  • add a color code such as 31 for red
  • end with m

So red text starts with \u001B[31m, and resetting back to the default color uses \u001B[0m.

A minimal Java example looks like this:

java
1public class ColorDemo {
2    private static final String RESET = "\u001B[0m";
3    private static final String RED = "\u001B[31m";
4    private static final String GREEN = "\u001B[32m";
5    private static final String YELLOW = "\u001B[33m";
6
7    public static void main(String[] args) {
8        System.out.println(RED + "Error" + RESET);
9        System.out.println(GREEN + "Success" + RESET);
10        System.out.println(YELLOW + "Warning" + RESET);
11    }
12}

When you run this in a compatible terminal, each line appears in a different color.

A Cleaner Way to Structure Console Colors

Hardcoding escape strings everywhere gets messy quickly. A small helper method keeps console output readable.

java
1public class ColorPrinter {
2    private static final String RESET = "\u001B[0m";
3
4    public static void printWithColor(String colorCode, String message) {
5        System.out.println(colorCode + message + RESET);
6    }
7
8    public static void main(String[] args) {
9        printWithColor("\u001B[34m", "Info message");
10        printWithColor("\u001B[31m", "Failure message");
11    }
12}

This approach is easier to extend if you want background colors, bold output, or a consistent logging style.

Terminal Support Matters

The Java code itself is not the hard part. The terminal is.

ANSI sequences are widely supported in:

  • macOS Terminal
  • Linux terminals
  • Windows Terminal
  • recent Windows console environments

Older Windows command prompts historically needed extra configuration or did not behave well with ANSI codes. If you are writing a command-line tool for other users, test on the consoles they actually use.

If you need broader compatibility, use a library that abstracts terminal capabilities rather than writing raw ANSI codes everywhere.

When to Use a Library

For simple scripts, raw ANSI codes are enough. For larger CLI tools, a library can give you better portability and cleaner code.

Examples in the Java ecosystem include libraries that manage:

  • color enablement and fallback
  • cursor movement
  • table rendering
  • cross-platform terminal behavior

The benefit is not just convenience. It also reduces the risk of leaving the terminal stuck in a colored state because you forgot to reset formatting after printing.

A Useful Pattern for Status Output

One practical use of color is status labeling. For example, green for success, yellow for warnings, and red for failures makes logs easier to scan.

java
1public class StatusOutput {
2    private static final String RESET = "\u001B[0m";
3    private static final String RED = "\u001B[31m";
4    private static final String GREEN = "\u001B[32m";
5
6    public static void main(String[] args) {
7        System.out.println(GREEN + "[OK] Build completed" + RESET);
8        System.out.println(RED + "[FAIL] Tests failed" + RESET);
9    }
10}

This is still just System.out.println(). The method prints text normally; the console interprets the escape codes as styling instructions.

Common Pitfalls

A common mistake is assuming the color feature belongs to Java itself. It does not. The terminal decides whether ANSI escape sequences are honored.

Another mistake is forgetting to reset the color after printing. If you omit \u001B[0m, later output may stay colored unexpectedly.

Developers also sometimes build CLI tools that look correct in one terminal and break in another. If your tool will be shared, test it in multiple console environments.

Finally, avoid using color as the only source of meaning. Users may have accessibility needs, monochrome terminals, or logs redirected to files where color is lost.

Summary

  • 'System.out.println() does not directly support color, but it can print ANSI escape sequences.'
  • Console color works only when the target terminal supports those sequences.
  • Always reset formatting with \u001B[0m after colored output.
  • A helper method or library makes colored output easier to manage.
  • Use color to improve readability, not as the only way to convey information.

Course illustration
Course illustration

All Rights Reserved.