0

Just to understand, what is happening I done the below code:

public class Loopp {

    public static void main(String[] args) {
        int i=1;
        while(true) {
            Employee e = new Employee("MyName", i);
            i++;
            System.out.print(i + " ");
        }
    }
}

But on console I do not see any output, but when I run this in debug mode, then it prints 2 3 4 ..
I understand that gc is activated again and again to collected the garbage objects, but does that clear console also :|

Edit:

As per the answer , it worked for me, and I learned a new thing today

System.out.println(i + " ");
System.out.flush();
lowLatency
  • 5,534
  • 12
  • 44
  • 70

1 Answers1

8

You are using print without flush. Only println has autoflushing semantics.

Add System.out.flush() to your code.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Doesn't it flush when the program exits? – mostruash Jul 10 '13 at 20:00
  • 2
    @mostruash This program never exits – Brian Jul 10 '13 at 20:01
  • 1
    @mostruash But you *are* right in another way: when the buffer fills up, and it should rather quickly given the code, then the flush happens anyway. However, since that may be quite a lot of output, the console may delay showing it. – Marko Topolnik Jul 10 '13 at 20:02
  • On that note, is there any limit on the buffer size before it finally prints? I know it would be OS-specific if there were, but I'm wondering if there's any documentation about how big it is for the different OS flavors since it's ultimately just using standard out descriptors. – Brian Jul 10 '13 at 20:03