Java The Thing About Strings

Eugene Paitoo
Eugene Paitoo
1,727 Points

A little help needed

I'm totally lost right from the beginning to the end of this video. i didn't get the whole video and please a little help will be appreciated.

Thanks very much! :)

1 Answer

michaelcodes
michaelcodes
4,368 Points

Hi there! I think the way that the concept is explained in the video is a bit confusing.

A couple of the main takeaways are that: It is generally better practice to use the .equals method rather than double equal signs (==) to check for equality. The reason for this is because in java the == does NOT check for equality when dealing with objects, only with primitives (integers, etc). All objects inherent from a generic object (known as "object"), meaning all objects can use the .equals method by default.

Even objects that you make yourself inherit from the generic object. There are many methods that objects inherit from the generic object, but for right now we are only concerned about the .equals method.

If you were to use:

object1 == object2

This checks that the objects reference the same location in memory.. This is most likely not what we are looking for (and will return false unless they are the SAME object) if we are trying to compare some value. To check for equality we use:

object1.equals(object2)

I hope this helps a bit, these are some main points the video was trying to convey, if you have any other questions don't hesitate to ask! Also don't feel discouraged if you don't immediately understand it, I know it can be confusing and digesting it over a couple days really helps! Take care and happy coding :)

Pedro Cabral
Pedro Cabral
Java Web Development Techdegree Student 4,513 Points

I would also like to add that the benefit comes from overriding the .equals method with the author's own notion of equality, otherwise the method just ends up behaving like a "==" since it returns true if the calling object is the same as the object passed into the argument. Also, I believe the word "thing" in the topic of the workshop, refers to the String pooling/interning which is a different thing all together. But understanding the concept that michaelcodes wrote comes first as "the thing about Strings" (pooling/interning) builds upon that.

Eugene Paitoo
Eugene Paitoo
1,727 Points

Thanks very much. i'm still a code newbie though so sometimes getting a hold these stuff takes time, i don't know how you guys did it to reach so level of proficiency but anyways I'm much grateful for the answer. :)