Six big questions that Java beginners must understand <1>

Source: Internet
Author: User

Six big questions that Java beginners must understand

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

Question one: What did I declare!

String s = "Hello world!";

Many people have done this kind of thing, but what exactly do we declare? The answer is usually: a string, the content is "Hello world!". Such vague answers are often the source of unclear concepts. If you want an accurate answer, half of the people will probably answer the wrong question.

Shenyang 463 Plastic Surgery Hospital This statement declares a reference to an object named "S", which can point to any object of type string, currently pointing to "Hello world!" The object of this string type. This is what really happens. We do not declare a string object, we simply declare a reference variable that can only point to a string object. So, if after that sentence, if you run another sentence:

string string = S;

We are declaring another reference that can only point to a String object, named String, and no second object is generated, and string is pointing to the original object, that is, to the same object as S.

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

The = = operator is specifically used to compare the values of variables for equality. A better understanding is:

int a=10;

int b=10;

Then A==b will be true.

But the hard part of understanding is:

String A=new string ("foo");

String B=new string ("foo");

Then A==b will return false.

According to the previous post, the object variable is actually a reference, and their value is pointing to the memory address where the object resides, not the object itself. Both A and B use the new operator, which means that two strings of "Foo" will be generated in memory, and since they are "two", they naturally reside in different memory addresses. The values of A and B are actually two different memory address values, so using the "= =" operator, the result will be false. Admittedly, the objects referred to in A and B are "foo" and should be "equal", but the = = operator does not involve comparisons of object content. Shenyang 463 Plastic Surgery Hospital http://www.hengnaya.com/

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

Take a look at how the Equals method for object objects is implemented:

Boolean equals (Object o) {

return this==o;

}

The = = operator is used by default for object objects. So if your own class does not overwrite the Equals method, then your class will get the same result using equals and using = =. It is also possible to see that the Equals method of object does not meet the goal of the Equals method: to compare the contents of two objects for equality. Because the answer should be determined by the creator of the class, object leaves the task to the creator of the class.

Look at an extreme class:

Class monster{

Private String content;

...

Boolean Equals (Object another) {return true;}

}

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

So don't take it for granted when you're using the Equals method to determine whether the object's contents are equal. 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 in his hands. If you need to use the Equals method, or use any hash-based collection (hashset,hashmap,hashtable), look at the Java doc to see how the Equals logic of this class is implemented.

Six big questions that Java beginners must understand <1>

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.