Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Oliver Hayden
PLUS
Oliver Hayden
Courses Plus Student 714 Points

Layout of equals method and equalsIgnoreCase method?

Hi,

I'm a little bit confused about when Craig looks up both equals and equalsIgnoreCase methods they are written differently on the Oracle website to the way Craig writes them.

for example on Oracle the equals method is written:

boolean equals(Object anObject)

Craig writes it noun.equals("dork")

I understand the words are different because they are the ones from the code but I don't understand why the layout is different? Like where does the full stop come from? Or why is object before the brackets instead of inside them?

Just want to know for the future when looking new methods up, that I know how to write them.

Any help would be great thank you! Ollie

1 Answer

Hi Ollie,

The method skeleton/signature is different to how it looks in actual code. Let me try to explain!

The equals method can be defined as you've seen in the documentation:

boolean equals(Object object)

This means that the method is called equals, it returns a boolean and takes one parameter of any object.

Let's start with the object. In this course, we are comparing String objects and we use the string literal "dork" by way of example. Surrounding a string in inverted commans is enough to create a String object; we don't need to use formal construction for strings, unlike with some other objects.

The dot notation Craig uses, is how you call an instance method on an object or instance. The equals method belongs to a certain class (probably Object, I don't know) and all sub classes of that. Once you create an instance of a class, you can use its methods by utilising dot notation.

So, we've created an object of String called noun and we want to know if the string held in that variable is equal to the string literal "dork". We can use the equals method to do that on the noun instance:

noun.equals("dork");

That evaluates to either true or false (the returned boolean) which you can either assign to a variable;

trueOrFalse = noun.equals("dork");

.. there, the variable trueOrFalse will now hold either true or false. Alternatively, you can use a conditional to change your code's execution direction:

if(noun.equals("dork")){
  // do dorky stuff
} else {
  // don't be a dork
}

I hope that helps explain some of that to you. Shout back if you have further questions.

Steve.