JAVA Interview Related

Source: Internet
Author: User
Tags finally block modifier thread class wrapper stringbuffer

1. What is the difference between int and integer?

A: Java is a nearly pure object-oriented programming language, but for the convenience of programming or the introduction of basic data types, but in order to be able to use these basic data types as Object operations, Java for each basic data type has introduced the corresponding wrapper type (wrapper class), The wrapper class for int is integer, and the automatic boxing/unpacking mechanism is introduced from Java 5, allowing the two to be converted to each other.
-Original type: boolean,char,byte,short,int,long,float,double
-Package Type: boolean,character,byte,short,integer,long,float,double

2. Explain the use of the In-memory stack (stack), heap, and static area

A: Usually we define a variable of a basic data type, a reference to an object, and a field save for a function call that uses the in-memory stack space, while the object created by the new keyword and the constructor is placed in the heap space; the literal in the program (literal) is 100, "Hello" written directly And constants are placed in the static zone. Stack space is the fastest, but the stack is small, usually a large number of objects are placed in the heap space, in theory, the entire memory is not used by other processes in the space or even the hard disk virtual memory can be used as a heap space.
String str = new string ("Hello");
In the above statement, the variable str is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed in the static area.
How many objects have you created?
A: Two objects, one is the "hello" of the static zone, and the other is the object created on the heap with new.

3. Can I inherit the String class?

A: The String class is the final class and cannot be inherited.

4. What is the difference between string and StringBuilder, StringBuffer?

A: The Java platform provides two types of strings: string and Stringbuffer/stringbuilder, which can store and manipulate strings. Where string is a read-only string, meaning string content referenced by string cannot be changed. The string object represented by the Stringbuffer/stringbuilder class can be modified directly. StringBuilder is introduced in Java 5, and it is exactly the same as the StringBuffer method, except that it is used in a single-threaded environment because all aspects of it are not synchronized decorated. Therefore, it is also more efficient than stringbuffer.

5. The difference between overloading (overload) and overriding (override). Can overloaded methods be differentiated according to the return type?

A: The overloads and overrides of a method are implemented in a polymorphic way, except that the former implements the polymorphism at compile time, while the latter implements the runtime polymorphism. Overloads occur in a class, and methods with the same name are considered overloaded if they have different parameter lists (different parameter types, different number of arguments, or both); Overrides occur between subclasses and parent classes, and overrides require subclasses to be overridden by methods that have the same return type as the parent class, which is better accessed than the overridden method of the parent class. You cannot declare more exceptions than the parent class is overridden by the method (the Richter substitution principle). Overloads do not have special requirements for return types.

6. Char can not be stored in a Chinese character, why?

A: The char type can store a Chinese character because the encoding used in Java is Unicode (no specific encoding is selected, the character is used directly in the character set, this is the only way to unify), and a char type is 2 bytes (16 bits), so putting a Chinese is no problem.

7. What are the similarities and differences between abstract classes and interfaces (interface)?

A. Abstract classes and interfaces are not instantiated, but you can define references to abstract classes and interface types. B. If a class inherits an abstract class or implements an interface that requires all of its abstract methods to be implemented, the class still needs to be declared as an abstract class. Interfaces are more abstract than abstract classes, C. Because constructors can be defined in an abstract class, there can be abstract methods and concrete methods, and the constructors are not defined in the interface and the methods are all abstract methods. D. Members in an abstract class can be private, default, protected, public, and members of the interface are all public. Member variables can be defined in an abstract class, whereas member variables defined in an interface are actually constants. Classes with abstract methods must be declared as abstract classes, and abstract classes may not necessarily have abstract methods.

8. Can the abstract method be static at the same time (static), can it be a local method (native), and can be synchronized modified at the same time?

Answer: No. Abstract methods require subclasses to rewrite, and static methods cannot be overridden, so they are contradictory. Local methods are methods implemented by local code, such as C code, and abstract methods are not implemented and are contradictory. Synchronized is related to the implementation details of the method, and the abstract method does not involve implementation details, and therefore is contradictory to each other.

9. Describe the difference between a static variable and an instance variable.

A: A static variable is a variable modified by the static modifier, also known as a class variable, which belongs to a class, not to any object of the class, and a class has no matter how many objects are created, the static variable has only one copy in memory, and the instance variable must be dependent on an instance, first creating the object and then accessing it through the object Static variables can be implemented to allow multiple objects to share memory.

10. Is it possible to make a call to a non-static (Non-static) method from within a static method?

A: No, static methods can only access static members, because calls to non-static methods create objects first, and when static methods are called, they may not be initialized.

11. How do I implement object cloning?

Answer: There are two ways:
1). Implement the Cloneable interface and override the Clone () method in the object class;
2). Implement the Serializable interface, through the serialization and deserialization of objects to achieve cloning, you can achieve true deep cloning, the code is as follows.

12. Is the interface inheritable (extends) interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete Class)?

A: Interfaces can inherit interfaces, and multiple inheritance is supported. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes or they can inherit abstract classes.

13. Can a ". Java" source file contain more than one class (not an inner class)? What are the restrictions?

A: Yes, but there can be at most one public class in a source file and the file name must be fully consistent with the class name of the public class.

What are the uses of the final keyword in Java?

