国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Java Javagetting Started Collection of classic Java interview questions (2)

Collection of classic Java interview questions (2)

Jul 07, 2020 pm 04:19 PM
java Interview questions

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的變量必須在聲明時(shí)給定初值,而在以后的引用中只能讀取,不可修改。

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

finally:java的一種異常處理機(jī)制。

finally是對(duì)Java異常處理模型的最佳補(bǔ)充。finally結(jié)構(gòu)使代碼總會(huì)執(zhí)行,而不管無(wú)異常發(fā)生。使用finally可以維護(hù)對(duì)象的內(nèi)部狀態(tài),并可以清理非內(nèi)存資源。特別是在關(guān)閉數(shù)據(jù)庫(kù)連接這方面,如果程序員把數(shù)據(jù)庫(kù)連接的close()方法放到finally中,就會(huì)大大降低程序出錯(cuò)的幾率。

finalize,它是一個(gè)方法,屬于java.lang.Object類,它的定義如下:

protected void finalize()throws Throwable{}

眾所周知,finalize()方法是GC(garbagecollector運(yùn)行機(jī)制的一部分,在此我們只說(shuō)說(shuō)finalize()方法的作用是什么呢?

finalize()方法是在GC清理它所從屬的對(duì)象時(shí)被調(diào)用的,如果執(zhí)行它的過(guò)程中拋出了無(wú)法捕獲的異常(uncaughtexception,GC將終止對(duì)改對(duì)象的清理,并且該異常會(huì)被忽略;直到下一次GC開(kāi)始清理這個(gè)對(duì)象時(shí),它的finalize()會(huì)被再次調(diào)用。

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

How does garbage collection work in Java? How does garbage collection work in Java? Aug 02, 2025 pm 01:55 PM

Java's garbage collection (GC) is a mechanism that automatically manages memory, which reduces the risk of memory leakage by reclaiming unreachable objects. 1.GC judges the accessibility of the object from the root object (such as stack variables, active threads, static fields, etc.), and unreachable objects are marked as garbage. 2. Based on the mark-clearing algorithm, mark all reachable objects and clear unmarked objects. 3. Adopt a generational collection strategy: the new generation (Eden, S0, S1) frequently executes MinorGC; the elderly performs less but takes longer to perform MajorGC; Metaspace stores class metadata. 4. JVM provides a variety of GC devices: SerialGC is suitable for small applications; ParallelGC improves throughput; CMS reduces

Comparing Java Build Tools: Maven vs. Gradle Comparing Java Build Tools: Maven vs. Gradle Aug 03, 2025 pm 01:36 PM

Gradleisthebetterchoiceformostnewprojectsduetoitssuperiorflexibility,performance,andmoderntoolingsupport.1.Gradle’sGroovy/KotlinDSLismoreconciseandexpressivethanMaven’sverboseXML.2.GradleoutperformsMaveninbuildspeedwithincrementalcompilation,buildcac

go by example defer statement explained go by example defer statement explained Aug 02, 2025 am 06:26 AM

defer is used to perform specified operations before the function returns, such as cleaning resources; parameters are evaluated immediately when defer, and the functions are executed in the order of last-in-first-out (LIFO); 1. Multiple defers are executed in reverse order of declarations; 2. Commonly used for secure cleaning such as file closing; 3. The named return value can be modified; 4. It will be executed even if panic occurs, suitable for recovery; 5. Avoid abuse of defer in loops to prevent resource leakage; correct use can improve code security and readability.

See all articles