new Boolean() vs. static Boolean constants - how java compiler handles them

A coworker commented today that it is better practice to use static Boolean constants instead of creating new Boolean objects because Booleans are immutable and creating new Boolean objects would therefore be a waste of memory. For eg. do Boolean.TRUE instead of new Boolean(true). However a friend responded that a good java compiler would take care of that. Does anyone know if that is true? And if so, do most java compilers implement that optimization and is it safe to assume that it is a given?

[507 byte] By [citressa] at [2007-10-3 2:49:13]
# 1
A good Java compiler can't take care of that. If you say new Boolean(true), Java is obliged to give you a new object.
ejpa at 2007-7-14 20:37:59 > top of Java-index,Developer Tools,Java Compiler...
# 2

Even if the compiler takes care of it, it's good coding practice to use the static instances instead.

That's what they're there for, and it's a lot easier to read as well.

Boolean b = new Boolean(true);

is a lot less easy to read than

Boolean b = Boolean.TRUE;

especially when it's as parameters to methods, and littered all over the code.

And when using static imports it gets even better.

Boolean b = TRUE;

jwentinga at 2007-7-14 20:37:59 > top of Java-index,Developer Tools,Java Compiler...