JavaBeat Certifications Certifications Kits Articles Interview Questions OCAJP 7 OCPJP 5 OCPJP 6 OCEJWCD 6 SCBCD 5.0 SCEA SCJA

About Wrapper class in Java

Topic :
Feedback Request New QnA Print Email

Here no boxing or unboxing need to happen so it doens't happen. Generally when you compare objects using '==' it checks for reference equality rather its value equality(done through equals() method of object). But then how i==j returns true ? As String pool even Integer has its own pool or cache.

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}

From -128 to 127, values available in the the pool already, it takes from the pool. And for the values above/below it will create new object in the heap. Do the same operation for any value above 127 you will get false as the result.

Try the below snippet.

public class IntegerValueCheck {

public static void main(String[
] args) {
Integer i = 127;
Integer j = 127;
System.out.println("i==j >> "+ (i==j));

Integer a = 127;
Integer b = new Integer(127);

System.out.println("a==b >> "+ (a==b));

Integer k = 128;
Integer l = 128;
System.out.println("k==l >> "+ (k==l));


}

}

i==j >> true
a==b >> false
k==l >> false

Topic :
Feedback Request New QnA Print Email

All api java java 6.0 java 7.0 jdbc jsp servlet ejb jndi jms ejb 3.0 j2ee jee 5.0 jee 6.0 jsf struts spring Hibernate ajax JBoss Seam netbeans eclipse ant xml maven dojo junit javafx j2me log4j ESB JBoss Apache Quartz scjp mysql oracle gwt openjpa jmx yui google-guice android JBoss scwcd 5.0 scjp 1.5 scjp 1.6 scja scbcd 5.0

javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2013), JavaBeat