Java Integer Cache

Java Integer Cache

I was astounded to learn about this feature in Java called the IntegerCache.

A new functionality was added to reduce memory usage and enhance the processing of integer-type objects. Internally, integer objects are cached and utilised through the same referenced objects.

The Integer class keeps a cache of Integer instances in the range of -128 to 127, and all autoboxing, literals and uses of Integer.valueOf() will return instances from that cache for the range it covers.

When built with the constructor, integer objects will not be cached.

Below is my trial in an online IDE.

The purpose is mainly to save memory, leading to faster code due to better cache efficiency.

This is based on the assumption that these small values occur much more often than other ints and therefore it makes sense to avoid the overhead of having different objects for every instance (an Integer object takes up something like 12 bytes).

Happy learning!

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

Preethi Pattabiraman的更多文章

  • Switch-Case

    Switch-Case

    There is nothing that helps a developer like a switch case statement. With multiple conditions and business logic to…

    2 条评论
  • MongoDB Operators

    MongoDB Operators

    MongoDB provides a wide range of intuitive operators that help us to query the collections for the relevant documents…

    1 条评论
  • Circuit Breaker

    Circuit Breaker

    A microservice always needs its friends, the supporting microservices. They all collaborate to make an application…

    1 条评论
  • REST API - Content Negotiation

    REST API - Content Negotiation

    Please find part 1 of this series in the above link. Generally, REST resources can have multiple representations of the…

    1 条评论
  • REST API - Documentation

    REST API - Documentation

    There are some basic REST API features that are expected from any API, such as Documentation Content Negotiation I18N…

    9 条评论
  • Finally

    Finally

    Many of the interview questions are aimed to make us pause. But, if you note those little pointers, it will be a…

社区洞察