A: (1) Modifier class: Indicates that the class cannot be inherited, (2) The modification method: means that the method cannot be overridden, (3) A modifier variable: Indicates that a variable can only be assigned once and the value cannot be modified (constant).

There is a return statement in try{}, then the code in the finally{} immediately following this try will not be executed, when is it executed, before or after the return?

A: Execution occurs before the method returns the caller.
Note: It is not good to change the return value in Finally, because if a finally code block exists, the return statement in the try does not return the caller immediately, but instead records the return value until the finally code block finishes executing and returns its value to the caller. Then, if you modify the return value in Finally, the modified value is returned. Obviously, returning or modifying the return value in the finally will be very disturbing to the program, and C # directly in the way of compiling errors to prevent the programmer to do such a nasty thing, in Java can also raise the compiler's grammar check level to generate warnings or errors, where in eclipse can be set , it is strongly recommended that you set this to a compilation error.

16. Explain the difference between final, finally and finalize.

-final: Modifier (keyword) has three usages: if a class is declared final, it means that it can no longer derive a new subclass, i.e. it cannot be inherited, so it and abstract are antonyms. Declaring variables as final ensures that they are not changed in use, and that the variable declared as final must be given the initial value at the time of declaration, whereas in subsequent references only the non-modifiable can be read. A method that is declared final can also be used only and cannot be overridden in a subclass.
-finally: usually put in try...catch ... The subsequent construct always executes the code block, which means that the program executes either normally or unexpectedly, the code here can be executed as long as the JVM is not closed, and the code that frees the external resource is written in the finally block.
-the method defined in the Finalize:object class allows the use of the Finalize () method in Java to do the necessary cleanup before the garbage collector clears the object from memory. This method is called by the garbage collector when the object is destroyed, and by overriding the Finalize () method, you can defragment the system resources or perform other cleanup work.

What is the difference between collection and collections?

A: Collection is an interface, which is the parent interface of the set, list and other containers; collections is a tool class that provides a series of static methods to assist container operations, including searching for containers, sorting, threading, and so on.

What are the characteristics of the List, Map, and set three interfaces when accessing elements?

The list accesses elements in a specific index and can have duplicate elements. Set cannot hold repeating elements (using the Equals () method of the object to distinguish whether the element is duplicated). Map holds the key-value pair (Key-value pair) mapping, and the mapping relationship can be one-to-many. Both the set and map containers have two implementations based on the hash store and the sorting tree, and the hash-based version theory accesses the time complexity to O (1), while the implementation based on the sort tree version, when inserting or deleting elements, forms a sort tree by the key (key) of the element or element to achieve the sorting and deduplication effect.

What is the difference between the sleep () method of the thread class and the Wait () method of the object that allows the thread to pause execution?

A: The Sleep () method (Hibernate) is a static method of the thread class (thread), which causes the current thread to pause execution for a specified time, giving the execution opportunity (CPU) to another thread, but the lock on the object remains, so the sleep time is restored automatically (the thread returns to the ready state, Please refer to the Thread state transition diagram in question 66th). Wait () is a method of the object class, and the Wait () method of the calling objects causes the current thread to discard the lock on the object (the thread pauses execution), into the object's wait pool (wait pools), only the Notify () method of the calling object (or Notifyall () method) to wake the thread in the wait pool into the lock pool, and if the thread re-obtains the lock on the object, it can enter the ready state.

20. What is the difference between the sleep () method of a thread and the yield () method?

A: The ①sleep () method gives other threads the opportunity to run without taking into account the priority of the thread, thus giving the low-priority thread a chance to run, and the yield () method only gives the same priority or higher priority thread the opportunity to run;
The ② thread executes the sleep () method and then goes into a blocking (blocked) state, and the yield () method is transferred to the ready State;
The ③sleep () method declaration throws interruptedexception, and the yield () method does not declare any exceptions;
The ④sleep () method is more portable than the yield () method, which is related to operating system CPU scheduling.

21. How many implementations do you have to write multi-threaded program?

A: There are two ways to implement multithreading before Java 5: One is to inherit the thread class, and the other is to implement the Runnable interface. Both methods define the behavior of the thread by overriding the run () method, which is recommended because inheritance in Java is a single inheritance, a class has a parent class, and if inheriting the thread class can no longer inherit other classes, it is obvious that the runnable interface is more flexible.

22. Explain the JDBC operational database steps

(1) Load driver
(2) Create a link
(3) Create a statement
(4) EXECUTE statement
(5) Processing results
(6) Close resources

What is the difference between statement and PreparedStatement? Which performance is better?

A: Compared to statement, the ①preparedstatement interface represents precompiled statements, and its main advantage is that it can reduce SQL compilation errors and increase the security of SQL (reducing the likelihood of SQL injection attacks) The SQL statements in the ②preparedstatement can be parameterized, avoiding the hassle and insecurity of concatenation of SQL statements with strings, ③ PreparedStatement has significant performance advantages when processing SQL in bulk or executing the same query frequently. Because the database can cache compiled-optimized SQL statements, the next time a statement of the same structure executes, it will be quickly (without having to compile and build the execution plan again).

The approximate difference between ArrayList and LinkedList

1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.

JAVA Interview Related

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: info-contact@alibabacloud.com 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.