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

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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];
        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,290 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
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,020 points
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,520 points
+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,820 points
+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,360 points

edited Nov 6, 2018 by Omkar 4,529 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,030 points
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,030 points
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,030 points
+6 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
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,290 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.