Classic Java basic interview questions, welcome to collect and share.
Question: What happens if the main method is declared private?
Answer: It can compile normally, but it will prompt "the main method is not public" when running.
Question: What is the difference between passing reference and passing value in Java?
Answer: Passing a reference means passing an address rather than the value itself, and passing a value is a copy of the value.
Question: If you want to rewrite the equals method of an object, what else should you consider?
Answer: hashCode.
Question: How is Java's "write once, run everywhere" implemented?
Answer: Java programs will be compiled into a class file composed of bytecodes, which can run on any platform, so Java is platform-independent.
Question: Explain the role of each keyword in the public static void main(String args[]) statement
Answer: The public: The main method is the first method called when a Java program runs, so it must be visible to the Java environment. So the visibility is set to pulic.
static: The Java platform will not create an instance of this class when calling this method, so this method must be declared static.
void: main method has no return value.
String is the type of parameter passed in the command line, and args refers to an array of strings passed in the command line.
Question: ==Difference from equals
Answer: == Compare whether two objects are the same object in memory, which means that the storage locations in memory are the same. The values stored by the two String objects are the same, but they may be stored in different places in memory.
==Comparison is the reference, while the equals method compares the content. public boolean equals(Object obj) This method is provided by an Object object and can be rewritten by subclasses. The default implementation will return true only when the object is compared with itself, and at this time it is equivalent to ==. String, BitSet, Date, and File all override the equals method. For two String objects, equal values means they contain the same character sequence. For a wrapper class with a basic type, equal values means that the corresponding basic type's values are the same.
public class EqualsTest { public static void main(String[] args) { String s1 = "abc"; String s2 = s1; String s5 = "abc"; String s3 = new String("abc"); String s4 = new String("abc"); System.out.println("== comparison : ” + (s1 == s5)); System.out.println("== comparison : ” + (s1 == s2)); System.out.println("Using equals method : ” + s1.equals(s2)); System.out.println("== comparison : ” + s3 == s4); System.out.println("Using equals method : ” + s3.equals(s4)); }}result:
== comparison : true
== comparison : true
Using equals method : true
false
Using equals method :true
Question: What happens if the static modifier of the main method is removed?
Answer: The program can compile normally. A NoSuchMethodError exception will be thrown during runtime.
Question: Why is the oracle type4 driver called a thin driver?
Answer: Oracle provides a type 4 JDBC driver, called a thin driver. This driver contains an implementation of TCP/IP Net8 in Oracle that is fully implemented in Java. Therefore, it is platform-independent and can be downloaded by the browser at runtime and does not rely on any client's oracle implementation. The client connection string uses the TCP/IP address port, not the tnsname of the database name.
Question: Let me introduce the finalize method
Answer: final: constant declaration. Finally: Handle exceptions. finalize: Helps with garbage collection.
The variables declared in the interface are final by default. The final class cannot be inherited, which means there are no subclasses. This is done for basic types of security reasons, such as String and Integer. This also allows the compiler to make some optimizations and makes it easier to ensure thread security. The final method cannot be rewritten. The value of the final variable cannot be changed. The finalize() method will be called before an object is destroyed and recycled. Finally, it is usually used for exception handling, regardless of whether or not an exception is thrown, it will be executed. For example, closing the connection is usually done in a finally block.
Question: What is the Java API?
Answer: Java API is a collection of a large number of software components that provide a lot of useful features, such as GUI components.
Question: What is the GregorianCalendar class?
Answer: GregorianCalendar provides support for traditional Western calendars.
Question: What is the ResourceBundle class?
Answer: ResourceBundle is used to store resources in a specified locale. Applications can load these resources according to the runtime locale, thereby providing display of different languages.
Question: Why are there no global variables in Java?
Answer: Global variables are globally visible, and Java does not support globally visible variables because: global variables destroy the principle of reference transparency. Global variables cause namespace conflicts.
Question: How to convert String type to Number type?
Answer: The valueOf method of the Integer class can convert a String into a Number. Here is a code example:
String numString = "1000";
int id=Integer.valueOf(numString).intValue();
Question: What is the SimpleTimeZone class?
Answer: SimpleTimeZone provides Gregorian date support.
Question: What is the difference between a while loop and a do loop?
Answer: The while structure determines whether the next iteration should continue at the beginning of the loop. The do/while structure determines whether the next iteration will continue at the end of the loop. The do structure will execute the loop body at least once.
Question: What is the Locale class?
Answer: The Locale class is used to dynamically adjust the program's output according to the locale environment.
Question: What are the principles of object-oriented programming?
Answer: There are three main points: polymorphism, inheritance and encapsulation.
Question: Introduction to the principles of inheritance
Answer: Inheritance allows one object to obtain the properties of another object. Using inheritance allows the tested functions to be reused and can be modified at once, and all inheritance places take effect at the same time.
Question: What is implicit type conversion?
Answer: Implicit type conversion is simply assigning one type to another type, without explicitly telling the compiler that the conversion has occurred. Not all types support implicit type conversion.
Code example:
int i = 1000;
long j = i; //Implicit casting
Question: Is sizeof a keyword in Java?
Answer: No.
Question: What is the native method?
Answer: The native method is a non-Java code implementation method.
Question: In System.out.println(), what are System, out, and println?
Answer: System is a predefined final class provided by the system. Out is a PrintStream object, and println is an overloaded method in the out object.
Question: What are encapsulation, inheritance and polymorphism?
Answer: Simply put, polymorphism refers to multiple implementations of one name. Polymorphism allows an entity to implement different operations in a common way. The specific operation is determined by actual implementation.
There are three ways of manifesting polymorphism in Java: method overloading is implemented through inheritance and method rewriting through Java interface.
Question: What is an explicit type conversion?
Answer: Explicit type conversion explicitly tells the compiler to convert objects.
Code example:
long i = 700.20;
int j = (int) i; //Explicit casting
Question: What is a Java virtual machine?
Answer: Java virtual machines are software systems that can be ported to different hardware platforms.
Question: What is type downconversion?
Answer: Down conversion refers to converting from a common type to a specific type, which is performed downward on the inheritance structure.
Question: What is the access modifier for Java?
Answer: The access modifier is a keyword that indicates the access type of a class member. Use these keywords to define access to methods or variables for programs. They contain:
public: All classes can access protected: All subclasses can access private: Only the attributed classes can access default: The attributed classes and subclasses under the same package can access
Question: What are the parent classes of all classes?
Answer: Object.
Question: What are the basic types of Java?
Answer: byte,char, short, int, long, float, double, boolean.
Question: What are the characteristics of static types?
Answer: Static variables are bound to the class, not instance objects of the class. Each instance object shares the same static variable. That is to say, a class has only one static variable, no matter how many objects it has. Class variables or static variables are declared through the keyword static. Class variables are usually used as constants. Static variables are usually accessed by class names. When the program is running, this variable will be created until the program is finished and will not be destroyed. The scope of class variables is the same as that of instance variables. Its initial value is the same as member variables. When the variable is not initialized, there will be a default value according to its data type. Similarly, static methods are methods that belong to classes, not class objects. Their calls do not act on class objects, and they do not need to create any class instances. Static methods themselves are final, because rewriting will only happen on class instances. Static methods are bound to the class, not objects. The static methods of the parent class will be blocked by the static methods of the child class, as long as the original method is not declared as final. Non-static methods cannot override static methods, that is, you cannot change a static method into an instance method in a subclass.
Non-static variables have a separate copy of the value on each object instance.
Question: What is the difference between the & operator and the && operator?
Answer: When an & expression is evaluated, both operands will be evaluated, and && is more like a shortcut to an operator. When an && expression evaluates, the first operand is calculated first, and if it returns true, the second operand will be calculated. If the first operand is taken as false, the second operand will not be evaluated.
Question: How does Java handle the overflow and underflow of integers?
Answer: Java stores the corresponding low-order bytes in the calculation result into the corresponding value according to the size of the type.
Question: What happens if public static void is written as static public void?
Answer: The program is compiled and run normally.
Question, what is the difference between declaring variables and defining variables?
Answer: Declare variables We only provide the type and name of the variable and do not initialize it. The definition includes two stages: declaration and initialization String s; just variable declaration, String s = new String("bob"); or String s = "bob"; is variable definition.
Question: Which parameter passing type does Java support?
Answer: Java parameters are all passed values. For an object, the passed value is a reference to the object, that is, the copy of the original reference and the parameter reference both point to the same object.
Question: What are the principles of object encapsulation?
Answer: Encapsulation is to bind the data and the code that operates the data to a separate unit. This ensures the security of the data and prevents the incorrect use of external code. Objects allow programs and data to be encapsulated to reduce potential interference. Another understanding of encapsulation is to act as a protective layer for data and code to prevent random access to code outside the protective layer.
Question: How do you understand variables?
Answer: A variable is a named memory area for access by the program. Variables are used to store data, and as the program is executed, the stored data may also change.
Question: What is numerical improvement?
Answer: Numerical enhancement refers to the conversion of data from a smaller data type to a larger data type for integer or floating point operations. During the process of numerical increase, the byte, char, short values will be converted into int type. When needed, the int type may also be promoted to long. Long and float may be converted to double types.
Question: What is the type conversion of Java?
Answer: Converting from one data type to another is called type conversion. Java has two types of conversion methods, one is explicit type conversion and the other is implicit.
Question: What is the first parameter of the string array in the parameters of the main method?
Answer: The array is empty, without any elements. Unlike C or C++, the first element is the program name by default. If the command line does not provide any parameters, the String array in the main method is empty, but not null.
Question: How to determine whether an array is null or empty?
Answer: The output value of array.length. If it is 0, the array is empty. If it is null, a null pointer exception will be thrown.
Question: Can multiple classes be allowed to own at the same time and have main methods in the program?
Answer: Yes. When the program runs, we will specify the running class name. The JVM will only look for main methods in the class you specified. Therefore, there is no naming conflict between multiple classes having main methods.
Question: When do static variables load? Compile or runtime? What is the timing of static code block loading?
Answer: When the class loader loads the class into the JVM, static variables will be created, which has nothing to do with whether the object is created. Memory space will be allocated when static variables are loaded. The code of the static code block will only be executed once when the class is initialized for the first time. A class can have multiple static code blocks, it is not a member of the class, has no return value, and cannot be called directly. Static code blocks cannot contain this or super, they are usually initialized with static variables.
Question: Can a class have multiple main methods?
Answer: Yes, but only one main method has the following signature:
public static void main(String[] args) {}
Otherwise the program will not be compiled. The compiler will warn you that the main method already exists.
Question: Let me briefly introduce how JVM works?
Answer: JVM is an abstract computer. Just like a real computer, they will first compile the .java file into a .class file (the .class file is the bytecode file), and then use its interpreter to load the bytecode.
Question: What if the values of two variables are exchanged in place?
Answer: First add two values and assign values to the first variable, then subtract the second variable from the result and assign values to the second variable. Then subtract the second variable with the first variable and assign the value to the first variable at the same time. The code is as follows:
int a=5,b=10;a=a+b; b=ab; a=ab;
Use XOR to switch. The first method may also cause overflow. The method of XOR is as follows: int a=5,b=10;a=a+b; b=ab; a=ab;
int a = 5; int b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
Question: What is data encapsulation?
Answer: One way to encapsulate data is to create set and get methods in the class to access the object's data variables. Generally speaking, variables are private, while the get and set methods are public. Encapsulation can also be used to verify data when storing data, or to calculate data, or to use it as introspection (such as using javabeans in struts). Encapsulating data and functions into a separate structure is called data encapsulation. Encapsulation is actually encapsulating data and associated operation methods into an independent unit, so that data can be accessed using these associated methods. Encapsulation provides data security, which is actually a way to hide data.
Question: What is the reflection API? How is it implemented?
Answer: Reflection refers to the function of being able to view the status and characteristics of a class during runtime and being able to perform dynamic management. These functions are provided through some built-in class reflection APIs, such as Class, Method, Field, Constructors, etc. Example of use: Use the getName method of the Java reflection API to get the class name.
Question: Will the JVM itself maintain the cache? Is it allocated objects in the heap, or is the heap of the operating system or the heap managed by the JVM itself? Why?
Answer: Yes, the JVM itself manages the cache, which creates objects in the heap and then references those objects in the stack.
Question: What is virtual memory?
Answer: Virtual memory is also called extended memory, and there is actually no real physical memory.
Question: Can the method be static and synchronized at the same time?
Answer: Yes. If you do this, the JVM acquires the lock on the java.lang.Class instance associated with this object. Doing so is equal to:
synchronized(XYZ.class) {
}
Question: What is the difference between String and StringTokenizer?
Answer: StringTokenizer is a tool class used to split strings.
StringTokenizer st = new StringTokenizer("Hello World"); while (st.hasMoreTokens()) { System.out.println(st.nextToken());}
Output:
Hello
World
Question: What are the characteristics of transient variables?
Answer: The transient variable will not be serialized. For example, when a class that implements the Serializable interface is serialized to the ObjectStream, variables of type transient will not be written to the stream. At the same time, when deserialized back, the corresponding variable value is null.
Question: Which containers use Border layout as their default layout?
Answer: Window, Frame, Dialog.
Question: How to understand what synchronization is?
Answer: Synchronization is used to control the access of shared resources between multiple threads to ensure that only one thread can access this resource at the same time. In an asynchronously protected multi-threaded program, when a thread is modifying a shared variable, another thread may also use or update its value. Synchronization avoids the generation of dirty data.
Synchronize the methods:
public synchronized void Method1 () {// Appropriate method-related code.}Synchronize the code blocks inside the method:
public myFunction (){ synchronized (this) { // Synchronized code here. }}The above is a compilation of the information for Java interview questions. We will continue to add relevant information in the future. Thank you for your support to this site!