O
O
overmes2012-09-28 22:06:18
Java
overmes, 2012-09-28 22:06:18

int and integer

I am one such fool that I did not know that the following code produces false:

Integer i = 200;
Integer j = 200;
if(i == j){
    System.out.print("true");
}else{
    System.out.print("false");
}


and the following returns true:
Integer i = 100;
Integer j = 100;
if(i == j){
    System.out.print("true");
}else{
    System.out.print("false");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dshvechikov, 2012-09-28
@overmes

As far as I know, values ​​from -128 to 127 are in the machine's java object pool, so they have one address in memory, and you compare objects using ==, i.e. link

S
ScratchBoom, 2012-09-28
@ScratchBoom

And here it is - the same cache:
www.grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java#573

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question