Lesson 4 | System.out |
Objective | Explore why and how System.out is special. |
System.out is a static member of the System class
In classic Java 1.1, System.out is a static member of the System class that represents the standard output stream. It is an instance of the PrintStream class, which is a subclass of OutputStream. By default, System.out is connected to the console or the standard output device, typically the terminal or command prompt. System.out is commonly used for printing text to the console for debugging or informational purposes. For example, to print a message to the console in Java, you would use the System.out.println() method:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
In this example, System.out.println() is used to print the message "Hello, world!" followed by a newline character to the console.
It is important to note that while System.out is useful for basic output and debugging purposes, it is not the most efficient or robust solution for logging in larger applications. For more advanced logging and output management, consider using dedicated logging frameworks like Log4j or SLF4J.
System.err Print Stream
System.err is also a print stream and instead of being hooked up to stdout, it is connected to stderr
.
Both stdout
and stderr
are Unixisms for the console (generally the terminal window in which the program was started) that crept into C, and through C into other platforms like DOS and the Mac. Most of the time System.err
and System.out
amount to the same thing. However it is possible to redirect one to a file or pipe without redirecting the other. You can also redirect both to the same or different locations.
Many common misconceptions about I/O occur because the first exposure to I/O by most programmers is through the console.
The console is convenient for quick hacks and toy examples commonly found in textbooks, but it is really a very unusual source of input and destination for output, and good Java programs avoid it. It behaves almost, but not completely, unlike anything else you would want to read from or write to.
While consoles make convenient examples in programming texts like this one, they are a horrible user interface and really have little place in modern programs.
Users are more comfortable with a well-defined (GUI) graphical user interface.
Furthermore, the console is unreliable across platforms.
The Mac, for example, has no native console and Mac Runtime for Java 2 and earlier has a console window that works only for output,
but not for input; that is, System.out works but System.in does not.
java.lang.System
System.out
and System.err
are public final static
fields in the java.lang.System
class.
They refer to instances of the PrintStream
class, which is normally connected to the console, though some operating systems allow you to redirect it to a file or pipe.
System.out
is primarily used for simple, character-mode applications and for debugging. Its most important reason for existence is convenience, not robustness. Sun has made System.out
very easy to use by allowing it to ignore many issues involved in internationalization and error checking.
This makes System.out
easy to use in quick and dirty hacks and simple examples while simultaneously making it unsuitable for production code.
The PrintStream
class has print()
and
println()
methods that handle each and every Java data type.
PrintStream methods
The PrintStream class has
- print() and
- println()
methods that handle each and every Java data type.
Predefined Streams
As you know, all Java programs automatically import the java.lang package.
This package defines a class called System, which encapsulates several aspects of the runtime environment.
For example, using some of its methods, you can obtain the current time and the settings of various properties associated with the system.
System also contains three predefined stream variables:
- in,
- out, and
- err.
These fields are declared as
- public,
- static, and
- final
within System
This means that they can be used by any other part of your program and without reference to a specific System object.
System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default.
System.err refers to the standard error stream, which also is the console by default. However, these streams may be redirected to any compatible I/O device.
System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream.
These are byte streams, even though they are typically used to read and write characters from and to the console.
As you will see, you can wrap these within character-based streams, if desired. The preceding chapters have been using System.out in their examples.
You can use System.err in much the same way. As explained in the next section, use of System.in is a little more complicated.
public void print()
public void println()
public void print(boolean b)
public void println(boolean b)
public void print(char c)
public void println(char c)
public void print(int i)
public void println(int i)
public void print(long l)
public void println(long l)
public void print(float f)
public void println(float f)
public void print(double d)
public void println(double d)
public void print(char[] c)
public void println(char[] c)
public void print(String s)
public void println(String s)
public void print(Object obj)
public void println(Object obj)
Anything at all can be passed to a print()
method, and it is guaranteed to match at least one of these methods.
Object types are converted to strings by invoking their toString()
method. Primitive types are converted with the appropriate String.valueOf()
method.
overloading + Sign | Combinations of Values
Combinations of values are converted to strings and concatenated.
One aspect of making System.out
simple for quick jobs is not in the PrintStream
class at all, but rather in the compiler.
By overloading the +
sign to allow for concatenation of strings with each other, with primitive data types, and with objects, you can pass
anything at all to the print()
and println()
methods. Consider the following line:
System.out.println("As of " + (new Date()) +
" and there have been over " + hits +
" hits on the Web site." );
The compiler is responsible for rewriting this complicated expression in the following fashion:
StringBuffer sb = new StringBuffer();
sb.append("As of ");
Date d = new Date();
sb.append(d);
sb.append(" and there have been over ");
sb.append(hits);
sb.append(" hits on the Web site.")
String s = sb.toString();
System.out.println(s);