Interview Questions Collection--java basic part (i)

Source: Internet
Author: User
Tags bitwise wrapper

1. Can I include more than one class (not an inner class) in a ". Java" source file? What are the restrictions?

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

2. Does Java have goto?

  Reserved words in Java are not currently used in Java.

3, talk about the difference between & and &&.

  Both & and && can be used as the logical AND operator, representing logic and (and), when the result of an expression on either side of the operator is true, the entire result is true, otherwise the result is false if one of the parties is false.

  && also has a short-circuit function, that is, if the first expression is false, the second expression is no longer evaluated , for example, for an if (str! = null &&!str.equals ("")) expression, When STR is NULL, subsequent expressions do not execute, so the nullpointerexception will not appear if you change && to & NullPointerException exception will be thrown. if (x==33 & ++y>0) y grows, if (x==33 && ++y>0) does not grow

  & can also be used as a bitwise operator, when an expression on either side of the & operator is not a Boolean,,& represents bitwise AND Operation , and we typically use 0x0f to perform a & operation with an integer to get the minimum 4 bit bits of that integer, for example, 0x31 & 0x0f The result is 0x01.

4. Can the switch statement function on a byte, can it function on a long string?

  In switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant, an integer expression can be an int primitive type or an integer wrapper type , because, Byte,short,char can be implicitly converted to int, so, These types, as well as these types of wrapper types, are also available. Obviously, long and string types do not conform to the syntax of switch, and cannot be implicitly converted to int types, so they cannot be used 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 type of the expression is automatically promoted by the s1+1 operation, the result is of type int, and when assigned to the short type S1, the compiler reports an error that requires a cast type.

  for short S1 = 1; S1 + = 1; since + = is a Java-language-defined operator, the Java compiler will treat it specially, so it compiles correctly.

6. Can I store a Chinese character in char type?

  char-type variables are used to store Unicode encoded characters, and the Unicode encoding character set contains Chinese characters, so the char type can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character . Supplemental Note:Unicode encoding consumes two bytes, so variables of type char are also two bytes.

7. When a variable is decorated with the final keyword, is the reference immutable or does the referenced object not change?

  When you use the final keyword to decorate a variable, it means that the reference variable cannot be changed, and the contents of the object to which the reference variable is pointing can be changed.

For example, for the following statement:

Final StringBuffer a=new StringBuffer ("immutable");
Executing the following statement will report a compile-time error:

A=new StringBuffer ("");
However, the following statements can be executed by compiling:

A.append ("broken!");

When someone defines a method's parameters, you might want to block the method from modifying the passed-in Parameter object in the following form:

public void method (final stringbuffer param) {

}

In fact, this cannot be done, and the following code can still be added inside the method to modify the Parameter object:

Param.append ("a");

8. What is the difference between "= =" and Equals method?

  the "= =" operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare variables is the same, to compare two basic types of data or two reference variables are equal, only with the = = operator.

  if the data that a variable points to is an object type, then two memory is involved, the object itself occupies a chunk 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 memory, and new object () is another memory, at which point the value stored in memory for the variable obj is the first address of the memory that the object occupies. for variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator .

  The equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:

  String A=new string ("foo");

String B=new string ("foo");

Two new statements create two objects, and then use a, B, to point to one of the two variables, which 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, and the contents of the two objects are the same. Therefore, the expression a.equals (b) returns True.

In real-world development, we often have to compare whether the string content passed in is, for example, string input = ...; Input.equals ("Quit"), many people do not pay attention to using = = to compare, this is wrong, remember, string comparison is basically using the Equals method.

If a class does not define its own Equals method, 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 its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same.

9. What is the difference between a static variable and an instance variable?

  Differences in syntax definitions: Static variables should be added before keyword, which is not added before the instance variable .

  The difference when the program runs: instance variables belong to an object's properties, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used . In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name.

For example, for the following program, no matter how many instance objects are created, only one Staticvar variable is always assigned, and each instance object is created, the Staticvar adds 1; However, each instance object is created, a Instancevar is assigned. That is, multiple instancevar may be assigned, and each Instancevar value is only added 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. Is it possible to make a call to a non-static method from within a static method?

