What's the difference between ".equals" and "=="?

Viewed 102275

I switched lecturers today and he stated using a weird code to me. (He said it's better to use .equals and when I asked why, he answered "because it is!")

So here's an example:

if (o1.equals(o2))
{
 System.out.println("Both integer objects are the same");
}

Instead of what I'm used to:

if (o1 == o2)
{
  System.out.println("Both integer objects are the same");
}

What's the difference between the two. And why is his way (using .equals) better?

Found this on a quick search but I can't really make sense of that answer:

11 Answers
Related