Linked Questions
11 questions linked to/from Comparing Integer values in Java, strange behavior
5 votes
3 answers
800 views
Integer comparison in Java [duplicate]
Integer comparison in Java is tricky, in that int and Integer behave differently. I get that part. But, as this example program shows, (Integer)400 (line #4) behaves differently than (Integer)5 (line ...
-1 votes
1 answer
56 views
What is the exact behaviour of comparing Integers in Java? [duplicate]
as far as I understand due to Java autoboxing I can do this: Integer i = 10; Integer j = 10; boolean b = i == j; Or this: int ii = 10; int jj = 10; Integer i = ii; Integer j = jj; boolean b = i == j;...
210 votes
8 answers
70k views
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } Output: false class D { public static void main(...
27 votes
7 answers
218k views
Integer value comparison
I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out....
12 votes
6 answers
27k views
What is the cleanest way to compare an int with a potentially null Integer in Java?
Map<String,Integer> m; m = new TreeMap<String,Integer>(); Is it good practice to add the following cast just to avoid the null pointer exception when m.get() is null. System.out.println( (...
11 votes
5 answers
14k views
How != and == operators work on Integers in Java? [duplicate]
The following code seemed really confusing to me since it provided two different outputs.The code was tested on jdk 1.7. public class NotEq { public static void main(String[] args) { ver1(); ...
3 votes
3 answers
1k views
Why Comparison Of two Integer using == sometimes works and sometimes not? [duplicate]
I know that i am comparing reference while i'm using == which is not a good idea but i did not understand why is this happening. Integer a=100; Integer b=100; Integer c=500; Integer d=500; System.out....
2 votes
5 answers
508 views
Why == in case of Integer check for value not reference in java , case different with new operator
As we know that == in case of Objects returns true if pointing to the same reference else it returns false. So , if i have taken Integer a = new Integer("1"); // Creating Integer Object a ...
-2 votes
2 answers
322 views
i am getting true and false output why? [duplicate]
I know int range is -2147483648 to +2147483647 but here I'm getting output as true and false. Why? Actually i1 and i2 point to the same object, so output is true. I can understood but i3 and i4 also ...
0 votes
4 answers
126 views
When does Integer != 1 come true [closed]
This is classical, but writing to Google did not give me hits. My code: Integer i = ..; // Sth is put from database. if (i != 1) { // do sth } else { // do not. } Case: I know that this ...
0 votes
0 answers
38 views
Why is this HackerRank problem working fine with 'int' but not with 'Integer'? [duplicate]
"Birthday Cake Candles" is the name of this problem in HackerRank This is the working code and if I replace "int" with "Integer" it is not working. It is working for all ...