Summary of questions in Java Face (i) Summary of __java face questions

Source: Internet
Author: User

1. What is the difference between "= =" and the Equals method?
A: (1) = = operator is designed to compare the value of two variables is equal, that is, used to compare variables in the corresponding memory stored in the same value, to compare the two basic types of data or two reference variables are equal, can only use the = = operator.

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

(2) The Equals method is used to compare the contents of two separate objects to be the same as to compare the appearance of two people, and it compares two objects independently. For example, for the following code:
String A=new string ("foo");
String B=new string ("foo");
The two new statement creates two objects. And then with a,b these two variables point to one of the objects, this is two different objects, their 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 these two objects are the same, so the expression a.equals (b) returns True.
In the actual development, we often want to compare the transmission of the string content, such as, for example, string input = ...; Input.equals ("Quit"), a lot of people do not pay attention to use = = For comparison, this is wrong, random from the Internet to find some of the actual teaching video to see, there are a large number of such errors. Remember, the comparison of strings is basically all about using the Equals method.
If a class does not define the Equals method itself, 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 (Object o) {return
      this==o;
}

This means that if a class does not define the Equals method itself, its default Equals method (inherited from the object class) is to use the = = operator, and to compare whether the object pointed to by two variables is the same object, and then use equals and use = = to get the same result, If two separate objects are compared, the total returns false. If you write a class that wants to be able to compare the contents of the two instance objects created by the class, you must overwrite the Equals method, and write your own code to determine where the contents of the two objects are considered to be the same.

2. Whether a call to a non-static method can be emitted from within a static method.
A: No, because a non-static method is an associated object, you must create an instance object to be invoked by the instance object , and the static method does not need to create the object, which can be called directly by the class , and when Static is invoked, you may not have created any instance objects , if you emit a call to a non-static method from a static method, the non-static method is associated with which object. This logic cannot be established, so calls to a non-static method cannot be emitted internally by a static method.

3.math.round (11.5) How much is it? How much is math.round (-11.5)?
The Math class provides three methods related to rounding: ceil, Floor, round, which correspond to the meanings of their English names, for example, the meaning of the ceil is the ceiling, which means that the method is rounded up, so the Math.ceil (11.3) The result of 12,math.ceil (-11.3) is the -11;floor of the English meaning is the floor, the method means the downward rounding, so Math.floor (11.6) results are 11,math.floor (-11.6) The result is 12 , the most difficult to master is the round method, which means "rounding", the algorithm is Math.floor (x+0.5), the original number plus 0.5 and then down the whole, so, math.round (11.5) The result is 12,math.round (-11.5) The result is-11.

4. What's wrong with the code below?
(1) if (Username.equals ("Zxx") {}: If username is empty, the IF (Username.equals ("Zxx")) {} will report a null pointer exception. Change to: if ("Zxx". Equals (username)) {} is more appropriate.
(2) int x = 1;
return x==1?true:false;
Solution: X==1 is originally a Boolean type, so it is superfluous to follow.

5. Please state the scope of the public,private,protected, and do not write when (friendly) the difference

Scope    Current Class The same package descendants class other package 
public     √      √ 
protected √ √         x Friendly √ x x 
Private    √      x         x x 

the difference between 6.Overload (overloaded) and override (Overrides). (important)
Overloaded overload means that there can be more than one method with the same name in the same class, but the argument lists of these methods are different (that is, the number or type of arguments is not the same);
overriding override means that a method in a subclass can be exactly the same as the name and parameters of a method in the parent class;

The following points should be noted in the overlay:
1. The marking of the covering method must match the mark of the method covered, so as to reach the effect of coverage;
2. The return value of the overridden method must be consistent with the return of the overridden method;
3. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4, the overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

7. Talk about the session in Java EE Understanding, how you use the session.
On the server, the session is used to differentiate each Internet user; The user assigns a session to the user as soon as one connects to the server; the server distinguishes each user by its session ID , the GetAttribute and setattribute of the session can verify the client's landing, but this method is outdated;
Here's a bit more, the session saves the information on the server, and the cookie is saved on the client, the session is more secure than the cookie, and the session is more resources than the cookie; Development principles: Session to be as small as possible – Try to keep less information in session.

the life cycle of the 8.Servlet.

The Servlet lifecycle can be defined as the entire process from creation through destruction. The following procedures are followed by the servlet: the
servlet initializes by invoking the Init () method.
the Servlet invokes the service () method to handle the client's request. The
Servlet terminates (ends) by calling the Destroy () method.
Finally, the Servlet is garbage collected by the JVM's garbage collector.

9. The benefits of tiered design.

The benefits of layering:
        1, to achieve decoupling between the software;
        2. Facilitate the division of Labor
        3. Easy to maintain
        4, improve software component reuse
        5. Easy to replace a product, such as a durable layer with hibernate, Need to replace the product with TopLink,
          there is no need to change other business code, directly to change the configuration.
        6. Facilitate the expansion of product features.
        7. Easy to adapt to the changing needs of users

10.

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.