user5544654
user5544654

Reputation:

How to Reimplement System.out.print()

just out of curiosity is it possible to make a method that prints the string in the parameter I have no reason for doing this I just want to know what goes on behind the scenes of out.print and out.println

consoleString("Hello World!");    

public consoleString(string stringForConsole) {
    stringForConsole = What would go here to print this into the console?;

}

Upvotes: 0

Views: 88

Answers (3)

Samuel Toh
Samuel Toh

Reputation: 19248

Q: What goes on behind the scenes of out.print and out.println

A: This is a difficult question. I'm not sure but you can actually see the source code for System.out.println(...) and start tracing its code path from there.

The Java source code is available from the JDK package and it is available for grab on Oracle's Java download page found here.

--This is for Linux x64 package.--

Unzip the package and you will see another nested zip package in there named src.zip. Unzip it for the source code.

System.out code path

Checkout the file lang/System.java file and you'll see the implementation for println().

Just in case you're lazy to do the steps above. This is what it looks like.

public final class System {
    ...
    public final static PrintStream out = null;
    ...
}

So System.out is actually a PrintStream class. So now... you need to look at the PrintStream class...

Blah Blah and the story continues from PrintStream class.

You'll eventually come to this piece of code.

public void write(...) {
    try {
        synchronized (this) {
            ensureOpen();
            out.write(b);
            if ((b == '\n') && autoFlush)
                out.flush();
        }
    }
    catch (InterruptedIOException x) {
        Thread.currentThread().interrupt();
    }
    catch (IOException x) {
        trouble = true;
    }
}

Note. This is not the end of the story... The code still goes on and on till you see output on the screen.

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

Pretty basic question but

public consoleString(String stringForConsole) {
    System.out.println (stringForConsole);
}

If you use a IDE like Eclipse you can step into the code and see what it is doing.

If you click on out for example, you will see that it is using a PrintStream and uses

 * @see     java.io.PrintStream#println()
 * @see     java.io.PrintStream#println(boolean)
 * @see     java.io.PrintStream#println(char)
 * @see     java.io.PrintStream#println(char[])
 * @see     java.io.PrintStream#println(double)
 * @see     java.io.PrintStream#println(float)
 * @see     java.io.PrintStream#println(int)
 * @see     java.io.PrintStream#println(long)
 * @see     java.io.PrintStream#println(java.lang.Object)
 * @see     java.io.PrintStream#println(java.lang.String)

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201439

This is platform specific, but on linux systems (and other *nix systems), you could open /dev/stdout and write to it. Like,

PrintStream ps = new PrintStream(new FileOutputStream(new File("/dev/stdout")));
ps.println("Hello, World");

Upvotes: 1

Related Questions