first, the Java Basic article-concept
1. Describe the Linux you know:
Linux originated in 1991, 1995 popular free operating system, currently, Linux is the mainstream server operating system, widely used in the Internet, cloud computing, smart phones (Android) and other fields. Because Java is primarily used for server-side development, there are many Linux-deployed environments for Java applications.
The directory structure of the Windows operating system is in drive letter units, C, D, E, and so on, the data is stored under the various drive characters, and the Linux operating system on the top level only a root directory root, all files are stored under this root directory.
Linux is not like the graphics interface of Windows, it is done by command, the common commands are as follows:
A. PWD: Used to display the current working directory;
B. LS: Used to view the current working directory contents;
C. CD: Used to change the current working directory.
2. 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.
Java is designed to allow applications to run on arbitrary platforms without requiring programmers to rewrite or recompile each platform individually. The Java Virtual machine makes this possible because it knows the instruction length and other features of the underlying hardware platform.
What is the 3.JDK, JRE, and JVM relationship?
The JDK (Java Development Kit) is the Java Development Kit, which contains the compilation, runtime, and other development tools necessary to write Java programs, as well as the JRE. Development tools such as the Javac command for compiling Java programs, Java commands for starting the JVM running Java programs, the Javadoc command for generating documents, and JAR commands for packaging, and so on.
The JRE (Java Runtime Environment) is a Java runtime environment that provides the software environment necessary to run Java applications, including Java virtual machines (JVMS) and a rich system class library. The System class library is the Java pre-packaged functional class, just use it directly, can greatly improve the development efficiency.
The JVM (Java virtual machines) is the Java VM that provides the runtime environment support for the bytecode file (. Class).
Simply put, the JDK contains the JRE that contains the JVM.
What are the data types supported by 4.Java? What is auto-unboxing?
Basic data type:
Integer value: Byte,short,int,long,
Character type: Char
Floating-point type: float,double
Boolean Type: Boolean
integer default int, decimal type double by default. The float and long types must be suffixed.
The first thing to know is that a string is a reference type, not a base type, and a variable that refers to a type declares that the variable is actually stored in memory as a reference address, and the entity is in the heap. Reference types include classes, interfaces, arrays, and so on. The string class is also final decorated.
The wrapper class is a reference type, and auto-boxing and unpacking are conversions between the base type and the reference type, as to why the conversion is possible, since the base type is converted to a reference type, the new object can be In order to invoke the wrapper class in the encapsulated method to convert between the basic type or ToString (of course, with the name of the class directly can also be used to see that the method is static), and if you want to store the base type in the collection, the generic qualified type can only be the corresponding wrapper type.
5. What is Object-oriented?
面向对象是一种思想,世间万物都可以看做一个对象,这里只讨论面向对象编程(OOP),Java是一个支持并发、基于类和面向对象的计算机编程语言,面向对象软件开发的优点:代码开发模块化,更易维护和修改;代码复用性强;增强代码的可靠性和灵活性;增加代码的可读性。
- Four basic features of object-oriented:
Abstraction: The process of extracting a key feature of something in the real world and building a model for that thing. For the same thing under different requirements, the characteristics that need to be extracted may differ. The resulting abstract model typically contains: attributes (data) and operations (behaviors). This abstract model we call class. Instantiates a class to get an object.
Encapsulation: Encapsulation enables classes to be independent and isolated, guaranteeing the high cohesion of classes. Exposes only properties and operations that are necessary to the outer or child class of the class. Class-Encapsulated implementation-dependent class modifiers (public, protected, private, and so on)
Inheritance: A reuse mechanism for existing classes. If a class inherits an existing class, the class will have all the non-private attributes (properties and operations) of the inherited class. The inheritance here refers to the inheritance of classes and the implementation of interfaces.
Polymorphism: Polymorphism is achieved on the basis of inheritance. Three elements of polymorphism: inheritance, overrides, and parent class references to child class objects. When a parent class reference points to a different subclass object, the same method is called, rendering a different behavior, or a class polymorphic attribute. Polymorphism can be divided into compile-time polymorphism and run-time polymorphism.
Abstraction, encapsulation, inheritance, and polymorphism are the basis of object-oriented. On the four basic features of object-oriented, we need to follow some basic design principles in object-oriented programming design.
- The seven principles of object-oriented design:
Solid principles (Single duty principle, open closure principle, Richter replacement principle, interface isolation principle and dependency inversion principle)
Dimitri Law
The combination is better than the inheritance principle (synthetic multiplexing principle).
Based on these principles of object-oriented design, the predecessors have summed up some design patterns to solve the different problem scenarios, and the gof23 of Gang of four is most well known.
- 24 Design Modes (GOF23+1):
Create Model:
1. Simple Factory mode (not included in gof23)
2. Factory mode
3. Abstract Factory mode
4. Single-Case mode
5. Prototype mode
Creator mode
6. Structural mode:
7. Combination Mode
8. Decorator Mode
9. Appearance mode
10. Adapter Mode
11. Proxy Mode
12. Enjoy meta mode
13. Bridging mode
Behavioral mode:
14. Observer mode
15. Policy mode
16. State mode
17. Mediation Mode
18. Template Method
19. Command mode
20. Memo Mode
21. Visitor Mode
22. Interpreter Mode
23. Iterator Mode
24. Responsibility Chain Model
This is simply a description of the relationship between the definition and the feature and the design pattern, which is not discussed in detail.
6. Please write down the results of the following expressions, the answer can be written in 10 or 16 binary
1). 0xaa | 0x55
2). & 240
3). 10 ^ 12
4).-2 >> 1
5).-2 >>> 1
1). Analysis: Hex number with 0x ... To indicate that the following hexadecimal bit is four bits, two hexadecimal bits are one byte, up to 8 hexadecimal bits, 32 bytes, for example: 0xFFFFFFFF. or ("| ") operation, all 0 is 0, the other is 1.
So: 0XAA is represented as 10101010 by binary, 0x55 is represented as 01010101, bitwise or followed by 11111111, decimal number is 255, hexadecimal number is 0xFF.
2). Analysis: 10 binary converted to 2 binary, with this number divided by 2, the logger and the remainder, the trader again divided by 2, the logger and the remainder ... Until the upper is 0 or the remainder is 0 stops, the remainder is reversed to form the binary from low to high (the last remainder is the binary lowest bit). With the ("&") operation, the full 1 is 1, the other is 0.
So: 15 equals 1111, 240 equals 11110000, 15 is 0 in front, bitwise AND after 00001111, which results in 00000000
3). Analysis: Also or ("^") operation, the same takes 0, different take 1.
So: 1010 ^ 1100 = 0110, decimal is 6, hexadecimal is 0x06.
4). Analysis: With the symbol right shift (">>"), that is, signed bit, negative sign bit 1, positive sign bit 0,-2 of the binary method is positive negation plus 1, so 2 binary representation is 0000 0000 0000 0000 0000 0000 0000 0010, Take the inverse plus one
1111 1111 1111 1111 1111 1111 1111 1110, which is the binary representation of-2.
Note: >>, <<, >>>, operators are only for int and long, byte, short, char type is required to be converted to int.
So: The signed right shift is 1111 1111 1111 1111 1111 1111 1111 1111, minus one inverse of the sign bit, resulting in a signed decimal number of-1.
5). Analysis: Unsigned Right Shift (">>>"), that is, either positive or negative, the symbol bit is 0 after the right shift.
So:-2 binary unsigned Right shift one followed by 0111 1111 1111 1111 1111 1111 1111 1111, i.e. 2^31-1, two 31-time minus one.
Note: The main difference between right shift and unsigned right shift is the top left 0 or 1, unsigned right shift any time the highest bit is 0, the symbol right shift is a positive complement 0, negative complement 1. (No unsigned left shift!) )。
What is the difference between 7.& and &&?
There are two ways to use the & operator: (1) bitwise and; (2) Logic and. The && operator is short-circuiting and arithmetic. The difference between the logic and the short-circuit is very large, although both require that the Boolean value on both sides of the operator is true the value of the entire expression is true. && is called a short-circuit operation because if the value of the expression on the left side of && is false, the expression on the right will be shorted out directly and will not be evaluated. Most of the time we may need to use && instead of &, for example, to verify that the user's name is not NULL and is not an empty string when validating the login, it should be written as: Username! = null &&!username.equals ("") , the order of the two cannot be exchanged, not the & operator, because if the first condition is not true, the equals of the string cannot be compared, otherwise a nullpointerexception exception will be generated. Note: Logical OR operator (|) and short-circuit or operator (| | The difference is also true.
8. What is value passing and reference passing?
A value pass is a basic variable, passing a copy of the variable, changing the copy without affecting the original variable.
A reference pass is generally for an object type variable, passing a copy of the object's address, not the original object itself.
It is generally assumed that the delivery within Java is a value pass. The delivery of an instance object in Java is a reference pass.
9. Can I access non-static variables in the static environment?
The static 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.
What does the method overlay (overriding) and method overloading (overloading) in 10.Java mean?
Method overloads in Java occur when the same class has two or more methods with the same name but different parameters. In contrast, the method overrides the method that the subclass redefined the parent class. Method overrides must have the same method name, parameter list, and return type. The override may not restrict access to the methods it covers.
In 11.Java, what is a construction method? What is a constructor method overload? What is a copy construction method?
The construction method is called when the new object is created. Each class has a method of construction. The Java compiler creates a default constructor for this class in cases where the programmer does not give the Class A constructor method.
Construction method overloading and method overloading are similar in Java. You can create multiple construction methods for a class. Each construction method must have its own unique argument list.
Java does not support copy construction methods like in C + +, because Java does not create a default copy construction method if you do not write your own construction method.
Does 12.Java support multiple inheritance?
Classes in Java do not support multiple inheritance, only single inheritance (that is, a class has only one parent). However, the interface in Java supports multiple inheritance, that is, a sub-interface can have multiple parent interfaces. (The function of the interface is to extend the function of the object, a sub-interface inherits multiple parent interfaces, indicating that the sub-interface extends several functions, when the class implements the interface, the class expands the corresponding function).
13. Explain the usage of the in-memory stack (stack), heap, and method area.
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 stack space in the JVM, while objects created with the new keyword and the constructor are placed in the heap space, and the heap is the main area of the garbage collector management, because the garbage collector now uses the Generational collection algorithm , so the heap space can also be subdivided into Cenozoic and Laosheng generation, and then the specific point can be divided into Eden, Survivor (also can be divided into from Survivor and to Survivor), tenured; The method area and the heap are all memory regions shared by each thread. Used to store data such as class information, constants, static variables, and JIT compiler-compiled code that have been loaded by the JVM; the literal in the program (literal), such as 100, "Hello" and constant are placed in the constant pool, the constant pool is part of the method area. Stack space operation is the fastest but the stack is very small, usually a large number of objects are placed in the heap space, stack and heap size can be adjusted by the JVM's startup parameters, the stack space is exhausted will cause stackoverflowerror, The heap and constant pool space are not sufficient to cause outofmemoryerror.
14. What is the difference between an interface and an abstract class?
From the design level, abstraction is the abstraction of a class, a template design, an interface is an abstraction of behavior, and a norm of behavior.
Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:
All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
A class can implement many interfaces, but only one abstract class is inherited
Classes can not implement all methods of abstract classes and interface declarations, and of course, in this case, the class must also be declared abstract.
An abstract class can implement an interface without providing an interface method implementation.
The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.
You can also refer to the differences between abstract classes and interfaces in JDK8.
15. Calculate 2 times 8 with the most efficient method?
A: 2 << 3 (left 3 is the equivalent of multiplying by 2 by 3, and the right shift 3 is equivalent to dividing by 2 3).
16. Handwriting Singleton mode (a hungry man and full-han mode) and Factory mode?
(1) Single case A hungry man mode://A Hungry man type Singleton class. When class is initialized, it is self-instantiated
2 public class Singleton1 {
3//Private default constructor
4 Private Singleton1 () {}
5//already self-instantiated
6 private static final Singleton1 single = new Singleton1 ();
7//Static Factory method
8 public static Singleton1 getinstance () {
9 return single;
10}
11}
(2) Lazy mode://Lazy single-case class. Instantiated at the first call
2 public class Singleton2 {
3//Private default constructor
4 Private Singleton2 () {}
5//Note that there is no final
6 private static Singleton2 single=null;
7//Static Factory method
8 public synchronized static Singleton2 getinstance () {
9 if (single = = null) {
Ten single = new Singleton2 ();
11}
return single;
13}
14}
(3) Factory mode:
Interface ifactory{
Public iproduct createproduct ();}
Class Factory implements ifactory{
Public IProduct createproduct () {return new Product ();}}
public class client{
Public Static void Main (String [] args) {ifactory factory=new factory ();
IProduct product=factory.createproduct ();
Product. Productmethod ();}}
The difference between 17.String and StringBuilder, StringBuffer?
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.
Ii. Java Basics-Collections and Arrays
What is a 18.Java set frame? What are some of the advantages of a collection framework ?
There are collections in each programming language, and the original Java version contains several collection classes: Vector, Stack, Hashtable, and array. With the extensive use of the collection, Java1.2 proposes a collection framework that encompasses all the set interfaces, implementations, and algorithms. Java has been going on for a long time with the use of generics and concurrent collection classes in a thread-safe way. It is also included in the Java bundle, blocking interfaces, and their implementations. Some of the advantages of the collection framework are as follows:
(1) Use the core collection classes to reduce development costs rather than implementing our own collection classes.
(2) with the use of rigorously tested collection framework classes, the code quality will be improved.
(3) You can reduce code maintenance costs by using the collection classes that are included with the JDK.
(4) reusability and operability.
19. What are the advantages of generics in the collection framework?
Java1.5 introduces generics, and all of the collection interfaces and implementations use it in large quantities. Generics allow us to provide a set of object types that can accommodate a collection, so if you add any element of another type, it will be wrong at compile times. This avoids classcastexception at run time, because you will get an error message at compile time. Generics also make the code neat, and we don't need to use explicit conversions and instanceof operators. It also benefits the runtime because it does not produce byte-code instructions for type checking.
What are the basic interfaces of the 20.Java collection framework?
Collection is the root interface for the collection hierarchy. A collection represents a set of objects that are the elements of that object. The Java platform does not provide any direct implementation of this interface. # # title # #
Set is a collection that cannot contain duplicate elements. This interface models the abstraction of a mathematical set and is used to represent a collection, just like a deck of cards.
A list is an ordered collection that can contain repeating elements. You can access any element by its index. The list is more like an array of length dynamic transformations.
Map is an object that maps a key to value. A map cannot contain duplicate keys: Each key can only map one value at a maximum.
Some of the other interfaces are queue, Dequeue, SortedSet, SortedMap, and Listiterator.
21. Why collection not inherit from Cloneable and serializable interfaces ?
The collection interface specifies a set of objects, which are the elements of the object. How to maintain these elements is determined by the specific implementation of collection. For example, some collection implementations such as list allow repeating elements, while others such as set are not allowed. Many collection implementations have a public clone method. However, it is meaningless to put it in all implementations of the collection. This is because collection is an abstract representation. It is important to realize.
When dealing with specific implementations, the semantics and meaning of cloning or serialization only work. Therefore, a specific implementation should decide how to clone or serialize it, or whether it can be cloned or serialized.
Authorizing cloning and serialization in all implementations ultimately leads to less flexibility and more restrictions. A particular implementation should determine whether it can be cloned and serialized.
22. Why does the map interface not inherit the collection interface?
Although the map interface and its implementation are part of the collection framework, the map is not a collection and the collection is not a map. Therefore, map inheritance collection meaningless, and vice versa.
If map inherits the collection interface, then where does the element go? Map contains the Key-value pair, which provides a way to extract a collection of key or value lists, but it is not suitable for a "set of object" specifications.
23. What is an iterator (Iterator)?
The iterator interface provides many ways to iterate over a collection element. Each collection class contains an iterative method that can return an iterator instance. Iterators can delete elements of the underlying collection during the iteration, but they cannot call the collection's remove (Object Obj) directly, and can be removed through the Remove () method of the iterator.
What is the difference between 24.Iterator and listiterator?
Their differences are listed below:
The iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
Iterator to a collection can only be forward traversal, listiterator can be either forward or back.
Listiterator implements the iterator interface and includes other functions such as adding elements, replacing elements, getting the index of the previous and subsequent elements, and so on.
25. What is the difference between fast failure (fail-fast) and security failure (fail-safe)?
Fast failure: When you iterate a collection, a Concurrentmodification exception is thrown if another thread is modifying the collection you are accessing.
Under the Java.util package is a quick failure.
Security failure: You will be able to make a copy of the underlying collection when you iterate, so you will not be affected when you modify the upper collection, and will not throw a concurrentmodification exception.
Under the Java.util.concurrent package are all security failures.
How does the HashMap in 26.Java work?
We know that the two most commonly used structures in Java are arrays and analog pointers (references), and almost all data structures can be combined using both, HashMap. In fact, HashMap is a "chain table hash", the following is its data structure: The leftmost is an array, each element in the array is a linked list, each element of the list is entry.
HashMap is based on the principle of hashing, we use put (key, value) to store objects into HashMap, using get (key) to get objects from HashMap. When we pass the key and value to the put () method, we first call the Hashcode () method on the key, and the returned hashcode is used to locate the bucket position to store the entry object.
27. What happens when a hashcode of two objects is the same?
Because hashcode are the same, their buckets are in the same position and ' collisions ' will occur. Because HashMap uses a linked list to store objects, this entry (Map.entry object with key-value pairs) is stored in the linked list.
28. If the hashcode of the two keys is the same, how do you get the value object?
When we call the Get () method, HashMap uses the hashcode of the key object to find the bucket position, and then calls the Keys.equals () method to find the correct node in the linked list and finally finds the value object to find.
What is the importance of the 29.hashCode () and Equals () methods?
HashMap uses the hashcode () and Equals () methods of the Key object to determine the index of the key-value pair. These methods are also used when we try to get the values from the HashMap. If these methods are not implemented correctly, in this case, two different keys may produce the same hashcode () and Equals () outputs, HashMap will think they are the same, then overwrite them instead of storing them in different places. Similarly, all collection classes that do not allow the storage of duplicate data use Hashcode () and Equals () to find duplicates, so it is important to implement them correctly. Implementations of Equals () and hashcode () should follow these rules:
(1) if O1.equals (O2), then o1.hashcode () = = O2.hashcode () is always true.
(2) if o1.hashcode () = = O2.hashcode (), does not mean that o1.equals (O2) will be true.
Refer to http://blog.csdn.net/javazejian/article/details/51348320 for details
What is the difference between 30.HashMap and Hashtable?
1, HashMap is non-thread-safe, Hashtable is thread-safe.
2, HashMap the key and value are allowed to have a null value exists, and Hashtable is not.
3, because of the problem of thread safety, hashmap efficiency is higher than Hashtable.
4, Hashtable is synchronous, and HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multithreaded environments.
Generally it is not recommended to use Hashtable,① is Hashtable is a legacy class, the internal implementation of a lot of not optimized and redundant. ② even in the multi-threaded environment, there are now synchronous concurrenthashmap substitution, there is no need for multithreading and Hashtable.
31. How do I decide whether to choose HashMap or TreeMap?
HashMap is the best choice for operations such as inserting, deleting, and locating elements in a map. However, if you need to traverse an ordered set of keys, TreeMap is a better choice. Based on the size of your collection, it may be quicker to add elements to the HashMap, changing the map to TreeMap for an orderly key traversal.
What are the similarities and differences between 32.ArrayList and vector?
ArrayList and vectors are very similar in many cases.
(1) Both are index-based and internally supported by an array.
(2) Both maintain the order in which they are inserted, and we can get the elements according to the insertion order.
(3) The iterator implementations of ArrayList and vectors are fail-fast.
(4) Both ArrayList and vectors allow null values, or you can use index values to randomly access elements.
Here are the different points of ArrayList and vectors.
(1) vector is synchronous, and ArrayList is not. However, if you seek to change the list at the time of iteration, you should use Copyonwritearraylist.
(2) ArrayList is faster than vector, it is not overloaded because of synchronization.
(3) ArrayList is more general because we can easily get synchronization lists and read-only lists using the Collections tool class.
What is the difference between 33.Array and ArrayList? When is it more appropriate to use an array?
Arrays can hold basic types and objects, whereas ArrayList can only hold objects.
The array is of the specified size, and the ArrayList size is fixed.
The array does not provide as many functions as ArrayList, such as AddAll, RemoveAll, and iterator. Although ArrayList is obviously a better choice, there are times when the array is more useful.
(1) If the size of the list has been specified, most of the cases are stored and traversed.
(2) for traversing the base data type, although collections uses auto-boxing to alleviate the encoding task, working on a list of basic types of a specified size can also become slow.
(3) If you want to use a multidimensional array, use [] [] than the list
Java Interview 2018 FAQ topic Summary (i) (EXT)