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