RFR: 8344706: Compiler Implementation of Compact Source Files and Instance Main Methods

Stuart Marks smarks at openjdk.org
Mon Apr 7 17:17:57 UTC 2025


On Sat, 5 Apr 2025 02:31:21 GMT, Chen Liang <liach at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/IO.java line 122:
>> 
>>> 120:     public static void print(Object obj) {
>>> 121:         System.out.print(obj);
>>> 122:         System.out.flush();
>> 
>> Is it worth using a local variable to avoid calling `print` and `flush` on different streams in case `System.out` is reassigned in between?
>> 
>> 
>> var out = System.out;
>> out.print(obj);
>> out.flush();
>
> This code is not that perf sensitive, guess it is fine as-is

I think there's a correctness angle. If you print something to a stream you want to flush the same stream. If System.out is changed in between (unlikely, but possible) then output destined for the old stream could be stranded in its buffer.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/24438#discussion_r2031674696


More information about the core-libs-dev mailing list