"Java Learning Notes" review of Baidu interview (I.)

Source: Internet
Author: User

Today, we reviewed some of the questions asked by Baidu during the interview:

1. What are the common packaging categories and what are the differences between them and the basic types?

The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which in the actual use of a lot of inconvenience, in order to solve this shortcoming, in the design of the class for each basic data type to design a corresponding class to represent, The classes that correspond to the eight basic data types are collectively referred to as wrapper classes (Wrapper Class), and some are translated as outer or data type classes.


Basic type Byte Boolean Short Char Int Long Float Double
Packing class Byte Boolean Short Character Integer Long Float Double
Each of the eight basic data types has a corresponding wrapper class. The difference: The basic type is not object-oriented, exists in the stack, the wrapper class is object-oriented, there are object operations, there are corresponding methods for use, such as String to integer type. The main function of wrapper class: wrapper class Convenient object operation, contains the maximum value of each type of minimum value maxvalue,minvalue For example, some very convenient type conversion methods in integer
Method int Str.parseint () String i.tostring () int I.intvalue () Integer (int i) I.valueof (String s)
Type transformation String→int Integer→string Integer→int Int→integer String,int→integer
In addition, a cast is equivalent to a method that uses a wrapper class.
Double Dou; int i = (intnew  Double (dou); int i = Dou_obj.intvalue ();

Is there a difference between equals () and "= =" in 2.String? Please explain

Different, the former compares the reference, the latter compares the content. String overrides the Equals () method in the object class, comparing the contents of the string, and "= =" returns true only if the reference points to the same string object, and "= =" is stricter than equals ().

You can see the following example of overriding Equals () in the String class:

 Public Booleanequals (Object anobject) {if( This==anobject) {        return true; }    if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =count; if(n = =anotherstring.count) {CharV1[] =value; CharV2[] =Anotherstring.value; inti =offset; intj =Anotherstring.offset;  while(n--! = 0) {            if(v1[i++]! = v2[j++])            return false; }        return true; }    }    return false;} 

What are the 3.Object class methods?

    • ToString (): Returns the class name of the object @ hash code. General recommended overloads.
GetClass (). GetName () + "@" + gethexstring (Hashcode ());
    • Hashcode (): Returns the hash encoding of the object.

Each object in Java is represented by a hashcode, and the JVM maintains a hash table (hashTable), which is then addressable by the JVM through the hash code in the table, so that the object represented by the hash code can also be used to find the object

    • Equals (): Compares objects for equality.

The traditional "= =" comparison, is the comparison of the object reference is equal, the reference is the object's Hashcode (), so generally unequal, but if we want to compare two objects themselves, here the object class provides a method of equality of the object itself, in the object class itself equals () It is written in the following kind of notation:

public boolean equals (Object obj) {    return (this = = obj);} 

It's actually the same as the "= =" Comparison we wrote, and then someone would ask equals () What's the use? In fact, I feel that the Java designer has provided us with a way to compare custom objects, and in the API it says that Object the equals method of the class implements the most differentiated equality relationship on the object: When and only if two non-null references x, y reference the same object, To return True. Then we can actually rewrite this method to compare their content.

Also, note that the API documentation says that if we rewrite the Equals () method, it is best to rewrite the Hashcode () method, because if two objects are equal according to the equals (object) method, then each object in both objects is called hashCodemethod must produce the same integer result, if two objects are not equal according to the equals (object) method, they hashCode do not require unequal calling methods, but for hash tables, the performance is best unequal. Therefore, we'd better rewrite.

As mentioned earlier, the String class overrides the Equals () method, but the string class also overrides the Hashcode () method, which is the hashcode () generated from the string content;

//Hash is a hashed code//count is a string length//Offset is a displacement//Value is the character array value of the string Public inthashcode () {inth =Hash; intLen =count; if(h = = 0 && len > 0) {        intOff =offset; CharVal[] =value;  for(inti = 0; i < Len; i++) {h= 31*h + val[off++]; } Hash=h; }    returnh;}

Therefore, the following conclusions are drawn:

① two obj, if equals () is equal, hashcode () must be equal

② two obj, Equals () is not necessarily equal if hashcode () is equal

And then there are:

What is 4.Set, List, and map? Their characteristics and what methods they have.

5.String, StringBuffer, StringBuilder the difference?

To be Continue ...

Java Learning Notes Baidu interview question review (a)

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.