1, when using the final keyword to modify a variable, is the reference cannot be changed, or the reference variable cannot be changed?
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");
Execute the following statement to report a compilation error: A=new StringBuffer ("");
However, the following can be done by compiling: A.append ("Lallaal");
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 is not possible, the method can still modify the object parameters inside.
2. What is the difference between a static variable and an instance variable?
Differences in syntax definitions: Static variables require static modification, and instance variables are not required.
When the program runs: the instance variable belongs to the property of an object, and an instance object must be created, where the reasonable 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, also known as a class variable, as long as the program loads the class's bytecode, without creating any instance objects, the static variables will be allocated memory 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 directly refer to the class name.
3. 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 the object being created, an object must be created before a non-static method call is made, 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, no instance object may have been created, and if a call to a non-static method is emitted from the static method, the non-static method cannot confirm which object it is associated with. So the answer is no.
4. The difference between integer and int
int is one of the original data types provided by Java in 8. Java provides a wrapper class for each raw data type. Integer is the encapsulated class of int. The int default value is 0,integer and the default value is null. The former cannot express an unassigned state, which can be distinguished.
5. What is the difference between overload and override? Can the overloaded method change the type of the return value?
Overload are overloads, Overrid are overrides, that is, methods.
Overload means that you can have more than one method with the same name in the same class, but these methods have different parameter lists (the number of arguments or the type of the parameter).
overriding override means that a method in a subclass can be exactly the same as the name and argument of a method in the parent class, and when called by the subclass to create an instance object, the method defined in the subclass is called, which is equivalent to completely overwriting the method defined in the parent class (which is also a Java polymorphic embodiment). When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a subclass that throws a parent class exception, because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The permissions of a subclass method can be larger than the parent class and cannot be smaller. If a method in the parent class is private, the subclass does not have a overwrite 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.
Coverage requires a few points to note:
A, the mark of the method of coverage must match with the mark of the method that is covered, can reach the effect of coverage;
b, the return value of the overridden method must be consistent with the return of the overridden method;
C, the overridden method throws an exception that must be the same as the exception thrown by the overridden method, or its subclass;
D, the overridden method cannot be a method that is modified by private, otherwise only a new method with the same name is defined in its subclass;
Overload points to note:
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, can not be overloaded by access rights, return type, thrown exception;
3, the method of the exception type and number will not affect the overload;
6. What is the difference between AbstractClass and interface syntax?
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. The access type of an abstract method in an abstract class can be public,protected and (the default type, although
Eclipse does not give an error, 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, and interfaces cannot contain static methods
6. Abstract classes and interfaces can contain static member variables, the access type of static member variables in the abstract class can be arbitrary, but the variables defined in the interface can only be publicstatic final type, and the default is the Publicstatic final type.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
7, String, StringBuffer, StringBuilder the difference?
String string constant (final decoration, not inherited), string is a constant and cannot be changed after it is created.
StringBuffer string variables, thread safety, and final modification, are not allowed to be inherited, in which the large number of methods are synchronized, including the common Appen method is also synchronized (Synchronized), the ToString method of the object cache To reduce the cost of the element.
1 Public synchronized 2ifnull3 tostringcache = arrays.copyofrange (value, 0 45 return new true6 }
The StringBuilder string variable (non-thread-safe) inherits and implements the same interface and class as StringBuffer, except that the method is not synchronized (synchronize) and is basically consistent. The difference is that the ToString method returns an object
1 Public 2//3returnnew String (value, 04 }
8. What is the difference between ArrayList and LinkedList?
Both implement the list interface, the main difference:
1, ArrayList is an index-based data interface, its underlying is an array, you can use the time complexity of O (1) to random access to elements, LinkedList is a list of elements to store its data, each row of elements and its first and the latter element is connected together, In this case, the complexity of the lookup is O (n).
2. Relative arraylist,linkedlist inserts, additions, and deletions are fast because the elements are added anywhere in the collection, and do not need to recalculate the size or update the index as an array.
3, LinkedList more memory than ArrayList, because LinkedList stores two references for each node, one to the previous element, and one to the next.
9, the class of the instantiation of the order, such as the parent static data, constructors, fields, sub-static data, constructors, fields, when new, their execution order .
The static variables of the parent class--The static code segment of the parent class--The static variable of the child class--The static code segment of the child class--the non-static variable of the parent class--the constructor of the parent class--The non-static variable of the child class--and the subclass's
10.
Java Foundation Interview (i)