Java Interview--01

Source: Internet
Author: User

001.What is the difference between "a==b" and "A.equals (b)"? What's the use of A.hashcode ()? What is the relationship with A.equals (b)?

The default equals implementation of object: A.equals (b) and a==b have the same result, that is, the default implementation will return TRUE if only A and B point to the same object. where string overrides equals to return true as long as string has the same character series.

If both A and B are objects, a==b is a reference to compare two objects, and only if A and B point to the same object in the heap to return true, whereas A.equals (b) is a logical comparison, it is often necessary to override the method to provide a comparison of logical consistency. For example, the String class overrides the Equals () method, so it can be used for two different objects, but contains the same letters as the comparison.

The Hashcode () method is the hash value of the corresponding object integral type. It is often used for hash-based collection classes such as Hashtable, HashMap, Linkedhashmap, and so on. According to the Java specification, two objects that use the equal () method to determine equality must have the same hash code.

Note:

①hash code is a way of encoding, and in Java each object has a hashcode,java that can be used to identify an object by this hashcode.

②equlas are mostly used to make string comparisons, and to determine the basic data type or object type, you need to use = =

002. Strings=new string ("xyz"), how many string objects have been created?

First run: Two: In a String constant pool, if there is no "XYZ" object, an "XYZ" object and a new String ("XYZ") object are created.

Strng a = "xyz";

string s = new string ("XYZ");

Second run: one: In a String constant pool, if an "XYZ" Object already exists, no more "XYZ" objects are created and only the new string ("XYZ") object is created;

Java Interview--01

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.