No. Because the non-static method is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly . That is, when a static method is called, there may not be any instance objects created, and if a call to a non-static method is emitted from a static method, which object does that non-static method relate to? This logic cannot be established, so a static method cannot emit a call to a non-static method inside.

11, the difference between integer and int

  Int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, and integer is the wrapper class provided by Java for Int. the default value for int is 0, and the default value of integer is null, that is, the integer can distinguish between unassigned and a value of 0, and int cannot express an unassigned condition , for example, to express the difference between not taking a test and a test score of 0, You can only use integer. In JSP development, the default for integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int is 0, so the result is 0 when displayed in a text box with an El expression, so int does not work together as a type of form data for the Web tier.

In Hibernate, if the OID is defined as an integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined for the int type, It is also necessary to set its Unsaved-value property to 0 in the HBM mapping file.

In addition, the integer provides multiple integer-related action methods, such as converting a string to an integer, and also defining constants that represent the maximum and minimum values of integers.

12, please say the scope public,private,protected, and do not write when the difference

The visible range of these four scopes is shown in the following table.

Description: If no access modifier was written above the decorated element, the friendly is represented.

13, the difference between overload and override. Can the overloaded method change the type of the return value?

  overload is the meaning of overloading, override is the meaning ofoverrides , that is, rewriting .

  Overloaded overload means that there can be more than one method with the same name in the same class, but the parameter lists of these methods vary (that is, the number of arguments or the type differs).

  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, and invoking the method from an instance object created by the subclass invokes the definition method in the subclass, which is equivalent to overwriting the exact same method defined in the parent class. This is also a representation of the polymorphism of object-oriented programming. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The subclass method can only be accessed more than the parent class, and cannot be smaller. If the method of the parent class is private, then the subclass does not have an override limit, which is equivalent to adding a new method to the subclass.

As to whether the overloaded method can change the type of return value, it depends on what you want to ask. The subject is very vague. If several overloaded methods have different parameter lists, their type of returnees can of course be different. But I guess the question you want to ask is: if the parameter list of the two methods is exactly the same, can you make them different from the return values to implement the overloaded overload? This is not possible, we can use contradiction to illustrate this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return results , for example, when we call the Map.Remove (key) method, although the Remove method has a return value, However, we usually do not define a variable to receive the return result, when it is assumed that the class has two names and the parameter list is exactly the same method, only the return type is different, Java will not be able to determine the programmer's bottom is to invoke which method, because it can not be judged by the return result type.

  Override can be translated as an overlay, literally knowing that it is covering a method and rewriting it to achieve different effects. the most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, and we need to implement all the methods of the interface declaration when we implement it . In addition to this typical usage, we may also override the methods in the parent class in the subclass, overriding the following points in the overlay:

  1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;

2. The return value of the overridden method must be the same as the return of the overridden method;

3. The exception that is 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.

  overload may be familiar to us, can be translated as overloaded , it means that we can define some of the same name, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, To select the appropriate method to execute. the following points are to be noted in using overloading:

    1. you can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, such as can be Fun (int,float), but not fun (int,int));
    2. cannot be overloaded by Access permission, return type, thrown exception;
    3. The exception type and number of methods do not affect overloading;
    4. For inheritance, if a method is priavte in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.
14, has a and is a difference

  Is-a represents a relationship of its own. Rabbits, for example, belong to an animal (inheritance relationship).

  Has-a represents a combination that contains a relationship . For example, rabbits contain legs, first-class components.

