Home > Java > javaTutorial > Share a summary of interview questions in Java

Share a summary of interview questions in Java

零下一度
Release: 2017-06-28 16:02:04
Original
1090 people have browsed it

1) What is Java Virtual Machine? Why is Java called a "platform-independent programming language"?

The Java virtual machine is a virtual machine process that can execute Java bytecode. Java source files are compiled into bytecode files that can be executed by the Java virtual machine.
Java is designed to allow applications to run on any platform without requiring programmers to rewrite or recompile separately for each platform. The Java virtual machine makes this possible because it knows the instruction length and other characteristics of the underlying hardware platform.

2) What is the difference between JDK and JRE?

The Java Runtime Environment (JRE) is the Java virtual machine that will execute Java programs. It also contains the browser plug-ins required to execute the applet. The Java Development Kit (JDK) is a complete Java software development kit, including JRE, compiler and other tools (such as JavaDoc, Java debugger), which allows developers to develop, compile, and execute Java applications.

3) What does the "static" keyword mean? Is it possible to override a private or static method in Java?

The "static" keyword indicates that a member variable or member method can be accessed without an instance variable of the class to which it belongs.
Static methods in Java cannot be overridden because method overrides are based on dynamic binding at runtime, while static methods are statically bound at compile time. Static methods are not related to any instance of the class, so conceptually they do not apply.

4) Is it possible to access non-static variables in a static environment?

static variables belong to classes in Java, and their values ​​are the same in all instances. When a class is loaded by the Java virtual machine, static variables are initialized. If your code attempts to access non-static variables without using an instance, the compiler will report an error because these variables have not yet been created and are not associated with any instance.

5) What are the data types supported by Java? What is an automatic unpacking box?

The 8 basic data types supported by the Java language are: byte, short, int, long, float, double, boolean, char
Autoboxing is the Java compiler’s function in basic data types and A conversion between corresponding object packaging types. For example: convert int to Integer, double to Double, etc. Otherwise, it is automatically unboxed.

6) What do method overriding and method overloading in Java mean?

Method overloading in Java occurs when two or more methods in the same class have the same method name but different parameters. In contrast, method overriding means that the subclass redefines the method of the parent class. Method overrides must have the same method name, parameter list and return type. An overrider may not restrict access to the method it overrides.

7) What is a constructor in Java? What is constructor overloading? What is a copy constructor?

When a new object is created, the constructor will be called. Every class has a constructor. In the case where the programmer does not provide a constructor for the class, the Java compiler will create a default constructor for the class.
Constructor overloading and method overloading in Java are very similar. Multiple constructors can be created for a class. Each constructor must have its own unique parameter list.
Java does not support copy constructors like C++. This difference is because Java will not create a default copy constructor if you do not write the constructor yourself.

8) Does Java support multiple inheritance?

Classes in Java do not support multiple inheritance, but only single inheritance (that is, a class has only one parent class). However, interfaces in Java support multiple inheritance, that is, a sub-interface can have multiple parent interfaces. (The function of an interface is to extend the functions of an object. A sub-interface inherits multiple parent interfaces, indicating that the sub-interface extends multiple functions. When a class implements the interface, the class extends the corresponding functions).

9) What is the difference between interface and abstract class?

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, but the difference is:
All methods in the interface are implicitly abstract. An abstract class can contain both abstract and non-abstract methods.
A class can implement many interfaces, but it can only inherit one abstract class.
The class does not need to implement all the methods declared by the abstract class and interface. Of course, in this case, the class must also be declared abstract. .
Abstract classes can implement interfaces without providing interface method implementations.
Variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.
Member functions in Java interfaces are public by default. Member functions of an abstract class can be private, protected or public.
Interface is absolutely abstract and cannot be instantiated. An abstract class cannot be instantiated, but it can be called if it contains a main method.

10) What are pass by value and pass by reference?

The object is passed by value, which means that a copy of the object is passed. Therefore, even if you change the object copy, the value of the source object will not be affected.
Objects are passed by reference, which means that what is passed is not the actual object, but a reference to the object. Therefore, changes made externally to the referenced object will be reflected in all objects.
11) What is the difference between a process and a thread?

A process is an executing application, and a thread is an execution sequence within the process. A process can have multiple threads. Threads are also called lightweight processes.

12) How many different ways are there to create a thread? Which one do you prefer? Why?

There are three ways to create threads:
Inherit the Thread class
Implement the Runnable interface
Applications can use the Executor framework to create a thread pool
This way of implementing the Runnable interface is more efficient Popular because this does not require inheriting the Thread class. In the case where other objects have been inherited in the application design, this requires multiple inheritance (and Java does not support multiple inheritance), and only interfaces can be implemented. At the same time, the thread pool is also very efficient and easy to implement and use.

13) Briefly explain the several available states of threads.

New, runnable, running, blocking, dead


14) What is the difference between synchronized methods and synchronized code blocks?

Difference:
The synchronization method uses this or the current class object as the lock by default;
The synchronization code block can choose what to lock, which is more granular than the synchronization method. We can choose to synchronize only the part of the code that will cause synchronization problems instead of the entire method;

15) How is thread synchronization done inside the monitor (Monitor)? What level of synchronization should the program do?

Monitors and locks are used together in the Java virtual machine. The monitor monitors a synchronized code block to ensure that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread is not allowed to execute synchronized code before acquiring the lock.

16) What is deadlock?

A deadlock occurs when two or more processes are waiting for other processes to finish executing before they can continue. The result is that all processes are stuck in an infinite wait.

17) How to ensure that N threads can access N resources without causing deadlock?

When using multi-threading, a very simple way to avoid deadlock is to specify the order in which locks are acquired, and force threads to acquire locks in the specified order. Therefore, if all threads lock and release locks in the same order, there will be no deadlock.

18) What are the basic interfaces of the Java collection class framework?

The collection class interface specifies a set of objects called elements. Each concrete implementation class of the collection class interface can choose to save and sort the elements in its own way. Some collection classes allow duplicate keys, while others do not.
Java collection classes provide a set of well-designed interfaces and classes that support operations on a set of objects. The most basic interfaces in Java collection classes are:
Collection: represents a group of objects, each object is its child element.
Set: Collection that does not contain duplicate elements.
List: An ordered collection that can contain repeated elements.
Map: An object that can map keys to values. Keys cannot be repeated.

19) Why does the collection class not implement the Cloneable and Serializable interfaces?

The semantics and meaning of cloning or serialization are related to the specific implementation. Therefore, it is up to the specific implementation of the collection class to determine how it is cloned or serialized.

20) What is an iterator?

The Iterator interface provides many methods for iterating over collection elements. Every collection class contains iteration methods that return iterator instances. Iterators can delete elements of the underlying collection during the iteration process, but they cannot be deleted by directly calling the remove(Object Obj) of the collection. They can be deleted through the remove() method of the iterator.


The above is the detailed content of Share a summary of interview questions in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template