What to do without System.out, to print on console?

io, java

Solution

You could bypass the System object if you want to. System.out does a lot of extra stuff (handling unicode, for instance), so if you really want just the raw output and performance, you actually probably even should bypass it.

import java.io.*;

public class PrintOutTest {
  public static void main(String args[]) throws IOException {
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
      FileOutputStream(FileDescriptor.out), "ASCII"), 512);
    out.write("test string");
    out.write('\n');
    out.flush();
  }
}

This has been elaborated a bit further in here.

Problem

I have been stuck with a problem that was asked recently at an interview. The problem was stated as: Suppose you don't have access to System class in Jdk API. You cannot use ECHO also. You are in JRE 5 environment. How will you print anything on the console? The question really started with -- Why has Java given us the `PrintStream` object `System.out`? And why is it final? Isn't there any other way to print anything on console?

Original source