No, Java VMs I know don't perfom such optimizations. With modern JVMs it's actually cheaper to create new object rather then look for some existing one. What they also mean in this FAQ is that short-lived, throw-away objects don't really impact garbage collection time (but might increase GC frequency). Also I can imagine JVM performing escape analysis and allocating such object on stack rather than on heap.
However I do think it's quite wasteful. Look at java.lang.Integer
(and all other primitive wrappers) cache or Optional.absent()
in Guava:
public static <T> Optional<T> absent() {
return (Optional<T>) Absent.INSTANCE;
}
There are plenty of other optimizations like that in the JDK: enum
s are singletons, Collections.emptyList()
always returns the same instance, etc.