亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Java JavaInterview questions Common basic Java interview questions

Common basic Java interview questions

Dec 14, 2019 pm 03:06 PM
java

Common basic Java interview questions

What is the difference between JDK and JRE?

JDK: The abbreviation of Java Development Kit, java development tool kit, provides java development environment and running environment.

JRE: The abbreviation of Java Runtime Environment, java runtime environment, provides the required environment for the operation of java. (Recommended study: java common interview questions)

Specifically, JDK actually includes JRE, and also includes the compiler javac that compiles java source code, and also includes many java program debugging and Tools for analysis. To put it simply: if you need to run java programs, you only need to install JRE. If you need to write java programs, you need to install JDK.

== What is the difference between equals and equals?

The effects of == are different for basic types and reference types, as follows:

Basic types: What is compared is whether the values ??are the same;

Reference type: What is compared is whether the references are the same;

quals is essentially ==, except that String and Integer override the equals method and turn it into a value comparison.

== For basic types, it is a value comparison, for reference types, it is a reference comparison; and equals is a reference comparison by default, but many classes override the equals method, such as String, Integer etc. turns it into a value comparison, so under normal circumstances equals compares whether the values ??are equal.

If the hashCode() of two objects is the same, equals() must also be true, right?

No, the hashCode() of the two objects is the same, and equals() may not be true.

String str1 = "通話";
String str2 = "重地";
System.out.println(String.format("str1:%d | str2:%d",  str1.hashCode(),str2.hashCode()));
System.out.println(str1.equals(str2));

Result of execution:

str1:1179395 | str2:1179395
false

Code interpretation: Obviously the hashCode() of "call" and "powerful place" are the same, but equals() is false, Because in a hash table, equal hashCode() means that the hash values ??of two key-value pairs are equal. However, equal hash values ??do not necessarily mean that the key-value pairs are equal.

What is the role of final in java?

The final modified class is called the final class, and this class cannot be inherited.

Final modified methods cannot be overridden.

Final modified variables are called constants. Constants must be initialized. After initialization, the value cannot be modified.

What is Math.round(-1.5) in java equal to?

is equal to -1.

String is a basic data type?

String does not belong to the basic type. There are 8 basic types: byte, boolean, char, short, int, float, long, double, and String belongs to the object.

What are the classes for operating strings in java? What's the difference between them?

The classes that operate on strings include: String, StringBuffer, and StringBuilder.

The difference between String and StringBuffer and StringBuilder is that String declares an immutable object. Each operation will generate a new String object, and then point the pointer to the new String object, while StringBuffer and StringBuilder can be used in the original object. The operation is performed on the basis of , so it is best not to use String when the content of the string is frequently changed.

The biggest difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe, while StringBuilder is non-thread-safe, but the performance of StringBuilder is higher than StringBuffer, so it is recommended to use StringBuilder in a single-threaded environment and in a multi-threaded environment. It is recommended to use StringBuffer.

String str="i" is the same as String str=new String("i")?

It’s different because the memory allocation method is different. String str="i", the Java virtual machine will allocate it to the constant pool; and String str=new String("i") will be allocated to the heap memory.

How to reverse a string?

Use the reverse() method of StringBuilder or stringBuffer.

Sample code:

// StringBuffer reverse
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcdefg");
System.out.println(stringBuffer.reverse()); // gfedcba
// StringBuilder reverse
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("abcdefg");
System.out.println(stringBuilder.reverse()); // gfedcba

What are the common methods of the String class?

indexOf():返回指定字符的索引。
charAt():返回指定索引處的字符。
replace():字符串替換。
trim():去除字符串兩端空白。
split():分割字符串,返回一個(gè)分割后的字符串?dāng)?shù)組。
getBytes():返回字符串的 byte 類型數(shù)組。
length():返回字符串長度。
toLowerCase():將字符串轉(zhuǎn)成小寫字母。
toUpperCase():將字符串轉(zhuǎn)成大寫字符。
substring():截取字符串。
equals():字符串比較。

Do abstract classes have to have abstract methods?

No, abstract classes do not necessarily have to have abstract methods.

Sample code:

abstract class Cat {
    public static void sayHi() {
        System.out.println("hi~");
    }
}

In the above code, the abstract class does not have abstract methods but it can run normally.

What are the differences between ordinary classes and abstract classes?

Ordinary classes cannot contain abstract methods, while abstract classes can contain abstract methods.

Abstract classes cannot be instantiated directly, but ordinary classes can be instantiated directly.

Can abstract classes be modified with final?

No, defining an abstract class is for other classes to inherit. If it is defined as final, the class cannot be inherited, which will cause conflicts with each other, so final cannot modify the abstract class, as shown in the figure below , the editor will also prompt an error message:

Common basic Java interview questions

What is the difference between an interface and an abstract class?

Implementation: Subclasses of abstract classes use extends to inherit; interfaces must use implements to implement the interface.

構(gòu)造函數(shù):抽象類可以有構(gòu)造函數(shù);接口不能有。

main 方法:抽象類可以有 main 方法,并且我們能運(yùn)行它;接口不能有 main 方法。

實(shí)現(xiàn)數(shù)量:類可以實(shí)現(xiàn)很多個(gè)接口;但是只能繼承一個(gè)抽象類。

訪問修飾符:接口中的方法默認(rèn)使用 public 修飾;抽象類中的方法可以是任意訪問修飾符。

java 中 IO 流分為幾種?

按功能來分:輸入流(input)、輸出流(output)。

按類型來分:字節(jié)流和字符流。

字節(jié)流和字符流的區(qū)別是:字節(jié)流按 8 位傳輸以字節(jié)為單位輸入輸出數(shù)據(jù),字符流按 16 位傳輸以字符為單位輸入輸出數(shù)據(jù)。

BIO、NIO、AIO 有什么區(qū)別?

BIO:Block IO 同步阻塞式 IO,就是我們平常使用的傳統(tǒng) IO,它的特點(diǎn)是模式簡單使用方便,并發(fā)處理能力低。

NIO:New IO 同步非阻塞 IO,是傳統(tǒng) IO 的升級(jí),客戶端和服務(wù)器端通過 Channel(通道)通訊,實(shí)現(xiàn)了多路復(fù)用。

AIO:Asynchronous IO 是 NIO 的升級(jí),也叫 NIO2,實(shí)現(xiàn)了異步非堵塞 IO ,異步 IO 的操作基于事件和回調(diào)機(jī)制。

Files的常用方法都有哪些?

Files.exists():檢測文件路徑是否存在。
Files.createFile():創(chuàng)建文件。
Files.createDirectory():創(chuàng)建文件夾。
Files.delete():刪除一個(gè)文件或目錄。
Files.copy():復(fù)制文件。
Files.move():移動(dòng)文件。
Files.size():查看文件個(gè)數(shù)。
Files.read():讀取文件。
Files.write():寫入文件。

The above is the detailed content of Common basic Java interview questions. 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
1488
72
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

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.

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

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

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

Google Chrome cannot open local files Google Chrome cannot open local files Aug 01, 2025 am 05:24 AM

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

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

See all articles