MemoryAllocations
From APIDesign
(New page: Is there an externally observable difference between following programs? <source lang="java"> private static String msg; public static void sayHello() { msg = "Hello World!"; } </source...) |
(New page: Is there an externally observable difference between following programs? <source lang="java"> private static String msg; public static void sayHello() { msg = "Hello World!"; } </source...) |
Current revision
Is there an externally observable difference between following programs?
private static String msg; public static void sayHello() { msg = "Hello World!"; }
private static String msg; public static void sayHello() { msg = "Hello" + " World!"; }
At first sight they behave the same, but participants of HPAPIFest09 found out that one can write an external test to distinguish between their behaviour. The trick is simple, just fill all the available heap memory with some garbage and then call the sayHello method. In the first case, it always succeeds, in the latter one, it may yield an OutOfMemoryError.
As there exist an external program that can distinguish between two version of the sayHello library method, we are dealing with some API aspect. This is unlikely to be an aspect important during our regular daily work on server side or desktop libraries. However as soon as we switch to the world of Real time Java, we may soon find out how important it is.