编程知识 cdmana.com

Six years of interview experience to share java basic interview questions

1. The difference between object-oriented and process oriented

 

1. The difference between object-oriented and process oriented

Process oriented

advantage : Better performance than object-oriented , Because class calls need to be instantiated , It costs a lot , Compare the consumption of resources . For example, single chip microcomputer 、 Embedded development 、Linux/Unix, Process oriented development is generally adopted for , Performance is the most important factor .

shortcoming : No object-oriented and easy to maintain 、 Easy to reuse 、 Easy to expand

object-oriented

advantage : Easy maintenance 、 Easy to reuse 、 Easy to expand , Because of the encapsulation of object-oriented 、 Inherit 、 Characteristics of polymorphism , You can design low coupling systems , Make the system more flexible 、 Easier to maintain

shortcoming : Performance is lower than process oriented

2.Java What are the characteristics of language

1. Easy to learn ;

2. object-oriented ( encapsulation , Inherit , polymorphic );

3. Platform independence (Java Platform independence of virtual machine );

4. reliability ;

5. Security ;

6. Multithreading support (C++ The language has no built-in multithreading mechanism , Therefore, the multithreading function of the operating system must be called for multithreading programming , and Java Language provides multithreading support );

 

7. It supports network programming and is very convenient (Java The birth of language is designed to simplify network programming , therefore Java Language not only supports network programming, but also is very convenient );

8. Compilation and interpretation coexist ;

3. About JVMJDK and JRE The most detailed and popular answer

JVM

Java virtual machine (JVM) Is to run Java The virtual machine of bytecode .JVM There are specific implementations for different systems (Windows,Linux,macOS), The purpose is to use the same bytecode , They all give the same result .

What is bytecode ? What are the advantages of bytecode ?

stay Java in ,JVM Understandable code is called bytecode ( The extension is .class The file of ), It does not target any specific processor , Virtual machine only .Java

Language by bytecode , To some extent, it solves the problem of low efficiency of traditional interpretative languages , At the same time, it retains the characteristics of transplantable explanatory language . therefore Java Programs run more efficiently , and , Because bytecode is not specific to a particular machine , therefore ,Java The program can run on many different computers without recompilation .

Java Program from the source code to run generally have the following 3 Step :

What we need to pay special attention to is .class-> The machine code step . In this step jvm Class loader first loads the bytecode file , Then the interpreter interprets the execution line by line , The execution speed of this method will be relatively slow . and , Some methods and code blocks are often called , That's hot code , So the introduction of

JIT compiler ,JIT Belongs to runtime compilation . When JIT After the compiler completes the first compilation , It will save the machine code corresponding to bytecode , You can use it directly next time . And we know that , The operation efficiency of machine code must be higher than Java Of the interpreter . It also explains why we often say Java It's a language where compilation and interpretation coexist .

HotSpot Inert assessment was used (LazyEvaluation) How to do it , According to the law of 28 , Consume most of

There is only a small part of the code in the system resources ( Hot code ), And that is JIT Need to compile

Part of .JVM Information will be collected according to each time the code is executed and some optimizations will be made accordingly , So the more times you execute , The faster it goes .JDK9

A new compilation mode is introduced AOT(Ahead of Time Compilation), It compiles bytecode directly into machine code , This avoids

JIT Preheat and other expenses .JDK Support hierarchical compilation and AOT Collaborative use of . however ,AOT The compiler's compilation quality is certainly not comparable to JIT

Compiler .

summary :Java virtual machine (JVM) Is to run Java The virtual machine of bytecode .JVM There are specific implementations for different systems (Windows,Linux,macOS), The same bytecode is used , They all give the same result . Bytecode and different systems JVM The realization is Java Language “ A compilation , It can run anywhere ” The key .

 

JDK and JRE

JDK yes JavaDevelopmentKit, It's fully functional JavaSDK. It has a JRE Everything you have , And the compiler (javac) And tools ( Such as javadoc and jdb). It can create and compile programs .JRE yes Java Runtime environment . It is run compiled Java A collection of all the content required by a program , Include Java virtual machine (JVM),Java Class library ,java Commands and other basic components . however , It can't be used to create new programs . If you just want to run Java

Procedure words , So you just need to install JRE That's all right. . If you need to do something Java Programming work , Then you need to install JDK

