0

position of output of System.err.println(); is varying at the output even if position of it in code is similar. kindly please see the 4 cases with same position of System.err.println(); in it but its position in output is varying. please see the output at last of the program in comments.please Explain the Execution order.try the below code to see the astonishing output yourself.

public class System_Ex6 {
    public static void main(String[] args)throws Exception {

        System.out.println("-----------------case 1-------------------");
        System.out.println("Java");
        System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color

        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("os.name"));
        //System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("java.Vendor"));
        //System.out.println(System.getProperty("os.version"));
        System.gc();

        System.out.println("-----------------case 2-------------------");
        System.out.println("Java");
        System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color

        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("os.name"));
        //System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("java.Vendor"));
        //System.out.println(System.getProperty("os.version"));
        System.gc();

        System.out.println("-----------------case 3-------------------");
        System.out.println("Java");
        System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color

        //System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("java.Vendor"));
        //System.out.println(System.getProperty("os.version"));
        System.gc();

        System.out.println("-----------------case 4-------------------");
        System.out.println("Java");
        System.err.println("ErrorStatement<======Please observe the position of it "); //output comes in red ink color

        System.out.println(System.getProperty("java.version"));
        System.out.println(System.getProperty("os.name"));
        //System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("java.Vendor"));
        System.out.println(System.getProperty("os.version"));
        System.gc();



    }//main()

}//class
/*
output:
=======

-----------------case 1-------------------
Java
1.8.0_74
Windows 8
null
ErrorStatement<======Please observe the position of it 
-----------------case 2-------------------
Java
1.8.0_74
ErrorStatement<======Please observe the position of it 
Windows 8
null
-----------------case 3-------------------
Java
ErrorStatement<======Please observe the position of it 
Windows 8
6.2
null
-----------------case 4-------------------
Java
1.8.0_74
Windows 8
null
6.2
ErrorStatement<======Please observe the position of it 



*/
  • No, i think theres more to this Question as in all 4 cases in above,most code is same but the major difference comes when we call "os.version", does this calls System.out.flush(); and System.err.flush(); internallys ?? – edwin moses ma Oct 04 '16 at 13:59

2 Answers2

0

When I run the program on my system, the relative position of the ErrorStatement line is the same in all cases.

Here's what I see:

$ java System_Ex6 
-----------------case 1-------------------
Java
ErrorStatement<======Please observe the position of it 
1.8.0_102
Linux
null
-----------------case 2-------------------
Java
ErrorStatement<======Please observe the position of it 
1.8.0_102
Linux
null
-----------------case 3-------------------
Java
ErrorStatement<======Please observe the position of it 
Linux
4.7.5-100.fc23.x86_64
null
-----------------case 4-------------------
Java
ErrorStatement<======Please observe the position of it 
1.8.0_102
Linux
null
4.7.5-100.fc23.x86_64

$ java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

What you are seeing is an implementation-specific artifact. It could be happening in Java, or in the console program that is reading the data written to System.out / System.err and writing it into the scrolling text buffer that you are seeing ... and copying-pasting from.

Bottom line: this behavior is not specified, and you cannot rely on it. The fact that it is different on different systems is not surprising.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

System.out and System.err are are 2 different streams and are flushed at different times.