• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock exam question

 
Frank Hale
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody explain to me why this program as this behavior?
public class test {

public static void main(String[] args)
{
Integer a = new Integer(0);
Integer b = new Integer(0);
System.out.println(a.equals(b)); // True
System.out.println(a==b); // False
}
}
Why aren't both lines true? zero does infact equal zero correct or I am totally confused.
Thanks,
Frank
 
Ankur Gupta
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals() checks for the exact content in the two memory locations where the two objects 'a' and 'b' are. == checks the exact memory locations where the two objects are, and if they r same it returns true. Dose it make sense ?
 
Frank Hale
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thank you. I've just started looking at mock exam questions and although I do program in Java quite alot I still have alot of work to do in order to get ready for the real exam. Thank you for clearing up my question.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic