Prepare
Practice
Interview
Aptitude
Reasoning
English
GD
Placement papers
HR
Current affairs
Engineering
MCA
MBA
Online test
Login
Online Practice Test
>
Java 6
« Previous
Next »
Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?
Options
- Compiler error
- Runtime Exception
- True
- False
CORRECT ANSWER : False
Discussion Board
EXPLANATION
because == is used to compare 2 objects that means two objects refers to same memory location or not .
In this case we are using new operator that means both objects have different memory location that's why answer is false.
one more thing : if we remove new
Integer a = Integer(5)
Integer b = Integer(5)
that means both of the objects ( a and b ) refers to same memory location because java uses string pool and assign same memory location then answer will be true in that case.
Jitender Kumar 11-8-2015 06:00 AM
chemistry
pls i need it in my exams
olajitan victor 06-17-2015 10:02 AM
answer to question
I think this is because you compare objects, which have different id. You need to get the value from each of those objects.
i.e.
Integer a = new Integer(5);
Integer b = new Integer(5)
if(a.intValue() == b.intValue() ){
}
Therefore by using intValue() you get back the primitive type to compare and not the object.
Another way is the one below, but i am not sure if it returns primitive type
if(a.compareTo(b) == 0){
}
Tas 12-4-2014 05:36 AM
even though both are intialized with same values,why is it false!
why the answer is false?can any one xpln?
cherry 10-15-2014 09:40 AM
« Previous
Next »
Write your comments
*
*
Email must be in the form someone@domain.com
*
*
Enter the code shown above:
Please enter the code shown above
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)
Related Content
Java Beginner (11)
Java (39)
Java (40)
Java (22)
Java (30)
Java (25)
Java (20)
Java (20)
Core Java (20)
Core Java (10)
Core Java (72)
EJB (20)
JDBC (20)
Applet (20)
Struts (21)
Servlets (20)
Java Web Services (20)
Javascript (40)
J2EE (10)
jQuery (46)
Advertisement
▲