Behavior of Integer Wrapper class and == operator

0 votes

I have the below code:

Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2);//true

integer1 = 128;
integer2 = 128;
System.out.println(integer1 == integer2);//false

When I am executing this, my == operator is working fine for -128 to 127 else its returning me false. Can someone explain why so?

Jun 6, 2018 in Java by anto.trigg4
• 3,420 points
230 views

1 answer to this question.

0 votes

Well, this is happening because of the below code of Integer wrapper class:

public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

To put it in simple words, when you write,

Integer integer1 = 127;

It means,

Integer integer1 = Integer.valueOf(127);

Thus, for values between -128 to 127, the Integers are kept in cache and are returned multiple times from there whereas, for values lower than -128 or higher than 127, new Integers are generated.

answered Jun 6, 2018 by v.liyyah
• 1,300 points

Related Questions In Java

0 votes
1 answer

Usage of abstract class and interface

Well let me draw a clear line ...READ MORE

answered Oct 23, 2018 in Java by geek.erkami
• 2,320 points
87 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
461 views
0 votes
1 answer

In which class is the "length" property of array defined ?

It's defined in the Java language specification: The members ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,540 points
37 views
+1 vote
3 answers

Copy Java Array and make a duplicate of it

int[] a = {1,2,3,4,5}; int[] b = Arrays.copyOf(a, ...READ MORE

answered Aug 30, 2018 in Java by Sushmita
• 6,880 points
102 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,055 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,965 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,124 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
492 views
+7 votes
11 answers

Default parameters of XMS and XMX in JVM

You use these Java command-line parameters to ...READ MORE

answered Nov 13, 2018 in Java by Murali
100,589 views
0 votes
1 answer

Java Static nested class

Hi, to understand their usage, you must ...READ MORE

answered Jun 7, 2018 in Java by v.liyyah
• 1,300 points
44 views