Java basic interview (1) and java basic interview
1. When I use the final keyword to modify a variable, can the reference not be changed, or can the referenced variable not be changed?
When you use the final keyword to modify a variable, it is suggested that the variable cannot be changed, and the content in the object to which the referenced variable points can still be changed. For example, for the following statement: final StringBuffer a = new StringBuffer ("immutable ");
Run the following statement to report the compilation error: a = new StringBuffer ("");
However, you can compile the statement as follows: a. append ("lallaal ");
When defining the parameters of a method, someone may want to use the following form to prevent the method from modifying the passed parameter object:
Public void method (final StringBuffer param) {}. In fact, this cannot be done. This method can still modify object parameters internally.
2. What is the difference between static variables and instance variables?
Syntax definition: static variables need to be modified, but instance variables do not.
Differences during program running: instance variables belong to the attributes of an object. You must create an instance object, where reasonable variables are allocated space to use this instance variable. Static variables do not belong to a certain instance object, but belong to a class, also known as class variables. As long as the program loads the class bytecode, no instance object needs to be created, static variables are allocated with memory space, and static variables can be used. In short, instance variables can be used only after an object is created. Static variables can be referenced directly by class names.
3. Can I call non-static methods from inside a static method?
No, because the non-static method must be associated with the created object. You must create an object before calling non-static methods, you can directly call the static method without creating an object. That is to say, when a static method is called, no instance object may be created. If a non-static method is called from the static method, the non-static method cannot determine the object to which it is associated. So the answer is no.
4. Differences between Integer and int
Int is one of the eight primitive data types provided by java. Java provides encapsulation classes for each raw data type. Integer is the encapsulation class of int. The default value of int is 0, and that of Integer is null. The former cannot express unassigned states, and the latter can be differentiated.
5. What is the difference between Overload and Override? Can the Overloaded method change the type of the returned value?
Overload is a heavy load, Overrid is a overwriting, that is, method rewriting.
Overload indicates that multiple methods with the same name can exist in the same class, but the parameter lists of these methods are different (the number of parameters or the parameter type is different ).
Override indicates that the method in the subclass can have the same name and parameter as a method in the parent class. When this method is called by creating an instance object in the subclass, the method defined in the subclass is called, this is equivalent to completely overwriting the methods defined in the parent class (this is also a manifestation of java polymorphism ). When a subclass overwrites the method of the parent class, it can only throw fewer exceptions than the parent class, or a subclass that throws the parent class exception, because the subclass can solve some problems of the parent class, there cannot be more problems than the parent class. The permission of the subclass method must be greater than or equal to that of the parent class. If the method in the parent class is private, the Child class does not have the overwrite limit, which is equivalent to adding a new method to the Child class.
The question about whether the Overloaded method can change the type of the returned value depends on what you want to ask? This question is vague. If the parameter lists of several Overloaded methods are different, their return type can certainly be different. However, I guess the question you want to ask is: if the parameter lists of the two methods are the same, can you make them return different values to implement the Overload. This is not acceptable. We can use the reverse Identification Method to illustrate this problem, because sometimes we can call a method without defining the returned result variable, that is, do not care about the returned result, for example, we call map. in the remove (key) method, although the remove method has a return value, we usually do not define variables that receive the returned results. In this case, assume that there are two methods with the same name and parameter list in this class, java cannot determine which method the programmer wants to call because it cannot determine the type of the returned result.
Pay attention to the following points for overwriting:
A. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;
B. The returned value of the overwritten method must be the same as that of the overwritten method;
C. The exception thrown by the override method must be the same as the exception thrown by the override method, or its subclass;
D. The method to be overwritten cannot be a private method. Otherwise, only a new method with the same name is defined in its subclass;
Notes for Overload:
1. You can only use different parameter styles when using overload. For example, different parameter types, different parameter numbers, and different parameter Order (of course, several parameter types in the same method must be different, such as fun (int, float ), but cannot be fun (int, int ));
2. It cannot be overloaded by access permission, return type, or thrown exception;
3. The method exception type and quantity do not affect the heavy load;
6. What are the differences between abstractclass and interface syntax?
1. abstract classes can have constructor methods, and interfaces cannot have constructor methods.
2. abstract classes can contain common member variables. The interface does not contain common member variables.
3. abstract classes can contain non-Abstract Common methods. All methods in the interface must be abstract and cannot have non-Abstract Common methods.
4. The access type of abstract methods in abstract classes can be public, protected, and (default type, although
There is no error in eclipse, but it should not), but the abstract method in the interface can only be of the public type, and the default is the public abstract type.
5. abstract classes can contain static methods. Interfaces cannot contain static methods.
6. the abstract class and interface can contain static member variables. The access type of static member variables in the abstract class can be any, but the variables defined in the interface can only be of the publicstatic final type, the default value is publicstatic final.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
7. What are the differences between String, StringBuffer, and StringBuilder?
String constant (final modifier, which cannot be inherited). String is a constant and cannot be changed after creation.
StringBuffer string variables, thread security, and final modification, are not allowed to be inherited. The vast majority of methods are processed synchronously, the commonly used appen methods are also Synchronized. The toString method caches objects to reduce the overhead of elements.
1 public synchronized String toString() { 2 if (toStringCache == null) { 3 toStringCache = Arrays.copyOfRange(value, 0, count); 4 } 5 return new String(toStringCache, true); 6 }
The StringBuilder string variables (non-thread-safe) and StringBuffer both inherit and implement the same interfaces and classes. methods are basically the same except for synchronize, the difference is that the toString method returns an object.
1 public String toString() { 2 // Create a copy, don’t share the array 3 return new String(value, 0, count); 4 }
8. What is the difference between ArrayList and rule list?
Both implement the list interface, the main difference is:
1. ArrayList is an index-based data interface, which is an array at the underlying layer. It can be used to randomly access elements with the time complexity of O (1, the sorted list stores its data in the form of an element list. each row of element is not connected with the previous and last element. In this case, the search complexity is O (n ).
2. Compared with ArrayList, the insert, add, and delete operations of the sorted list are fast. Because the element is added to any position in the Set, the size is recalculated or the index is updated as the array is not needed.
3. The memory list occupies more memory than the ArrayList, because the memory list stores two references for each node. One refers to the first element and the other to the next element.
9. Class instantiation sequence, such as parent static data, constructor, field, subclass static data, constructor, and field. When new, their execution sequence.
Static variables of the parent class --> static code snippet of the parent class --> static variables of the subclass --> static code snippet of the subclass --> non-static variables of the parent class --> constructors of the parent class --> non-static variable of subclass --> constructor of subclass.
10,