了 . however , It's not absolute . Sometimes , Even if you don't plan to do anything on the computer Java Development , still

Need to install JDK. for example , If you want to use JSP Deploy Web Applications , So technically , You're just running on the application server Java Program . Then why do we need JDK Well ? Because the application server will JSP Convert to Javaservlet, And need to use JDK To compile the servlet.

4.OracleJDK and OpenJDK Comparison of

Maybe a lot of people like me didn't touch and use it before looking at this problem OpenJDK. that Oracle and OpenJDK Is there a significant difference between ? The following is through my collection of some information for you to answer this neglected question by many people . about Java7, There's nothing critical .OpenJDK The project is mainly based on Sun Donated HotSpot Source code . Besides ,OpenJDK Be selected as Java7 Reference implementation of , from Oracle Engineers maintain . About JVM,JDK,JRE and OpenJDK The difference between ,Oracle Blog posts are in 2012 There is a more detailed answer :

ask :OpenJDK Source code in the repository and for building OracleJDK What's the difference between the code for ?

answer : Very close to - our OracleJDK The build process is based on OpenJDK7 structure , Only a few parts have been added , For example, deploy code , These include Oracle Of Java Plug ins and JavaWebStart The implementation of the , And some closed source code party components , Such as the pattern rasterizer , Some open source third-party components , Such as

Rhino, And some bits and pieces , Such as additional documents or third-party fonts . Looking forward to the future , Our goal is to open source OracleJDK All the parts of , Apart from the part where we think about business functions .

summary :

1.OracleJDK The version will be released every three years , and OpenJDK The version is released every three months ;

2.OpenJDK Is a reference model and is fully open source , and OracleJDK yes OpenJDK An implementation of , It's not completely open source ;

3.OracleJDK Than OpenJDK A more stable .OpenJDK and OracleJDK The code is almost the same , but OracleJDK There are more classes and some bug fixes . therefore , If you want to develop a business / Business software , I suggest you choose OracleJDK, Because it's thoroughly tested and stable . In some cases , Some people mentioned using OpenJDK There may be a lot of application crash problems , however , Just switch to OracleJDK You can solve the problem ;

4. Top companies are using OracleJDK, for example AndroidStudio,Minecraft and IntelliJIDEA development tool , among OpenJDK Not very popular ;

5. In response and JVM Performance aspect ,OracleJDK And OpenJDK Compared to providing better performance ;

6.OracleJDK No long-term support for upcoming releases , Users must get the latest version every time by updating to the latest version for support ;

7.OracleJDK Licensed under binary code license agreement , and OpenJDK according to GPLv2 Permission to get permission .

5.Java and C++ The difference between

I know a lot of people haven't learned C++, But the interviewer just likes to take us Java and C++ Yeah ! Can't !!! Even if I haven't learned C++, Write it down, too !

     Both are object-oriented languages , All support encapsulation 、 Inheritance and polymorphism

    Java   No pointers are provided to directly access memory , Program memory is more secure

    Java The classes of are single inheritance ,C++ Support for multiple inheritance ; although Java Can't inherit more , But interfaces can inherit more .

    Java There is an automatic memory management mechanism , There's no need for programmers to manually release useless memory

6. What is? Java How is the main class of an application different from that of an applet

There can be more than one class in a program , But only one class can be the main class . stay  Java  In the application , This main class refers to the  main() Class of method . And in the  Java  Small program , This main class is inherited from the system class  JApplet  or  Applet  Subclasses of . The main class of an application does not have to be  public class , But the main class requirement of the applet must be  public  class . The main class is  Java  Entry point of program execution .

7. Java  What are the differences between apps and applets

Simply put, the application is launched from the main thread ( That is to say  main()  Method ).applet  The applet doesn't have main  Method , Mainly embedded in the browser page to run ( call  init() Thread or  run() To start up ), Embedded browser  flash  The game is similar to .

8.  The difference between character type constant and string constant

1.  Formally :  A character constant is a character caused by a single quotation mark String constants are characters caused by double quotes

2.  Meaning :  Character constant is equivalent to an integer value ( ASCII  value ), Can participate in expression operation A string constant represents an address value ( The string is stored in memory )

3.  Memory size Character constants only take up  2  Bytes String constant takes several bytes ( At least one character end flag ) ( Be careful : char  stay  Java  Two bytes in ) 

