Sunday, February 22, 2009

Optimizations in OOO

What is the approximate output of the following:

private ByteArrayOutputStream baos;
private ObjectOutputStream out;
baos=new ByteArrayOutputStream(10000000);
out = new ObjectOutputStream(baos);

for(int i=0;i<1000;i++)
out.writeObject("Madhu");

System.out.println("Size = " + baos.size());

Hopefully it should print 1000*5(length of "Madhu").

But it prints a number <100.

Why .............................?????????

Because OOO has a lot of optimizations techniques. It knows that we are writing the same object again and again, so it just increases a count of the object written.

So, be careful with java....

No comments: