Java interview questions (basic part)

1、Can a.Java source file include multiple classes (not internal classes)? What are the restrictions?  

  There can be multiple classes, but only one public class, and the public class name must be consistent with the file name.

2、JavaDo you have goto?

  javaThe reserved word is not used in Java now.

3、Let’s talk about the difference between &amp and & &amp.  

  &And & amp; & amp; can be used as logic and operators, representing logic and (and), when the result of the expression on both sides of the operator is true, the entire operation result is true, otherwise, as long as one side is false, the result is false.

  &&It also has the function of short-circuiting, that is, if the first expression is false, the second expression is no longer evaluated, for example, for if (str! = null & amp; & amp; str. equals (“”)) expressions, when STR is null, afterThe expression of the facet does not execute, so there is no NullPointerException that throws a NullPointerException if & amp; & amp; is changed to & amp;. If (x==33 & +)+y> 0) y will increase, If (x==33 & & ++y> 0) will not grow.

  &It can also be used as a bit operator, when the expressions on either side of the & amp; operator are not Boolean types, & amp; denotes bitwise and manipulation, and we usually use 0x0f to do & amp with an integer; to get the lowest four bits of the integer, for example, 0x31The result of &amp and 0x0f is 0x01.  

4、switchCan sentence function on byte, can it work on long, and can it work on String?

  In switch (expr1), expr1 can only be an integer expression or an enumeration constant, and an integer expression can be an int primitive type or an Integer wrapper type, because byte, short, char can be implicitly converted to an int, so thisSome types and types of packaging are also acceptable. Obviously, long and String types do not conform to the syntax of switch and cannot be implicitly converted to int types, so they do not work in swtich statements.  

5、short s1 = 1; s1 = s1 + 1;What’s wrong? Short S1 = 1; S1 + = 1; what’s wrong?

  For short S1 = 1; S1 = S1 + 1; because the S1 + 1 operation automatically elevates the type of the expression, the result is int, and when assigned to short type s1, the compiler reports an error that requires a cast type.

  For short S1 = 1; S1 + = 1; and because + = is an operator specified in the Java language, the java compiler does special processing on it, so it compiles correctly.  

6、charCan a Chinese character be stored in a type variable? Why?

  charThe type variable is used to store Unicode-encoded characters. The unicode-encoded character set contains Chinese characters, so, of course, char-type variables can store Chinese characters. However, if a particular Chinese character is not included in the Unicode coded character set, then this CThis special character cannot be stored in the har variable. Additional note: Unicode encoding takes up two bytes, so variables of type char also take up two bytes.

7、When final keyword is used to modify a variable, can it be changed or can not be changed?  

  When you modify a variable with the final keyword, it means that the reference variable cannot be changed, and the contents of the object to which the reference variable points can be changed.

  For example, for the following statement:

     final StringBuffer a=new StringBuffer(“immutable”);
  The following statement is used to compile the report error:

    a=new StringBuffer(“”);
  However, the following statements can be compiled through:

    a.append(” broken!”); 

  Someone who defines a method’s parameters may want to prevent the method from modifying the incoming parameter object internally as follows:

    public void method(final  StringBuffer  param){

    } 

  In fact, this is not possible, and you can still add the following code inside the method to modify the parameter object:

    param.append(“a”);

8、”==”What is the difference between equals and the method?

  “==”Operators are designed to compare the values of two variables to be equal, that is, to compare the values stored in the memory corresponding to the variable to be the same, to compare two basic types of data or two reference variables to be equal, you can only use the == operator.

  If the data a variable points to is of object type, then two blocks of memory are involved, the object itself occupies a block of memory (heap memory), and the variable occupies a block of memory (stack memory), such as Objet obj = new Object (); the variable obj is a memoryNew Object () is another memory where the value stored in the memory corresponding to the variable obj is the first address of the block of memory occupied by the object. For variables that point to object types, if you want to compare whether two variables point to the same object, it depends on the inside of the two variablesWhether the value in the storage is equal, we need to compare it with the = = operator.

  equalsThe method is used to compare the content of two independent objects is the same, as to compare the appearance of two people is the same, it compares the two objects are independent. For example, for the following code:

  String a=new String(“foo”);

  String b=new String(“foo”);

  Two new statements create two objects, and then point to one of them with a and B variables, which are two different objects with different first addresses, that is, values stored in a and B are not the same, so the expression a = = B returns false, and these two objectsThe content is the same, so the expression a.equals (b) will return true.

  In actual development, we often compare the content of the string passed in, for example, String input = etc. ; input. equals (“quit”), many people use == for comparison without paying attention, which is wrong, remember, stringThe comparison is basically based on the equals method.

  If a class does not define its own equals method, it inherits the equals method of the Object class. The implementation code of 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 its own equals method, its default equals method (inherited from the Object class) is to use the == operator, but also to compare whether two variables point to the same object, using equals and using === will get it?To the same result, if two independent objects are compared, they always return to false. If you write a class that wants to be able to compare the contents of two instance objects created by that class, you must override the equals method, and you write your own code to decide in what case two pairs can be considered.The content of the elephant is the same.

9、The difference between static and instance variables?

  The difference between syntax definitions is that the static key is added before static variables, but not before the instance variables.

  The difference between program runtime: instance variables belong to the properties of an object, and must be created, where instance variables are allocated space to use this instance variable. Static variables do not belong to an instance object, but to a class, so they are also called class variables, as long as the program loads the bytes of the classCode, without creating any instance objects, static variables are allocated space, and static variables can be used. In short, instance variables must be created before they can be used through this object, and static variables can be referenced directly by class names.

  For example, for the following program, no matter how many instance objects are created, only one staticVar variable is always assigned, and for each instance object created, the staticVar is added by 1; however, for each instance object created, an instance V is assignedAR, that is, multiple instanceVar may be allocated, and the value of each instanceVar is added only 1 times.

public class VariantTest{

  public static int staticVar = 0; 

  public int instanceVar = 0; 

  public VariantTest(){

    staticVar++;

    instanceVar++;

    System.out.println(“staticVar=” + staticVar + ”,instanceVar=” + instanceVar);

  }

}

10、Can you call a non static method from within a static method?

  May not. Because non-static methods are associated with objects, you must create an object before you can make method calls on that object, and static method calls can be called directly without creating objects. That is to say, when a static method is called,Perhaps no instance objects have been created yet. If a call to a non-static method is made from a static method, which non-static method is associated with? This logic can not be established, so a static method can not internally generate a pair of non ST.The invocation of the atic method.

11、IntegerDifference from int

  intIt is one of 8 original data types provided by Java. Java provides encapsulation classes for each primitive type, and Integer is the encapsulation class provided by Java for int. The default value of int is 0, while the default value of Integer is null, that is, Integer can be distinguished.If there is no difference between an unassigned and a zero, an int cannot express an unassigned situation. For example, if you want to express the difference between an unassigned and a zero, you can only use Integer. In JSP development, the default of Integer is null, so El expression is used in text.When displayed in this box, the value is a blank string, and the default value of int is 0, so when displayed in the text box with an EL expression, the result is 0, so int is not suitable for the type of form data in the web layer.

  In Hibernate, if OID is defined as an Integer type, then Hibernate can determine whether an object is temporary based on whether its value is null or not, and if OID is defined as an int type, it needs to be set in the HBM mapping file.Its unsaved-value attribute is 0.

  In addition, Integer provides several integer-related operations, such as converting a string into an integer, and defines constants that represent the maximum and minimum of an integer.

12、Please tell the difference between scope public, private, protected, and not writing.

The visible scope of these four scopes is shown in the table below.

Note: if there is no access modifier on the modified element, it means friendly.

13、OverloadThe difference from Override. Can the method of Overloaded change the type of return value?

  OverloadOverride is the meaning of overloading, that is to say, rewrite.

  Overloaded means that there can be multiple methods with the same name in the same class, but the list of parameters for these methods varies (that is, the number or type of parameters is different).

  Override represents that a method in a subclass can have exactly the same name and parameters as a method in a parent class. When called through an instance object created by a subclass, the method defined in the subclass is called, which is equivalent to overwriting the exact same method defined in the parent class. This is also trueA representation of polymorphism in object programming. When a subclass overrides a method of the parent class, it can only throw fewer exceptions than the parent class, or child exceptions of the exception thrown by the parent class, because the subclass can solve some of the problems of the parent class and can not have more problems than the parent class. The access permission of subclass methods can only be more than that of the parent class.Big, can’t be smaller. If the method of the parent class is private, then the subclass has no override restriction, which is equivalent to adding a completely new method to the subclass.

  The question of whether the Overloaded method can change the type of return value depends on what you want to ask. This topic is very vague. If the parameter lists of several Overloaded methods are different, of course, their returner types can be different. But I guess you want to.The question is: If the parameter lists of the two methods are exactly the same, can you overload the Overload with different return values? This is not possible, and we can use a counterargument to illustrate this, because we sometimes call a method without defining the return result variable, that is, noFor example, when we call the map. remove (key) method, although the remote method has a return value, we usually don’t define the variable that receives the return result, assuming that there are two methods in the class with exactly the same name and parameter list, justWith different return types, Java can’t determine which method the programmer actually wants to call, because it can’t tell by the return result type.  

  overrideIt can be translated as coverage, literally, covering a method and rewriting it in order to achieve a different effect. The most familiar coverage for us is the implementation of interface methods, which are generally declared only in the interface, and we need to implement the interface declaration when we implement itAll methods. In addition to this typical usage, we may also override methods in the parent class in inheritance. In overwriting, note the following:

  1、In order to achieve the effect of coverage, the mark of the covered method must match the mark of the covered method.

  2、The return value of the covering method must be consistent with the return of the covered method.

  3、The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or its subclass.

  4、The overridden method can’t be private, otherwise it just defines a new method in its subclass and doesn’t overwrite it.

  overloadIt means that we can define methods with the same name, distinguish them by defining different input parameters, and then call them, and the VM will choose the appropriate method to execute according to different parameter styles. Attention should be paid to using overload.A few points:

  1. You can only use different parameter styles when using heavy loads. For example, different parameter types, different number of parameters, different order of parameters (of course, several parameter types within the same method must be different, such as fun (int, float), but not fun (int, i)NT))
  2. Overloading can not be done through access permission, return type, and thrown exception.
  3. The exception type and number of methods will not affect overloading.
  4. For inheritance, if a method is priavte in the parent class, then it cannot be overloaded in the subclass, and if it is defined, it simply defines a new method without overloading.

14、has aThe difference from is a

  is-aIt represents the relationship of belonging. For example, rabbits belong to a kind of animal (inheritance relationship).

  has-aRepresenting combinations, including relationships. For example, rabbits contain legs and first class components.

