Java Interview Cookbook ———— Java Basics section (ii)

Source: Internet
Author: User

What is the difference between the "= =" and the Equals method?

(make one thing clear and then another, so that the difference will come out naturally, and it's hard to say that it's not clear) the= = operator is specifically used to compare the values of two variables for equality , That is, the memory used to compare variables is stored in the same value, to compare two basic types of data or two reference variables are equal, can only use the = = operator .

If the data that a variable points to is an object type, then it involves two blocks of memory, the object itself occupies a chunk of memory ( heap memory ), and the variable occupies a piece of memory, such as Object obj = new Object (), and the variable obj is a memory , new Object () is another memory, at which point the value stored in memory for the variable obj is the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator.

The equals method is used to compare the contents of two separate objects , as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:

String a = new string ("foo");

String b = new string ("foo");

Two new statements create two objects, and then use a/b to point to one of the objects, which is two different objects whose first address is different, that is, the values stored in a and B are not the same , so the expression a==b will return False, The contents of the two objects are the same, so the expression a.equals (b) returns True.

In real-world development, we often want to compare whether the string content passed in is equal, for example, string input = ...;

Input.equals ("Quit"), many people do not pay attention to use = = to compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, there are a lot of such errors. Remember that string comparisons are basically using the Equals method. If a class does not define its own Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows:

Boolean equals (Obejct o) {

This==o;

}

This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same.

12. What is the difference between a static variable and an instance variable?


13. Whether a call to a non-static method can be issued from within a static method


14. What is the difference between integer and int?


XV, Math.Round (11.5) equals what? How much does Math.Round (-11.5) equal?


16. What's wrong with the code below?


17, please say the scope public,private,protected, and do not write when the difference


18, the difference between overload and override. Can the overloaded method change the type of the return value?


19. Can the constructor constructor be override?


20, interface can inherit interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete Class)? Can I have a static main method in an abstract class?

Java Interview Cookbook ———— Java Basics section (ii)

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.