Understanding Java’s Integer Pool Can Avoid Problems

Understanding Java’s Integer Pool Can Avoid Problems

The next time you come across a situation where two Integers defined with equal values fail an equivalency (==) test, remember this tip. Suppose you have two integers defined as follows:

Integer i1 = 128Integer i2 = 128

If you then execute the test (i1 == i2), the returned result is false. The reason is that the JVM maintains a pool of Integer values (similar to the one it maintains for Strings). But the pool contains only integers from -128 to 127. Creating any Integer in that range results in Java assigning those Integers from the pool, so the equivalency test works. However, for values greater than 127 and less than -128), the pool does not come into play, so the two assignments create different objects, which then fail the equivalency test and return false.

In contast, consider the following assignments:

Integer i1 = new Integer(1); // Any number in the Integer pool rangeInteger i2 = new Integer(1); // Any number in the Integer pool range

Because these assignments are made using the ‘new’ keyword, they are new instances created, and not picked up from the pool. Hence, testing (i1== i2) on the preceding assignments returns false.

Here’s some code that illustrates the Integer pool:

public class IntegerPoolTest {    public static void main(String args[]){        Integer i1 = 100;        Integer i2 = 100;        // Comparison of integers from the pool - returns true.        compareInts(i1, i2);                 Integer i3 = 130;        Integer i4 = 130;        // Same comparison, but from outside the pool         // (not in the range -128 to 127)        // resulting in false.        compareInts(i3, i4);         Integer i5 = new Integer(100);        Integer i6 = new Integer(100);        // Comparison of Integers created using the 'new' keyword         // results in new instances and '==' comparison leads to false.        compareInts(i5, i6);     }    private static void compareInts(Integer i1, Integer i2){        System.out.println("Comparing Integers " +            i1 + "," + i2 + " results in: " + (i1 == i2) );    }}
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as