Java Puzzle 1 - Autoboxing

Java Puzzle 1 - Autoboxing

Use collections with care in your code or else you will get unwanted side effects. On this article, I show the likely problem that requires extra caution when working with collections.

No alt text provided for this image

The above code snippet is really easy and straightforward to understand.

Line 11 we are creating a collection HashSet object.

Line 13 to 15 we are adding elements into to the Set.

Line 17 to 19 we are removing elements from the Set.

Line 18 has no effect because s - 0 has no effect on the result, that is, the arithmetic operation results in the same value. Therefore line 21 should print Zero as size. Yes! We got it. If you agree with me, we are wrong :)

Let's explain why below.

First, we are adding a short primitive on line 14. However, owing to the concept of Auto-boxing added since Java 1.5 or Java 5, the short primitive is converted to it's corresponding wrapper type which is a Short. This one is fine.

Now coming to line 18 where we are removing the elements, observe that it is as though we are removing the same short we added on previous line. However, we are not! Let's break it down.

When you perform an arithmetic operation on variables of one of the following primitive types (byte, char, short), the resulting type is always an int. Even if it is say (short + short). So, s - 0 results in an int type, because before remove() actually execute, s - 0 must be evaluated first. Evaluation of it results in an int type. Now, auto-boxing the type to corresponding wrapper type, we end up with Integer object. But our Set contains elements of type Short. This is where the problem lies.

Although s1 and i1 are equivalent below, they are not equal!

Short s1 = 1;
Integer i1 = 1;

These two classes are just siblings to the same parent (Number) but they are not the same. An implementation of equals method for every wrapper class first checks if the Object passed as an argument is of the same type to this object. If not, it returns false. That is why s1 and i2 are not equal. Therefore, the code above should print 10 on the console.


I have created a short video to explain this concept practically, you can watch the video from the link below.

要查看或添加评论,请登录

Mothusi Molorane的更多文章

  • Delegate pattern, mixed feelings?

    Delegate pattern, mixed feelings?

    Using OpenApi code generator is a common practice as it allows developers to focus on writing business logic and let a…

  • Sleep vs Wait

    Sleep vs Wait

    In Java, sleep and wait are both used to pause the execution of a thread, but they serve different purposes and have…

    1 条评论
  • The for each loop

    The for each loop

    What is a forEach loop? The syntaxic sugar to iterate over an iterable, internally the compiler creates an iterator for…

  • Deciphering Spring Boot magic

    Deciphering Spring Boot magic

    What is spring auto configuration? First, this question seems to be easy because as you know, most developers use…

  • Spring Dependency Injection

    Spring Dependency Injection

    Dependency Injection Is an action of supplying dependencies to a dependant bean. Since most of my audience are familiar…

  • MicroService All In One

    MicroService All In One

    Good morning to my LinkedIn friend. Today, I want to share my presentation slides again.

  • Spring Authorisation Server

    Spring Authorisation Server

    SPRING SECURITY “Spring Security is a framework that provides authentication, authorization, and protection against…

  • Spring Data Series 2

    Spring Data Series 2

    To my LinkedIn audience, happy Tuesday morning. Remember the list of topics we are looking at in our series.

  • JPA and Spring Data

    JPA and Spring Data

    I have already published several articles on spring data and I will be building from them. If you have not read the…

  • I had an interview with a developer

    I had an interview with a developer

    Mothusi: Hello guys, today I have a Java interview with JavaSpace (aka JS) the name of my YouTube channel. Please help…

    3 条评论

社区洞察

其他会员也浏览了