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 Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Using the internet.

Can someone please explain to me how you get from

"public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides: equals in class Object Parameters: anObject - The object to compare this String against Returns: true if the given object represents a String equivalent to this string, false otherwise See Also: compareTo(String), equalsIgnoreCase(String)"

to the following:

if (noun.equals("dork")) { console.printf("That language is not allowed. Exiting. \n\n"); System.exit(0); }

I just want to know how to interpret this kind of information; as basic as you can please. =)

Thanks in advance.

Max.

Tag: Craig Dennis

Good question!

Let's look again at this line:

public boolean equals(Object anObject)

What that line is saying is that equals is a function that can be called anywhere (public), and that return a true|false value (boolean). It accepts an object as a parameter, and just one object (Object anObject).

Why this way of presenting a method, you may ask. Because a method is a function that belongs to a given type --in this case, String. Let's have a look at the String.java source code for the equals method:

    /* String.equals() */
public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

There you go. The source code presents the usage template right at the beginning, in the first comment. Then you get the rest of the function (which I find very readable and easy to understand). Sometimes going to the source code of the method can be enlightening.

I hope this answers your question. I am quite a beginner myself, and trying to answer your doubt made me learn a few things. :-)

PS: about the object.method notation, see The Java Dot Notation.

Thanks fbenedetti. I'm now on an online voyage of discovery. I'll let you know when I grasp what I need (wish I had more time to study).

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Max Reinsch , not naive at all. This is one of those things that just clicks, and then you won't even remember how you didn't get it at first. It's totally fine. It might just be a language thing.

So we can create a variable called example and store true or false in it like this:

boolean example = true;
// Or 
boolean example = false;

The String.equals method returns a true or false. So we could do this

String word = "hello";
boolean example = word.equals("hello");

The equals method should really be read as is equal to. So "hello" is equal to "hello". Then in example would be stored the value true.

That help clear things up?

Yes, that's starting to make more sense now. So typically if I wanted to store a true or false value inside of a variable then "boolean example = true/false;" is how I would state that? Then, if the returned value matches that of the stored value, the code executes as planned?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Max Reinsch !

First off, sorry about the confusion. Have you seen my video on Errors yet? I just re-watched this and realized that I cut a little deep when doing an edit on my script, I made a pretty big error. Whoops! I will try and fix it, thanks for pointing it out!

Can you let me know if the text below helps you please?

First, the datatype boolean stores true or false. We use that in our conditional if expression. The method String.equals accepts any Object, and what I failed to mention is that Strings are an Object.

Basically I wanted to show here exploration of the documentation, you'll see that afterwards I use it to get to String.equalsIgnoreCase through the See Also section of the documentation.

Essentially that code is this: If the word entered is equal to dork, exit the program.

Does that help?

I think there's a few bits of info I'm missing (either wasn't included or I havn't absorbed it all). I think the main reason I'm having difficulty with, let's say "public boolean equals(Object anObject)" is because of the order it's presented in. I'm so used to the basic "A+B=C" concept that this has thrown me off.

Forgive me for being so naive, it's all a little new to me.

I fully understand a lot of what you've said untill this tutorial.

Perhaps a basic explanation of the order of objects/syntax?

Cheers for getting back to me Craig, your tutorials are great!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher
if (whateverIsInTheseParenthesisIsTrue) {
   //Then this code will run
}
// This code below keeps running as normal.

Starting to make a lot more sense now. I shall put it into practice.

Cheers Craig Dennis and fbenedetti.