Java face question

Source: Internet
Author: User
Tags bitwise serialization stringbuffer

This article is transferred from hope900 blog, if there is infringement, please inform the deletion

Blog links are as follows: https://blog.csdn.net/hope900

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: xor ("^") operation, same fetch 0, different fetch 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 basic type pass in Java is a value pass. The delivery of an instance object in Java is a reference pass.

For example, writing a method to exchange values for two primitive types is not possible, and if it is a wrapper class, you can because the wrapper class passes a reference to the object

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). Can be roughly understood as: single inheritance, multi-implementation

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.

Java face question

Related Article

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.