Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java performance: true vs. Boolean.TRUE

Which of the following is better in terms of performance and efficient memory usage?

Boolean isItTrue(arg){      return Boolean.TRUE; }  boolean isItTrue(arg){     return Boolean.TRUE }  Boolean isItTrue(arg){     return true; }  boolean isItTrue(arg){     return true; } 

It should be faster and easier to work with primitive types, but on the other hand, when using a reference to a static object, no new value is created. Or is it optimized on compiler level and all true and false are replaced by references to the static objects to save memory?

like image 383
Dima Avatar asked Aug 02 '11 11:08

Dima


People also ask

What is the difference between Boolean true and true?

Boolean. TRUE is a reference to an object of the class Boolean, while true is just a value of the primitive boolean type. Classes like Boolean are often called "wrapper classes", and are used when you need an object instead of a primitive type (for example, if you're storing it in a data structure).

Is Boolean true in Java?

The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true" . Example: Boolean.

Does Java 1 evaluate to TRUE?

That's why it instead uses 1 and 0 as replacements for true and false values. In Java, 1 and 0 are of the type int (integer), so it produces an error.

What is a Boolean true?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The following table shows comparisons and boolean operations. x == y.


2 Answers

Firstly, the performance advantage of using any one over the others is most likely to be too small to be relevant. Code simplicity / readability / maintainability is a far more important ... in the vast majority of cases.


None of the examples involve creating Boolean instances.

It is theoretically possible that 3 of the 4 could trigger the initialization of the Boolean class AND that your application wouldn't otherwise have done that. In that highly unlikely event, your entire application will allocate 2 objects that wouldn't otherwise have been allocated. The initialization will probably take a few microseconds, and consume a few bytes of RAM (less than 50) in the long term.


This one will be equal to or faster than all of the others because it simply entails setting a register to zero.

boolean isItTrue(arg){     return true; } 

Taken in isolation, this has to load a static reference from memory, rather than zero a register. However, the JIT compiler may be able to optimize this away in some circumstances.

Boolean isItTrue(arg){      return Boolean.TRUE; } 

On the face of it, this involve a call to Boolean.valueOf(true) to "box" the true, but the JIT compiler should be able to optimize it to the same code as the previous one by inlining the call.

Boolean isItTrue(arg){     return true; } 

On the face of it, this involves a call to Boolean.booleanValue(Boolean.TRUE) to "unbox" the Boolean. This call can be inlined. It is also possible that the JIT compiler can avoid loading the reference to the Boolean object and fetching its value field.

boolean isItTrue(arg){     return Boolean.TRUE } 

Bottom line is that it the relative performance of the 4 alternatives depends on how successful the JIT compiler will be in optimizing. That will depend on the context, the specific of the JIT compiler, the JVM settings, and so on. In the best case, the JIT compiler could (at least in theory) produce the same (optimal) code for all of them.

like image 55
Stephen C Avatar answered Sep 20 '22 21:09

Stephen C


If there is any performance gain it is so minuscule as to be irrelevant. Boolean.TRUE and Boolean.FALSE don't return a new object in any case.

like image 43
Jim Avatar answered Sep 18 '22 21:09

Jim