tags:

views:

262

answers:

4

To my surprise in the following program the Eclipse console doesn't print while in the loop. It prints only: "start: finish". When I use println instead it does. Of when I remove the comment it does too. Surprise: when I copy the lines "start: finish" in the console, the clipboard does contain all printed numbers. Interesting to know waht is the cause of this behaviour?

public static void main(String[] args) {
     System.out.print("start: ");
     for (int i = 0; i < 10000; i++) {
      // if (i > 1000 && i < 1010)
       System.out.print(i + " ");
     }
     System.out.println("finish");
    }
A: 

Println in Java or print("\n") In C & C++ flush the buffer and force it to print every thing in buffer but when use print whitout any \n (new line) it's not necessarily print buffer

Am1rr3zA
A: 

Well, you have a very, very long line there. So, it is quite possible that you have exceeded the maximum line length that the Eclipse console can reliably display. The content probably has been printed there (as evidenced by the fact that it appeared in your clipboard when you copy and paste) but doesn't render well.

Michael Aaron Safyan
most probable, because it seems to work on other eclipse versions, so the buffer has been printed "invisibly"
Gerard
A: 

Behavior confirmed. Strange, must have something to do with the eclipse wrapped output stream. Eclipse 3.5 by me. Perhaps the internal buffering throws away output if it is not properly flushed() or too wide for a line.

package tests;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class EclipsePrint {
    public static void main(String[] args) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintStream out = System.out;
        System.setOut(new PrintStream(new BufferedOutputStream(bout), true));
        System.out.print("start: ");
        for (int i = 0; i < 10000; i++) {
                // if (i > 1000 && i < 1010)
                        System.out.print(i + " ");
        }
        System.out.println("finish");
        out.println(bout.size());
        out.println(bout.toString());

        bout = new ByteArrayOutputStream();
        System.setOut(new PrintStream(new BufferedOutputStream(bout), true));
        System.out.print("start: ");
        for (int i = 0; i < 1000; i++) {
                // if (i > 1000 && i < 1010)
                        System.out.print(i + " ");
        }
        System.out.println("finish");
        out.println(bout.size());
        out.println(bout.toString());
    }
}

There is a console settings at Run/Debug:Console:console buffer size (characters) in eclipse which allows you to use up to 10^6 chars as console buffer. Your first loop exceeds it.

kd304
A: 

Tried that on my eclipse 3.4/Mac OSx It printed well when the upper limit was 1000, but for 10000, the display truncated.

Instead of System.out.print, use System.out.println; that's for each line and it works fine for me for 10000.