System.out.println("Hello, world.");
However, this method does not work well with non-ASCII characters. The following is some test code to demonstrate this:
import java.io.PrintStream;
public class Test {
public static void main (String[] argv) {
String unicodeMessage =
"\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";
PrintStream out = System.out;
out.println(unicodeMessage);
}
}
When compiled and executed, it looks like this:
iboook:~/Documents/codes tokek$ java Test
?????????By looking at this, one might be misled to think that Unicode is not supported at the console for OS X. Well, it turns out that System.out prints Unicode strings using the MacRoman charset. One of the problems with this is that Japanese characters cannot be expressed with MacRoman, and the console uses UTF-8, not MacRoman. In order to surmount this problem, we will create a new PrintStream object that uses UTF-8.
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Test {
public static void main (String[] argv) throws UnsupportedEncodingException {
String unicodeMessage =
"\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(unicodeMessage);
}
}
(The PrintStream constructor is the reason why we need a throws UnsupportedEncodingException.)
The corrected code has the proper output of:
皆さん、こんにちは
Here, the constructor options mean:
- Use System.out as the underlying OutputStream
- Flush OutputStream after each print, println, and write call
- Write UTF-8 byte sequences to the underlying OutputStream. If the character encoding is not specified, a PrintStream constrctor will use MacRoman on OS X as the object's character encoding for print and println methods.
To clarify the Java 1.4.2 API for this class, it should be pointed out that the print methods don't always use the "platform's default character encoding" because a different one can be specified in the constructor's arguments -- it uses whatever encoding that is associated with the PrintStream object.

