Class StdOut


  • public final class StdOut
    extends Object
    The StdOut class provides static methods for printing strings and numbers to standard output.

    Getting started. To use this class, you must have StdOut.class in your Java classpath. If you used our autoinstaller, you should be all set. Otherwise, either download stdlib.jar and add to your Java classpath or download StdOut.java and put a copy in your working directory.

    Here is an example program that uses StdOut:

       public class TestStdOut {
           public static void main(String[] args) {
               int a = 17;
               int b = 23;
               int sum = a + b;
               StdOut.println("Hello, World");
               StdOut.printf("%d + %d = %d\n", a, b, sum);
           }
       }
      

    Differences with System.out. The behavior of StdOut is similar to that of System.out, but there are a few technical differences:

    • StdOut coerces the character-set encoding to UTF-8, which is a standard character encoding for Unicode.
    • StdOut coerces the locale to Locale.US, for consistency with StdIn, Double.parseDouble(String), and floating-point literals.
    • StdOut flushes standard output after each call to print() so that text will appear immediately in the terminal.

    Reference. For additional documentation, see Section 1.5 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

    Author:
    Robert Sedgewick, Kevin Wayne
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void main​(String[] args)
      Unit tests some methods in StdOut.
      static void print()
      Flushes standard output.
      static void print​(boolean x)
      Prints a boolean to standard output and flushes standard output.
      static void print​(byte x)
      Prints a byte to standard output and flushes standard output.
      static void print​(char x)
      Prints a character to standard output and flushes standard output.
      static void print​(double x)
      Prints a double to standard output and flushes standard output.
      static void print​(float x)
      Prints a float to standard output and flushes standard output.
      static void print​(int x)
      Prints an integer to standard output and flushes standard output.
      static void print​(long x)
      Prints a long integer to standard output and flushes standard output.
      static void print​(short x)
      Prints a short integer to standard output and flushes standard output.
      static void print​(Object x)
      Prints an object to standard output and flushes standard output.
      static void printf​(String format, Object... args)
      Prints a formatted string to standard output, using the specified format string and arguments, and then flushes standard output.
      static void printf​(Locale locale, String format, Object... args)
      Prints a formatted string to standard output, using the locale and the specified format string and arguments; then flushes standard output.
      static void println()
      Terminates the current line by printing the line-separator string.
      static void println​(boolean x)
      Prints a boolean to standard output and then terminates the line.
      static void println​(byte x)
      Prints a byte to standard output and then terminates the line.
      static void println​(char x)
      Prints a character to standard output and then terminates the line.
      static void println​(double x)
      Prints a double to standard output and then terminates the line.
      static void println​(float x)
      Prints an integer to standard output and then terminates the line.
      static void println​(int x)
      Prints an integer to standard output and then terminates the line.
      static void println​(long x)
      Prints a long to standard output and then terminates the line.
      static void println​(short x)
      Prints a short integer to standard output and then terminates the line.
      static void println​(Object x)
      Prints an object to this output stream and then terminates the line.
    • Method Detail

      • println

        public static void println()
        Terminates the current line by printing the line-separator string.
      • println

        public static void println​(Object x)
        Prints an object to this output stream and then terminates the line.
        Parameters:
        x - the object to print
      • println

        public static void println​(boolean x)
        Prints a boolean to standard output and then terminates the line.
        Parameters:
        x - the boolean to print
      • println

        public static void println​(char x)
        Prints a character to standard output and then terminates the line.
        Parameters:
        x - the character to print
      • println

        public static void println​(double x)
        Prints a double to standard output and then terminates the line.
        Parameters:
        x - the double to print
      • println

        public static void println​(float x)
        Prints an integer to standard output and then terminates the line.
        Parameters:
        x - the integer to print
      • println

        public static void println​(int x)
        Prints an integer to standard output and then terminates the line.
        Parameters:
        x - the integer to print
      • println

        public static void println​(long x)
        Prints a long to standard output and then terminates the line.
        Parameters:
        x - the long to print
      • println

        public static void println​(short x)
        Prints a short integer to standard output and then terminates the line.
        Parameters:
        x - the short to print
      • println

        public static void println​(byte x)
        Prints a byte to standard output and then terminates the line.

        To write binary data, see BinaryStdOut.

        Parameters:
        x - the byte to print
      • print

        public static void print()
        Flushes standard output.
      • print

        public static void print​(Object x)
        Prints an object to standard output and flushes standard output.
        Parameters:
        x - the object to print
      • print

        public static void print​(boolean x)
        Prints a boolean to standard output and flushes standard output.
        Parameters:
        x - the boolean to print
      • print

        public static void print​(char x)
        Prints a character to standard output and flushes standard output.
        Parameters:
        x - the character to print
      • print

        public static void print​(double x)
        Prints a double to standard output and flushes standard output.
        Parameters:
        x - the double to print
      • print

        public static void print​(float x)
        Prints a float to standard output and flushes standard output.
        Parameters:
        x - the float to print
      • print

        public static void print​(int x)
        Prints an integer to standard output and flushes standard output.
        Parameters:
        x - the integer to print
      • print

        public static void print​(long x)
        Prints a long integer to standard output and flushes standard output.
        Parameters:
        x - the long integer to print
      • print

        public static void print​(short x)
        Prints a short integer to standard output and flushes standard output.
        Parameters:
        x - the short integer to print
      • print

        public static void print​(byte x)
        Prints a byte to standard output and flushes standard output.
        Parameters:
        x - the byte to print
      • printf

        public static void printf​(String format,
                                  Object... args)
        Prints a formatted string to standard output, using the specified format string and arguments, and then flushes standard output.
        Parameters:
        format - the format string
        args - the arguments accompanying the format string
      • printf

        public static void printf​(Locale locale,
                                  String format,
                                  Object... args)
        Prints a formatted string to standard output, using the locale and the specified format string and arguments; then flushes standard output.
        Parameters:
        locale - the locale
        format - the format string
        args - the arguments accompanying the format string
      • main

        public static void main​(String[] args)
        Unit tests some methods in StdOut.
        Parameters:
        args - the command-line arguments