Basics of Java Interview Questions

1. How many classes can be included in the ".java" source file? What are the restrictions?

  • It can contain multiple classes, but only one public class, and the public class name must be the same as the file name. Usually only one class is contained in a file.

2. Can the switch statement work on byte, on the long type, and on the scope of String?

  • switch(e), where e must be int type or enum type. Since short, char or byte will be automatically converted to int, these types and packaging types of these types are also possible. Obviously, the long type is not possible, String is not supported before jdk1.7, and is supported after that.

3. Short s1 = 1; s1 = s1 + 1; Is there something wrong? short s1 = 1; s1 += 1; Is it wrong?

  • For short s1=1; s1=s1+1; because s1+1 will automatically promote the type of expression, the result is int type, and the compiler will report a type conversion error when assigning to short type s1.

  • For short s1=1; s1 += 1; Since += is an operator specified by the java language, the java compiler will perform special processing on it, so it can be compiled correctly.

4. How many bytes does a char type variable occupy?

  • The char type variable is used to store Unicode characters and occupies two bytes.

5. When using the final keyword to modify a variable, is it the reference cannot be changed or the referenced object cannot be changed?

  • When the final keyword is used to modify a variable, it is to guide that the variable cannot be changed, and the content of the object pointed to by the reference variable can still be changed. For example, for the following statement: final StringBuffer sb = new StringBuffer("test");
    sb = new StringBuffer(""); // error
    sb.append("hello "); //correct

6. What is the difference between static variables and instance variables?

  • The difference in language definition: static keyword must be added before static variables, but not before instance variables

  • The difference when the program is running: the instance variable belongs to the attribute of an object, and the instance object must be created before the instance variable can be allocated space before the instance variable can be used. Static variables do not belong to an instance object, but belong to a class, so it is also called a class variable. As long as the program loads the bytecode of the class, there is no need to create any instance object, the static variable will be allocated space, and the static variable can be used. In short, instance variables can only be used through this object after the object is created, and static variables can be directly referenced by the class name.

  • For example, for the following program, no matter how many instance objects are created, only one staticVar variable will always be assigned, and every time an instance object is created, the staticVar will increase by 1; however, every time an instance object is created, an instanceVar will be assigned. That is, multiple instanceVars may be allocated, and the value of each instanceVar is only increased once.

public class Test{
 public static int staticVar = 0;
 public int instanceVar = 0;
 public void Add(){
   staticVar++;
   instanceVar++;
   System.out.println("static var=" + staticVar + "instance Var=" + instanceVar);
 }
}

7. Is it possible to call a non-static method from within a static method?

  • Not possible. Because the non-static method is associated with the object, you must create an object before you can call the method on the object, while the static method does not need to create an object when you call it, you can call it directly. In other words, when a static method is called, no instance object may have been created yet. If a call to a non-static method is issued from a static method, which object is the non-static method associated with? This logic cannot be established, so a static method internally makes a call to a non-static method.

8. The difference between Integer and int?

  • int is one of the eight primitive data types provided by java. Java provides a wrapper class for each primitive type, and Integer is a wrapper class provided by Java for int. The default value of int is 0, and the default value of Integer is null, that is, Integer can distinguish the difference between unassigned and 0, while int cannot express the unassigned situation.

9. Can an interface inherit an interface? Can an abstract class implement an interface? Can an abstract class inherit a concrete class? Can an abstract class have a static main method?

  • Interfaces can inherit interfaces. Abstract classes can implement interfaces, and abstract classes can inherit concrete classes. There can be a static main method in an abstract class.

10. What is the mechanism for achieving polymorphism in Java?

  • It depends on that the reference variable defined by the parent class or interface can point to the instance object of the subclass or the concrete implementation class, and the method called by the program is bound dynamically at runtime, which is the method of the concrete instance object pointed to by the reference variable, that is The method of the object that is running in memory, not the method defined in the type of the reference variable.

11. What is the difference between abstract class and interface?

  • Abstract classes can have constructors, but interfaces cannot have constructors.

  • There can be ordinary member variables in the abstract class, but there are no ordinary member variables in the interface

  • An abstract class can contain non-abstract ordinary methods, and all methods in an interface must be abstract, and there can be no non-abstract ordinary methods.

  • The access type of the abstract method in the abstract class can be public, protected and default type, but the abstract method in the interface can only be of the public type, and the default is the public abstract type.

  • Static methods can be contained in abstract classes, and static methods cannot be contained in interfaces

  • Both abstract classes and interfaces can contain static member variables. The access type of static member variables in abstract classes can be arbitrary, but the variables defined in the interface can only be of publicstatic final type, and the default is publicstatic final type.

  • A class can implement multiple interfaces, but can only inherit one abstract class.

