Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When a boolean or int are placed in a java Map that contains Objects, does emplacing them actually create a new Object?

Since in Java, generics usually operate on Object under the hood, I was curious if emplacing such values incurs more overhead than placing an already existing object.

Consider having a Map<String, Object> that can contain arbitrary meta information. You want to put a flag kinda entry there – if it is present, the flag is enabled.

So you have something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    static final String MY_FLAG_NAME = "name";
    Map<String, Object> myMap;

Now I wonder if there is a difference in between these three implementations of setting the flat:

  1. myMap.put(MY_FLAG_NAME, true) (false to disable)
  2. myMap.put(MY_FLAG_NAME, null) (we can use containsKey, but this looks dirty to me)
  3. myMap.put(MY_FLAG_NAME, MY_FLAG_NAME) (string is already allocated, and we can use "" for disable)

My question here is if this will force at some point some kind of new Boolean() under the hood, or if it does not matter whether I put a string or a boolean. I never really got to understood how precisely Java handles the boolean/Boolean difference in generics, same for other primitive types.

This code would be called a lot, but I don’t have enough Java knowledge to make a trustworthy performance test.

>Solution :

Boxing constant booleans (like the literals true and false) does not create arbitrarily many new objects. The Java Language Specification says:

If the value p being boxed is the result of evaluating a constant expression (§15.29) of type boolean, […], and the result is true, false, […], then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

If it is not a constant boolean expression (e.g. someString.length() == 5), the language specification doesn’t guarantee boxing to produce the same object, but I think any reasonable implementation would implement boolean boxing as calling Boolean.valueof(boolean), which does not create arbitrarily many new Boolean objects. It just returns the static final fields Boolean.TRUE and Boolean.FALSE. Of course, new Boolean objects will be created when the TRUE and FALSE fields are initialised, but no other Boolean objects will be created if you only use Boolean.valueOf.

Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading