In PrintWriter, why does the print () function also not work automatically?

When considering a contract PrintWriterfor the following constructor:

public PrintWriter(OutputStream out, boolean autoFlush)

Creates a new one PrintWriterfrom an existing one OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which converts characters to bytes using the default encoding.

Parameters:
out- output stream
autoFlush- A boolean; if true, methods println, printfor formatwill clear the output buffer

See also: OutputStreamWriter.OutputStreamWriter (java.io.OutputStream)

Please note that the flag autoFlushonly works on println, printfand format. Now I know that printf, and formatbasically do the same thing, and that print, except for the larger number of parameters, but I just do not understand why they were not included print, as well as in the contract. Why did they make this decision?

+4
source share
1 answer

I suspect Java authors are making performance assumptions:

Consider the following code:

public static void printArray(int[] array, PrintWriter writer) {
    for(int i = 0; i < array.length; i++) {
        writer.print(array[i]);
        if(i != array.length - 1) writer.print(',');
    }
}

, flush() . , . , - , flush .

, printf, format println, , , . , -, .


- ( ):

, , -. , . , , , .

, Java -. , ; API- native input , . , , API , .

< >

, , . .

autoflush, . autoflush , . , autoflush PrintWriter println format.

+3

All Articles