12. Can an inner class refer to the members of its containing class? Are there any restrictions?

  • absolutely okay. If it is not a static inner class, there are no restrictions.

  • If you treat the static nested class as a special case of the inner class, in this case you cannot access the ordinary member variables of the outer class, but can only access the static members in the outer class. For example, the following code:

class Outer{
 static int x;
 static class Inner{
   void test()
   {
     System.out.println(x);
   }
 }
} 

13.String s = new String("xyz"); How many StringObjects have been created? Is it possible to inherit the String class?

  • Two or one, "xyz" as a constant object, this object will be placed in the string constant buffer, no matter how many times the constant "xyz" appears, there is only one in the constant buffer. When new String("xyz"), if the object does not exist in the constant buffer, you need to create a new object in the constant buffer first, and then use the contents of the constant buffer object to create a new String object, so it will create Two objects, if the object ("xyz") exists in the constant buffer, only one object will be created.

  • The String class cannot be inherited, because the String default final modification is not inheritable.

14. How many objects are created in the following statement: String s="a"+"b"+"c"+"d";

  • For the following code:

String s1 = "a";
String s2 = s1 + "b";
String s3 = "a" + "b";
System.out.println(s2 == "ab");
System.out.println(s3 == "ab");
  • The print result of the first statement is false; the print result of the second statement is true. This shows that the compiler can optimize expressions that are directly added to string constants. It is not necessary to wait until the runtime to perform the addition operation. Instead, remove the plus sign at compile time and directly compile it into one of these constants. the result of.

  • After the first line of code in the title is optimized by the compiler at compile time, it is equivalent to directly defining an "abcd" string, so the above code should only create a String object. Look at the following code:

String s = "a" + "b" + "c" + "d"
System.out.println(s == "abcd");  //true

15. There is a return statement in try {}, then the code in finally{} immediately after the try will be executed, when will it be executed, before or after return?

  • We know that the statement in finally{} will definitely be executed, so is this before return or after return? Look at the following code:

public class Test{
 public static void main(String[] args){
   System.out.println(new Test().test());
 }

 int test(){
   int x = 1;
   try{
     return x;
   }finally{
     ++x;
   }
 }
}
  • The result of the operation is 1, indicating that the return statement has been executed and then the finally statement is executed, but it does not return directly, but saves the return result, and then executes it in the finally statement.

16. The difference between final, finally, finalize?

  • Final is used to declare properties, methods, and classes, which respectively indicate that properties are immutable, methods cannot be overridden, and classes cannot be inherited. To access local variables for internal classes, local variables must be defined as final types.

  • Finally is a part of the structure of the exception handling statement, which means that it is always executed.

  • Finalize is a method of the Object class. This method of the recycled object will be called when the garbage collector is executed. This method can be overridden to provide other resource recovery during garbage collection, such as closing files. But the JVM does not guarantee that this method will always be called.

17. What are the similarities and differences between runtime exceptions and general exceptions?

  • Abnormality indicates an abnormal state that may occur during the running of the program, and runtime anomaly indicates an abnormality that may be encountered in the normal operation of the virtual machine, and is a common operating error. The java compiler requires that the method must declare that it throws non-runtime exceptions that may occur, but it does not require that it must declare that it throws uncaught runtime exceptions.

18. Is there an application difference between error and exception?

  • Error represents a serious problem when recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation. Exception represents a design or implementation problem. In other words, it represents a situation that would never happen if the program runs normally.

19. What is the difference between heap and stack in Java?

  • The heap and stack in the JVM belong to different memory areas, and they are used for different purposes. The stack is often used to save method frames and local variables, and objects are always allocated on the heap. The stack is usually smaller than the heap, and it is not shared among multiple threads, while the heap is shared by all threads of the entire JVM.

  • Stack: Some basic types of variables defined in functions and object reference variables are allocated in the stack memory of the function. When a variable is defined in a code block, Java allocates memory space for this variable in the stack. After exceeding the scope of the variable, Java will automatically release the memory space allocated for the variable, and the memory space can be used for other purposes immediately.

  • Heap: Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java virtual machine. After an array or object is generated in the heap, you can also define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, and the variable in the stack becomes After the reference variable of the array or object, the reference variable in the stack can be used in the program to access the array or object in the heap. The reference variable is equivalent to a name for the array or object.

Guess you like

Origin blog.csdn.net/qq_30398499/article/details/102154421