Java interview questions, java questions

Source: Internet
Author: User
Tags finally block string to number

Java interview questions, java questions
Common Java interview questions: What if the main method is declared as private?

Answer: It can be compiled normally, but the system prompts "the main method is not public" during running ".

Q: What is the difference between passing references and passing values in Java?

Answer: The pass-through refers to the transfer of the address rather than the value itself, and the transfer of the value is a copy of the transfer of the value.

Q: What should I consider if I want to override the equals method of an object?

Answer: hashCode.

Q: How does Java implement "write once and run everywhere?

Answer: Java programs are compiled into class files consisting of bytecode that can run on any platform. Therefore, Java is platform independent.

Problem: Describe the role of each keyword in the public static void main (String args []) Statement

Answer: public: The main method is the first method called when the Java program runs. Therefore, it must be visible to the Java environment. So the visibility is set to pulic.

Static: the Java platform does not create an instance of this class when calling this method. Therefore, this method must be declared as static.

Void: The main method does not return values.

String is the type of the command line input parameter, and args is the String array that the command line transmits.

Question: = differences from equals

Answer: = compare whether two objects are the same in the memory, that is, they are stored in the same location in the memory. The values of the two String objects are the same, but they may be stored in different places in the memory.

= Compare the reference and the equals method compare the content. The public boolean equals (Object obj) method is provided by the Object and can be rewritten by the subclass. By default, true is returned only when the object is compared with itself. This is equivalent to =. String, BitSet, Date, and File all override the equals method. For two String objects, equal values mean that they contain the same character sequence. For a package class of the basic type, equal values mean that the values of the corresponding basic types 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));               }}


== comparison : true== comparison : trueUsing equals method : truefalseUsing equals method :true
Q: What if the static modifier of the main method is removed?

Answer: The program can be compiled normally. NoSuchMethodError is thrown during running.

Q: Why is the oracle type4 driver called a thin driver?

Answer: oracle provides a type 4 JDBC driver, known as a thin driver. This driver includes a TCP/IP Net8 implementation that oracle implements completely using Java. Therefore, it is platform independent and can be downloaded by the browser at runtime, does not rely on oracle implementation of any client. The client connection string uses the TCP/IP address port instead of the database name tnsname.

Question: describes 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, that is, there is no subclass. This is based on basic type security considerations, such as String and Integer. This also makes it easier to ensure thread security by optimizing the compiler. The final method cannot be rewritten. The final variable value cannot be changed. The finalize () method is called before an object is destroyed or recycled. Finally is usually used for exception handling. It is executed no matter whether an exception is thrown or not. For example, closing a connection is usually completed in a finally block.

Question: What is Java API?

Answer: Java APIs are a collection of a large number of software components. They provide a large number of useful functions, such as GUI components.

Q: What is the GregorianCalendar class?

Answer: GregorianCalendar provides support for Western traditional calendars.

Q: What is the ResourceBundle class?

Answer: ResourceBundle is used to store Resources in the specified language environment. Applications can load these resources according to the language environment at runtime, so as to display resources in different languages.

Q: Why is there no global variable in Java?

Answer: global variables are globally visible. Java does not support globally visible variables because they undermine the reference transparency principle. Global variables cause namespace conflicts.

Q: How can I convert the String type to the Number type?

Answer: The valueOf method of Integer class can convert String to Number. The following is a sample code:

String numString = “1000″;int id=Integer.valueOf(numString).intValue();
Q: What is the SimpleTimeZone class?

Answer: SimpleTimeZone supports calendars.

Q: 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 is at the end of the loop to determine whether the next iteration will continue. The do structure executes the loop body at least once.

Question: What is the Locale class?

Answer: The Locale class is used to dynamically adjust program output based on the language environment.

Q: What is the principle of object-oriented programming?

Answer: There are three main points: polymorphism, inheritance, and encapsulation.

Question: Introduce the inheritance principles

Answer: inheritance allows an object to obtain the attributes of another object. Inheritance allows you to reuse all tested functions and make modifications at a time. All inherited functions take effect at the same time.

Q: What is implicit type conversion?

Answer: implicit type conversion means that a simple type is assigned to another type without explicitly telling the compiler that the conversion has occurred. Not all types support implicit type conversion.

Sample Code:

int i = 1000;long j = i; //Implicit casting
Q: Is sizeof a Java keyword?

Answer: No.

Q: What is the native method?

Answer: The native method is a non-Java code implementation method.

Q: In System. out. println (), what are System, out, And println?

Answer: System is the predefined final class provided by the System. out is a PrintStream object, and println is an overloaded method in the out object.

Question: What is encapsulation, inheritance, and polymorphism?

Answer: Simply put, polymorphism refers to multiple Implementations under a single name. Polymorphism allows an entity to perform different operations in a common way. The specific operation is determined by the actual implementation.

There are three manifestations of polymorphism in Java: Method overloading through inheritance implementation method rewriting through Java interface method rewriting.

Q: What is explicit type conversion?

Answer: explicit type conversion clearly tells the compiler to convert objects.

Sample Code:

long i = 700.20;int j = (int) i; //Explicit casting
Q: What is a Java Virtual Machine?

Answer: The Java virtual machine is a software system that can be transplanted to different hardware platforms.

Q: What is downward type conversion?

Answer: downward conversion refers to the conversion from a common type to a specific type, which is performed downward in the inheritance structure.

Q: What is the Java access modifier?

Answer: The access permission modifier is a keyword that indicates the access permission type of a class member. Use these keywords to restrict the access permissions of program methods or variables. They include:

Public: All classes can access protected. private can be accessed in the same package and all subclasses. Only the classes that belong to the same package can be accessed by default.

Q: What is the parent class of all classes?

Answer: Object.

Q: What are the basic types of Java?

Answer: byte, char, short, int, long, float, double, boolean.

Q: What are the characteristics of static types?

Answer: static variables are bound with classes, rather than instance objects of classes. 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 using the static keyword. Class variables are usually used as constants. Static variables are usually accessed by class names. When the program runs, this variable will be created and will not be destroyed until the program ends. The scope of class variables is the same as that of instance variables. The initial value is the same as the member variable. When the variable is not initialized, a default value is displayed based on its data type. Similarly, static methods belong to class methods rather than class objects. They do not act on class objects or create any class instances. Static methods are final, because rewriting only occurs on class instances. Static methods are bound with classes, not objects. The static method of the parent class is shielded by the static method of the quilt class, as long as the original method is not declared as final. Non-static methods cannot override static methods. That is to say, you cannot change a static method to an instance method in the subclass.

Non-static variables have a separate value for each object instance.

Q: What is the difference between the & operator and the & operator?

Answer: When an & Expression is evaluated, both operands are evaluated. & is more like a shortcut of an operator. When an & Expression is evaluated, the first operand is calculated first. If it returns true, the second operand is calculated. If the value of the first operand is fale, the second operand will not be evaluated.

Q: How does Java handle integer overflow and underflow?

Answer: Java stores the corresponding low-level bytes in the calculation results to the corresponding values based on the type size.

Q: What 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: To declare a variable, We only provide the type and name of the variable. Initialization is not performed. The definition includes two phases: Declaration and initialization: String s; only variable Declaration; String s = new String ("bob"); or String s = "bob"; is the variable definition.

Question: Which parameter transfer type does Java support?

Answer: All Java parameters are passed values. For an object, the passed value refers to the object reference. That is to say, the copy of the original reference and parameter reference all points to the same object.

Q: What is the principle of object encapsulation?

Answer: encapsulation is to bind data and operation data code to an independent unit. This ensures data security and prevents improper 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 arbitrary access to code outside the protection layer.

Q: How do you understand variables?

Answer: The variable is a named memory area for program access. Variables are used to store data. As the program executes, the stored data may change.

Q: What is the numerical increase?

Answer: increasing the value is to convert the index data from a smaller data type to a larger data type for integer or floating point operations. When the value is increased, the byte, char, and short values are converted to the int type. The int type may be extended to long when needed. Long and float may be converted to the double type.

Q: What is the type conversion of Java?

Answer: converting from one data type to another is called type conversion. Java has two types of conversions: explicit and implicit.

Q: What is the first parameter of the string array in the parameters of the main method?

Answer: The array is empty and has no 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 null, but not null.

Q: How can I determine whether the 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 is thrown.

Q: Can multiple classes in the program have the main method at the same time?

Answer: Yes. When the program runs, we will specify the name of the class to run. JVM only searches for the main method in the class you specified. Therefore, multiple classes have the main method and there is no name conflict.

Q: When will static variables be loaded? During compilation or runtime? What is the loading time of static code blocks?

Answer: when the class loader loads the class into JVM, it will create a static variable, which has nothing to do with whether the object is created. When static variables are loaded, memory space is allocated. The code of the static code block is executed only once during the first class initialization. A class can have multiple static code blocks. It is neither a member of the class nor a return value, and cannot be called directly. Static code blocks cannot contain this or super, which are usually initialized with static variables.

Q: Can a class have multiple main methods?

Answer: Yes, but only one main method can have the following signatures:

  public static void main(String[] args) {}

Otherwise, the program cannot be compiled. The compiler will warn you that the main method already exists.

Q: How does JVM work?

Answer: JVM is an abstract computer. Just like a real computer, they will. compile java files. class file (. class files are bytecode files), and then use its interpreter to load bytecode.

Question: If two variable values are exchanged in the same place?

Answer: add the two values and assign them to the first variable. Then, subtract the second variable from the result and assign the value to the second variable. Subtract the second variable from the first variable and assign the value to the first variable. The Code is as follows:

  int a=5,b=10;a=a+b; b=a-b; a=a-b;

You can also use an exclusive or operation. The first method may also cause overflow. The XOR method is as follows: int a = 5, B = 10; a = a + B; B = a-B; a = a-B;

int a = 5; int b = 10;a = a ^ b;b = a ^ b;a = a ^ b;
Q: What is Data encapsulation?

Answer: One way to encapsulate data is to create the set and get methods in the class to access the object's data variables. Generally, the variables are private, while the get and set methods are public. Encapsulation can also be used for data verification during data storage, calculation of data, or introspection (for example, using javabean in struts ). Encapsulate data and functions into an independent structure called Data encapsulation. Encapsulation is to encapsulate the data and associated operation methods into an independent unit so that the associated methods can be used to access the data. Encapsulation provides data security, which is actually a way to hide data.

Q: What is a reflection API? How is it implemented?

Answer: reflection is a function that allows you to view the status and features of a class during running and dynamically manage the class. These functions are provided through reflection APIs of some built-in classes, such as Class, Method, Field, and Constructors. Example: Use the getName method of the Java reflection API to obtain the class name.

Q: Does the JVM itself maintain the cache? Does it allocate objects in the heap, the heap in the operating system, or the heap managed by the JVM itself? Why?

Answer: Yes, the JVM manages the cache itself. It creates objects in the heap and then references these objects in the stack.

Question: What is virtual memory?

Answer: Virtual Memory, also called extended memory, does not actually have physical memory.

Q: Can the method be static or synchronized?

Answer: Yes. If this is done, JVM will obtain the lock on the java. lang. Class instance associated with this object. This is equivalent:

synchronized(XYZ.class) {}
Q: 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());}


Q: What are the features of the transient variable?

Answer: The transient variable is not serialized. For example, when a class implementing the Serializable interface is serialized to ObjectStream, transient variables are not written into the stream. At the same time, when deserialization is returned, the value of the corresponding variable is null.

Q: which containers use the Border layout as their default layout?

Answer: Window, Frame, and Dialog.

Q: What is synchronization?

Answer: synchronization is used to control the access to shared resources among multiple threads to ensure that only one thread can access this resource at a time. In a non-synchronous multi-threaded program, when one thread is modifying a shared variable, another thread may be using or updating its value. Synchronization avoids dirty data.

Synchronize methods:

public synchronized void Method1 () {// Appropriate method-related code.}

Synchronize code blocks within the method:

public myFunction (){    synchronized (this) {            // Synchronized code here.         }}

Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.