Home>Article>Java> Collection of classic Java interview questions (2)

Collection of classic Java interview questions (2)

王林
王林 forward
2020-07-07 16:19:12 1840browse

Collection of classic Java interview questions (2)

1. The difference between int and Integer

(Related tutorial recommendations:java interview questions)

1. Integer is a wrapper class of int, which is a basic data type of java

2. Integer variables must be instantiated before they can be used, while int variables do not need to be

3 , Integer is actually a reference to an object. When a new Integer is created, a pointer is actually generated pointing to the object; while int directly stores the data value

4. The default value of Integer is null, and the default value of int is The value is 0

2. What is a for each loop? What data types can it loop through?

The enhanced version of the for loop is simpler to write and less error-prone (because you don’t have to worry about the array going out of bounds). The bottom layer is also obtained using an iterator, but the iterator is obtained by the jvm. We only need to obtain the iterator, so in the process of using the foreach loop variable element, we are not allowed to use the collection object to modify the number of elements in the collection.

Writing:

for(String it : set){ System.out.println("集合的元素:" + it); }

Disadvantages:

An error may be reported when operating the array index or adding or deleting the collection.

(Recommended learning:java introductory program)

3. What is the difference between overloading and rewriting?

1. Method overloading

is a means for classes to process different types of data in a unified way. Multiple functions with the same name exist at the same time, with different number/type of parameters. Overloading is a manifestation of polymorphism in a class.

Method overloading in Java means that you can create multiple methods in a class. They have the same name but different parameters and different definitions.

When calling methods, the specific number and parameter types passed to them are used to determine which method to use. This is polymorphism.

When overloading, the method name should be the same, but the parameter type and number are different, and the return value type can be the same or different. The return value type cannot be used as a criterion for distinguishing overloaded functions.

2. Method rewriting

The parameter list must be completely the same as the overridden method, otherwise it cannot be called rewriting but overloading.

The returned type must always be the same as the return type of the overridden method, otherwise it cannot be called an override but an overload.

The access modifier limit must be greater than the access modifier of the overridden method (public>protected>default>private)

The overridden method must not throw a new checked exception or be larger than The overridden method declares a broader checked exception. For example: a method of the parent class declares a checked exception IOException. When overriding this method, you cannot throw Exception. You can only throw exceptions of subclasses of IOException, and you can throw unchecked exceptions.

Note: The constructor cannot be inherited, so it cannot be overridden, but it can be overloaded.

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

1. Neither abstract classes nor interfaces can be instantiated directly. If they are to be instantiated, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to classes that implement all interface methods. object.

2. Abstract classes must be inherited by subclasses, and interfaces must be implemented by classes.

3. Interfaces can only be used for method declarations. Abstract classes can be used for method declarations and method implementations.

4. Variables defined in interfaces can only be public static constants. Variables in abstract classes are ordinary variables.

5. All abstract methods in an abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when a class implements an interface, if it cannot implement all interface methods, then the class can only be an abstract class.

6. Abstract methods can only be declared, not implemented. Interfaces are the result of design, and abstract classes are the results of reconstruction.

7. Abstract classes do not need abstract methods.

8. If there is an abstract method in a class, then the class can only be an abstract class

9. The abstract method must be implemented, so it cannot be static or private.

10. Interfaces can inherit interfaces and multiple interfaces, but classes can only inherit from a single root.

The main reason why multiple inheritance of classes is not allowed is that if A inherits B and C at the same time, and b and c have a D method at the same time, how does A decide which one to inherit?

But there is no such problem with interfaces. Interfaces are all abstract methods and it doesn't matter who inherits them, so interfaces can inherit multiple interfaces.

(Video tutorial recommendation:java video tutorial)

5. The difference between final, finally, finalize

final: Keywords and modifiers in java.

A) If a class is declared final, it means that it cannot derive new subclasses and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and final at the same time.

B) If you declare variables or methods as final, you can ensure that they will not be changed during use.

(1)被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。

(2)被声明final的方法只能使用,不能重载。

finally:java的一种异常处理机制。

finally是对Java异常处理模型的最佳补充。finally结构使代码总会执行,而不管无异常发生。使用finally可以维护对象的内部状态,并可以清理非内存资源。特别是在关闭数据库连接这方面,如果程序员把数据库连接的close()方法放到finally中,就会大大降低程序出错的几率。

finalize,它是一个方法,属于java.lang.Object类,它的定义如下:

protected void finalize()throws Throwable{}

众所周知,finalize()方法是GC(garbagecollector运行机制的一部分,在此我们只说说finalize()方法的作用是什么呢?

finalize()方法是在GC清理它所从属的对象时被调用的,如果执行它的过程中抛出了无法捕获的异常(uncaughtexception,GC将终止对改对象的清理,并且该异常会被忽略;直到下一次GC开始清理这个对象时,它的finalize()会被再次调用。

The above is the detailed content of Collection of classic Java interview questions (2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete