Part I: Java Basics1. What is a Java virtual machine? Why is Java called a "platform-agnostic programming language"?
A Java Virtual machine is a virtual machine process that can execute Java bytecode. The Java source file is compiled into a bytecode file that can be executed by the Java virtual machine.
One compile, run everywhere (premise: There is a JVM virtual machine) because Java is a virtual machine-based language, and its execution is: now compiled , compiled into a bytecode (. Class) file, at the time of execution through the interpreter
The bytecode file is interpreted as a machine language based on the instruction length and other features of the underlying hardware platform , which is then run
2. Nouns in Java: What are the JDK,JRE,JVM?
- JDK:JDK is a complete Java software development package that includes a JRE, a compiler, and other tools (such as the Javadoc,java debugger) that enable developers to develop, compile, and execute Java applications.
- JRE:JRE is the Java Runtime Environment, which contains the JVM virtual machine and Java System Class library (lang packet, etc.)
- JVM:JVM is a specification for computing devices, a fictional computer that is implemented by simulating various computer functions on a real computer (so it knows the instruction length and other characteristics of the underlying hardware platform ).
3. What does the "static" keyword mean? Is it possible to overwrite (override) a private or static method in Java?
The static field indicates that the member is loaded with the class, so it can be accessed without instantiating the object
The static method in Java cannot be overwritten because method overrides are bound based on runtime dynamic (when the object is instantiated), and the static method is statically bound at compile time. The static method is not relevant to any instances of the class, so it is conceptually not applicable.
The method of private field decoration cannot be overwritten, because, you do not know that there is this method exists
4. Is it possible to access non-static variables in a static environment?
No, thestatic variable belongs to the class in Java, and it has the same value in all instances. Static variables are initialized when the class is airborne into Java virtual. If your code tries to access a non-static variable without an instance, the compiler will give an error because the variables have not been created yet and are not associated with any instances.
5. What are the data types supported by Java? What is auto-unboxing?
- Byte
- Short
- Int
- Long
- Float
- Double
- Boolean
- Char
Auto-Boxing is a conversion of the Java compiler between the base data type and the corresponding object wrapper type. For example: Convert int to integer,double into double, and so on. The reverse is automatic unpacking.
6. What does method overlay (overriding) and method overloading (overloading) in Java mean?
Method overrides: Subclasses override methods of the parent class (must have the same method name, parameter list, and return type)
Method overloading: In the same class, one or two or more methods of the same name form an overloaded
7. What is a constructor in Java? What is a constructor overload?
When a new object is created, the constructor is called. Each class has a constructor function. The Java compiler creates a default constructor for this class in cases where the programmer does not provide a constructor for the class.
Constructor overloading and method overloading are similar in Java. You can create multiple constructors for a class. Each constructor must have its own unique argument list.
8. Does Java support multiple inheritance?
The Java class does not support multiple inheritance. Each class can inherit only one class, but it can implement multiple interfaces.
Java interfaces can be multi-inherited, inheriting multiple interfaces
9. What is the difference between an interface and an abstract class?
same Point :
are located at the top of the inheritance for implementation or inheritance by other classes ;
Can not instantiate objects directly ;
All contain abstract methods , and their subclasses must overwrite these abstract methods ;
difference :
Abstract classes provide implementations for some of the methods, which prevent subclasses from repeating these methods and improve code reuse ; interfaces can contain only abstract methods ;
A class can inherit only one direct parent class (perhaps an abstract class ), but may implement multiple interfaces ;(interface compensates for Java's single inheritance )
Abstract class is what you should have in this thing , the inheritance system is an IS . A relationship
Interface is the extra content of this thing , the inheritance system is a like . A relationship
Inheriting class + implementing interface ==> He is a man (class), but he is like a Thief (interface)
The choice of the two :
Preferential selection of interface, as far as possible to use abstract classes ;
Abstract classes are required to define the behavior of subclasses and to provide common functionality for subclasses .
10. What is value passing and reference passing?
An 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 is not affected.
object is passed by reference, meaning that it is not the actual object, but the reference to the object. Therefore, changes made externally to the referenced object are reflected on all objects.
Part Two: threading1. What is the difference between a process and a thread?
A process is an application that executes, and a thread is an execution sequence within a process. A process can have multiple threads. Threads are also called lightweight processes.
2. What are the different ways to create threads? Which one do you like?
Method 1: Inherit the thread class, overriding the Run method
public class Subthread extends thread{public subthread () { super ("x5456"); Modify the thread name by constructing the method } public void Run () {for (int i=0;i<100;i++) { System.out.println (super.getname () + i);}} }
Call
public static void Main (string[] args) { //Create an object that just inherits the subclass of the thread class Subthread st = new Subthread (); Through the SetName method, modify the thread name st.setname ("x54256"); Call the object's Start method, which automatically executes our rewritten Run method St.start (); for (int i=0;i<100;i++) { System.out.println (Thread.CurrentThread (). GetName () +i); Gets the object of the current thread, calling the GetName () method }}
Method 2: Implement Interface runnable, override the Run method
public class Subrunnable implements runnable{public void Run () {for (int i=0;i<100;i++) { try { // Call the Thread class's sleep method, Hibernate 50ms, because the parent interface has no throws exception, so we can only use Try...catch thread.sleep; } catch ( Interruptedexception e) { e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () + "..." +i);}}
Call
public static void Main (string[] args) { //Create an object for the class that implements the Runnable interface subrunnable sr = new subrunnable (); Create the thread class object thread t = new Thread (SR); Start thread t.start (); for (int i=0;i<100;i++) { System.out.println (Thread.CurrentThread (). GetName () + "..." +i);} }
Implementing the Runnable interface is more popular because this does not require inheriting the Thread class. This requires multiple inheritance (while Java does not support multiple inheritance) in cases where other objects have been inherited in the design of the application, and only interfaces can be implemented. At the same time, the thread pool is very efficient and easy to implement and use.
3, a summary of the explanation of the number of available states of the thread.
Java Basics--On