15、ClassLoaderHow do I load class?

  jvmThere are multiple classloaders, each of which can be responsible for loading classes in a particular location. For example, bootstrap classloads are responsible for loading classes in JRE / lib / RT. jar, and classes in our usual JDK are located in RT. jar. ExtclassloaThe der is responsible for loading the classes in jar / lib / ext /*. jar, and the appclassloader is responsible for the directory specified in the classpath or the classes in the jar. In addition to bootstrap, other class loaders are also Java classes themselves.The parent class is ClassLoader.

16、Benefits of layered design

  The advantage of modularization is that if you want to register a user, the process receives the user’s input through the display interface, then processes the business logic, and then accesses the database while processing the business logic. If we want to register a user, the process receives the user’s input through the interface.It’s okay to have all these steps written in one method as a pipelining account, but the downside is that when the interface is modified, because the code is all in one method, the business logic and database access code can be broken, as well as when the business logic or database access code is modifiedBreak the code of other parts. Layering is to write the code for the interface, business logic, and database access sections in separate methods or classes, so that there is no one-stop problem. After this layering, you can also switch layers easily, for example, the original interface is Swin.G, now to change to BS interface, if initially designed according to layers, this time does not need to involve business and data access code, just write a web interface can be.

  Benefits of stratification:

  1.The decoupling between software is realized.

  2.Facilitate division of labour

  3.Easy to maintain

  4.Improving reuse of software components

  5.Easy to replace a product, such as the persistence layer with hibernate, need to replace the product with toplink, do not need the other business code, directly change the configuration.

  6.Facilitate the expansion of product functions.

  7.Easy to adapt to changes in user needs.

17、hashCodeThe role of the method?

  hashcodeThis method is used to identify whether the 2 objects are equal.

  equalsThe hashCode method and the hashCode method are both used to determine whether two objects are equal, but they are different.

  In general, equals is called by the user. If you want to determine whether two objects are equal, you can override the equals method and call it in the code to determine if they are equal. In short, the equals method is mainly used to judge from the surface orFrom the content point of view, the 2 objects are not equal. For example, if there is a student class whose attributes are only name and gender, then we can say that the two objects are equal as long as the name and gender are equal.

  hashcodeIn hashmap, for example, because the key is not repeatable, he judges the hashcode method when he decides whether the key is repeatable, and uses the equals method. Equals can not be repeated here.Hashcode as long as there is an inequality. So in a nutshell, hashcode is the encoding of an object, like MD5 in a file, and unlike equals, he returns an int, which is less intuitive. We usually cover equALS also needs to cover hashcode to make their logic consistent. For example, just now, the hashcode method also returns the hashcode value of the name plus the hashcode value of the sex if the name and gender are equal even if two objects are equalOde, so logically, they agree.

  To physically determine whether two objects are equal, use ==. If the physical (memory) addresses of two objects are equal, then the two objects must be the same object.

18、What is AOP?

1.AOPConcept introduction

  The so-called AOP, or Aspect orientied program, is aspect oriented programming.

  Aspect-Orlented-Programming, or AOP, is a powerful complement to object-oriented thinking.

  AOPThe advantage is that you can dynamically add and delete logic on the section without affecting the original execution code.

2.Explain what aspect (aspect) is.

  Aspects (facets) refer to one aspect (facets) of the system that runs through the various modules of the system. For example, logging, unified exception handling, transaction processing, permission checking, these functions are one aspect of the software system, not a point, in each module.

3.What is aspect oriented programming?

  Encapsulating the function of one aspect of the system into object form is aspect oriented programming.

4.How to do aspect oriented programming

  The object corresponding to the function module is embedded into the original system module as a facet. Using the proxy technology, the proxy will call the target and add the code (object) of the facet function. So, when configuring the proxy object with spring, only two attributes are needed to represent the object and the tangent object respectively.Advisor).

 19.Talk about your understanding of MVC

  MVCIt is the abbreviation of Model – View – Controler. That is the model view controller. MVC is a design pattern that separates the input, processing and output of the application.

  MVCThe models, views, and controllers in the system undertake different tasks respectively.

  • View: view is an interface that users see and interact with. The view displays relevant data to the user and accepts user input. The view does not carry out any business logic processing.
  • Models: models represent business data and business processes. It’s equivalent to JavaBean. A model can provide data for multiple views. This improves the reusability of applications.
  • Controller: When the user clicks the submit button in the Web page, the controller accepts the request and invokes the corresponding model to process the request. Then the corresponding view is called according to the result of the processing to display the result of the processing.

  MVCProcessing process: First, the controller accepts the user’s request, calls the corresponding model for business processing, and returns the data to the controller. The controller invokes the corresponding view to display the result of the process. And view it to the user.

 

Leave a Reply

Your email address will not be published. Required fields are marked *