9.  Constructors  Constructor  Whether it can be  override

When we talk about inheritance, we know that the private properties and construction methods of the parent class cannot be inherited , therefore Constructor  You can't be  override( rewrite ), But it can.  overload( heavy load ), So you can see that there are multiple constructors in a class .

10.  The difference between overloading and rewriting

heavy load :  Occurs in the same class , Method name must be the same , Different parameter types 、 The number is different. 、 Different order , Method return values and access modifiers can be different , Occurs at compile time .

rewrite :  Occurs in a parent-child class , Method name 、 The parameter list must be the same , The return value range is less than or equal to the parent class , Exception range thrown is less than or equal to parent class , Access modifier

The range is greater than or equal to the parent class ; If the parent method access modifier is  private  Then the subclass cannot override the method .

11. Java  Three characteristics of object-oriented programming :  encapsulation Inherit polymorphic

encapsulation

Encapsulation privatizes the properties of an object , At the same time, it provides some methods of properties that can be accessed by the outside world , If the property does not want to be accessed by the outside world , We don't have to provide access to the outside world . But if a class does not provide methods for external access , So this class doesn't make any sense .

Inherit

Inheritance is the technique of building new classes based on the definition of existing classes , New class definition can add new data or new functions , You can also use the functions of the parent class , Cannot selectively inherit a parent class . By using inheritance, we can easily reuse previous code .

The following is about inheritance  3  Please remember :

1.  Child class has parent class not  private  Properties and methods of .

2.  Subclasses can have their own properties and methods , That is, the subclass can extend the parent class .

3.  Subclass can implement the method of parent class in its own way .

polymorphic

Polymorphism refers to the specific type of the reference variable defined in the program and the uncertainty of the method call issued through the reference variable when programming , It's determined during program operation , That is, which class of instance object does a reference variable point to when it bottoms down , The method call issued by the reference variable is the method implemented in which class , Must be determined during run by program . stay  Java  There are two forms of polymorphism in : Inherit ( Override of the same method by multiple subclasses ) And interface ( Implement the interface and override the same method in the interface ).

12. String StringBuffer  and  StringBuilder  What's the difference  String  Why is it immutable

variability

In a nutshell :String  Class  final  The key character array holds the string ,privatefinal char value[], therefore  String  Object is immutable . and  StringBuilder  And StringBuffer  Inherit from  AbstractStringBuilder  class , stay  AbstractStringBuilder  Also USES character arrays to hold strings  char[]value  But it doesn't work  final  Keyword modification , So both of these objects are mutable .

StringBuilder  And  StringBuffer  We call the parent class to construct the method

AbstractStringBuilder  Realized , You can consult the source code .

AbstractStringBuilder.java

abstract class AbstractStringBuilder implements Appendable, CharSequence {

char[] value;

int count;

AbstractStringBuilder() { 

}

AbstractStringBuilder(int capacity) {

value = new char[capacity];

}

Thread safety

String  Objects in are immutable , It can be understood as a constant , Thread safety .AbstractStringBuilder  yes  StringBuilder  And  StringBuffer  The common parent of , Defines some basic operations for Strings , Such as  expandCapacity、append、insert、indexOf  Etc .StringBuffer  Add a synchronization lock to the method or a synchronization lock to the called method , So it's thread safe .StringBuilder  Method is not locked synchronously , So it's non thread safe .

performance

Every time the  String  When the type changes , Will generate a new  String  object , Then point the pointer to the new one  String  object .StringBuffer  Every time I was right about  StringBuffer  The object itself operates , Instead of generating new objects and changing object references . Use in the same case StringBuilder  Compared with  StringBuffer  Can only get  10%~15%  Left right performance improvement , But take the risk of multithreading insecurity .

Summary of the use of the three :

1.  Operate on a small amount of data  = String

2.  Single thread operation string buffer under the operation of a large number of data  = StringBuilder

3.  Multithreading string buffer operation under the operation of a large number of data  = StringBuffer

13.  Autoboxing and unboxing

Packing : Wrap basic types with their corresponding reference types ;

Unpacking : Convert wrapper type to base data type ;

14.  Why is it illegal to call a non static member within a static method

Because static methods can be called without objects , So in static methods , You cannot call other non static variables , You can't access non static variable members .

15.  stay  Java  Defines a constructor that does nothing and has no parameters .........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

Scroll to Top