Mad_Matt
Mad_Matt

Reputation: 75

JAVA : regarding System.out

JAVA : is there a difference between the two references "p" && "pp"?

    PrintStream p = new PrintStream(System.out);
    p.println("lol");

    PrintStream pp = System.out;
    pp.println("lol");

I would just like to shorten the System.out.println(); statement for some prototyping. cheers! matt

Upvotes: 2

Views: 159

Answers (3)

aioobe
aioobe

Reputation: 420951

No, there's no behavioral difference between the two.

System.out is already a PrintStream, and a new PrintStream(otherPrintStream) just creates a wrapper object which only delegates to the given PrintStream.


As @MarkoTopolnik suggest, you can even do

import static java.lang.System.out;

and just do

out.println("lol");

if you want to keep it short.

Upvotes: 3

Pramod Kumar
Pramod Kumar

Reputation: 8014

No difference.

Both the statement will effect the same. Slightly difference is we are creating a new object of PrintStream class in first statement unnecessary.

Upvotes: 1

Jules
Jules

Reputation: 15199

There is no functional difference, although the first creates a new object that you don't need and is therefore slightly less efficient.

Upvotes: 3

Related Questions