Six big questions that Java beginners have to understand

Source: Internet
Author: User
Tags comparison object object reference

For the problems in this series, everyone who learns Java should understand. Of course, it doesn't matter if you just learn to play Java. If you think you have gone beyond beginners, but do not understand these questions, please return yourself to the ranks of beginners.

Question one: What have I declared!

String s = "Hello world!";

A lot of people have done this, but what are we declaring? The answer is usually: a string with the content "Hello world!". Such vague answers are often the source of unclear concepts. If you want to answer correctly, half of the people will probably answer the wrong question.

This statement declares a reference to an object named "s" that can point to any object of type string, currently pointing to "Hello world!" The object of this string type. That's what's really going on. We didn't declare a string object, we just declared a reference variable that could only point to a string object. So if, after that sentence, you run another sentence:

String string = s;

We're declaring another reference that can only point to a String object, named String, and no second object is generated, string or point to the original object, that is, and s points to the same object.

Question two: What is the difference between "= =" and the Equals method?

The = = operator is specifically used to compare variable values for equality. The better understanding is that:

int a=10;
int b=10;

Then A==b will be true.

But what's not good to understand is:

String a=new String("foo");
String b=new String("foo");

The a==b will return false.

According to the previous post, object variables are actually references whose values point to the memory address where the object resides, not the object itself. Both A and B use the new operator, which means that there will be two strings in memory that are "Foo", and since they are "two", they naturally reside in different memory addresses. The value of a and B is actually a value of two different memory addresses, so using the "= =" operator will result in false. Admittedly, the objects of A and B are all "foo" and should be "equal", but the = = operator does not involve comparison of object content.

The comparison of object content is exactly what the Equals method does.

Look at how the Equals method of Object objects is implemented:

boolean equals(Object o){
return this==o;
}

The Object object uses the = = Operator by default. So if your own class doesn't overwrite the Equals method, then your class uses equals and uses = = to get the same result. It can also be seen that the Equals method of object does not achieve the goal that the Equals method should achieve: Compare two object contents for equality. Because the answer should be determined by the creator of the class, object leaves the task to the creator of the class.

Take a look at an extreme class:

Class Monster{
private String content;
...
boolean equals(Object another){ return true;}
}

I covered the Equals method. This implementation causes the comparison between them to always return true regardless of the monster instance content.

So when you're using the Equals method to determine whether the object's content is equal, don't take it for granted. Because you may think of equality, and the author of this class does not think so, and the implementation of the Equals method of the class is his master. If you need to use the Equals method, or use any collection based on the hash code (hashset,hashmap,hashtable), look at the Java doc to see how the Equals logic for this class is implemented.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.