15, ClassLoader How to load class?

  There are multiple classloader in the JVM, each of which can be responsible for loading classes at a particular location , for example, thebootstrap class loads the classes that are responsible for loading Jre/lib/rt.jar, and the classes in our usual JDK are in Rt.jar. Extclassloader is responsible for loading classes in Jar/lib/ext/*.jar, Appclassloader is responsible for CLASSPATH the specified directories or classes in the jar . In addition to Bootstrap, the other classloader themselves are Java classes, and their parent class is ClassLoader.

16. Benefits of Layered Design

  The various functions according to the calling process is modular, the advantages of modularity is can be arbitrary combination , example: If you want to register a user, the process for the display interface and through the interface to receive user input, and then business logic processing, in the processing of business logic and access to the database, This is also possible if we write all of these steps in a single method, but the downside is that when the interface is to be modified, because the code is all within a single method, it may break the code of the business logic and the database access, as well, when modifying the business logic or database access codes, will also break the code of other parts. layering is to put the interface part, the business logic part, the database access part of the code in their own separate methods or classes, so there is no reaching problem. after this layering, it is also convenient to switch layers, such as the original interface is swing, now to the BS interface, if initially layered design, this time does not need to involve business and data access code, just write a web interface.

Benefits of Tiering:

  1. Decoupling between software is realized;

2. Ease of Division

3. Easy Maintenance

4. Improve the reuse of software components

5. Easy to replace a product , such as the persistent layer is hibernate, need to replace the product with TopLink, do not use the other business code, directly change the configuration.

6. Easy to expand the product function.

7. Easy to adapt to the changing needs of users

17, the role of Hashcode method?

  Hashcode This method is used to identify whether 2 objects are equal.

  The Equals method and the Hashcode method are all 2 methods used to determine whether 2 objects are equal, but they are different.

  In general, the Equals method is called to the user, if you want to determine whether 2 objects are equal, you can rewrite the Equals method and then call in the code to determine whether they are equal. In simple terms, the Equals method is primarily used to judge whether 2 objects are not equal on the surface or on the content. For example, there is a student class, the attribute is only the name and gender, then we can think that as long as the name and gender equality, then say that the 2 objects are equal.

  Hashcode Method General users do not call , such as in HashMap, because key is not repeatable, he in the judgment key is not repeated when the hashcode this method, but also used the Equals method. Here can not repeat is that equals and hashcode as long as there is a range! So simply put,hashcode is equivalent to an object encoding, as if the file of MD5, he and equals is the difference is that he returned to the int type, compared to not intuitive. We generally overwrite equals while covering the hashcode to make their logic consistent . For example, if the name and gender are equal even if 2 objects are equal, then the Hashcode method also returns the Hashcode value of the name plus the hashcode value of the gender, so logically they are consistent.

  To physically determine whether 2 objects are equal, use = =, if the physical (memory) address of the two objects is equal, then the two objects must be the same object.

18. What is AOP? 1.AOP Concept Introduction

  The so-called AOP, or Aspect orientied program, is the aspect-oriented (tangent) programming.

Aspect-oriented programming aspect-orlented-programming, that is, AOP is a powerful complement to the object-oriented thinking mode.

The benefit of AOP is the ability to dynamically add and remove logic on facets without affecting the original execution code

2. Explaining what the aspect is (facets)

  The so-called aspect (facet), refers to the system through the various modules of a function is a aspect (tangent ), such as logging, unified exception handling, transaction processing, permission checks, These functions are a facet of the software system, not a single point, and appear in each module.

3. What is aspect-oriented programming

The function of one aspect of the system is encapsulated in the form of an object to deal with is aspect-oriented (tangent) programming

4. How to do aspect-oriented programming

  The corresponding object of the function module is embedded in the original system module, and the proxy technology is used to invoke the target, and the code (object) of the slicing function is added. Therefore, when you configure a proxy object with spring, you need to have two attributes, representing both the target and the Tangent object (Advisor).

19. Talk about your understanding of MVC

MVC is the abbreviation for Model-view-controler. That is, the model-view-controller. MVC is a design pattern that forcefully separates the input, processing, and output of an application.

Models, views, and controllers in MVC have different tasks.

    • Views: A view is an interface that the user sees and interacts with. The view displays the relevant data to the user and accepts the user's input. The view does not handle any business logic.
    • Model: Models represent business data and business processes. Equivalent to JavaBean. A model can provide data for multiple views. This improves the reusability of the application
    • Controller: When the user clicks the Submit button in the Web page, the controller accepts the request and invokes the appropriate model to process the request. The corresponding view is then called according to the result of the processing to display the results of the processing.

MVC process: First the controller accepts the user's request, invokes the corresponding model for business processing, and returns the data to the controller. The controller invokes the corresponding view to display the results of the processing. and presented to the user through a view.

It company interview Manual: http://www.mianwww.com/

Interview Questions Collection--java basic part (i)

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.