2019 Java interview questions the basis of series 228 (5), quick look at what you will not?

2019 Java interview questions foundation Series 228

Java interview questions (a)

1 to update the first to answer 20 questions resolved

juejin.im/post/5de8c6…

The second update answer 21 to 50 questions to resolve
juejin.im/post/5dea1d...

Part III Updated 51 to answer 95 questions to resolve
juejin.im/post/5decd3...


Java interview questions (b)

Title IV Update 1 to answer 20 questions resolved


Java interview questions (b)

21, there are two classes, B inherits A, C inherits B, we can convert C B Why? Such as C = (C) B;
22, which contains the clone class method? Is Cloneable or Object?
23, Java in the ++ operator is thread safe?
23, not thread-safe operations. It involves multiple instructions, such as read a variable value, increase, and then stored back into memory, this process may have multiple threads cross.
24, a = a + b and a + = b distinction
25, I will be able to assign a double value in the case without a cast to type long variable it?
What 26,3 == * 0.1 0.3 will be returned? true or false?
27, int and Integer which will take up more memory?
28, why in Java String is immutable (Immutable)?
31,64-bit JVM, int length is the majority?
32, the difference between Serial and Parallel GC?
33, 32-bit and 64-bit JVM, int type variable length of the majority?
34, Java and in WeakReference SoftReference difference?
35, WeakHashMap how it works?
36, JVM option -XX: + UseCompressedOops What is the role? Why should I use?
37, how to judge by JVM Java program is 32-bit or 64-bit?
38,32-bit and 64-bit JVM JVM maximum heap memory are the majority?
39. What is the difference between JRE, JDK, JVM and JIT?
40, explained Java heap space and GC?
41, you can guarantee GC execute it?
42, how to get memory Java program uses? Heap percentage to use?
43, Java heap and stack What is the difference?
44, "a == b" and "a.equals (b)" What is the difference?
45, a.hashCode () what is the use? What is the relationship with a.equals (b)?
46, differs from the final, finalize and finally in?
47, what compile-time constants in Java is? Use it What are the risks?
48, the difference (answer) between List, Set, Map and Queue
49, () the difference between the poll method and remove () method?
50, what is the difference in Java and LinkedHashMap PriorityQueue is?

Benpian update answer questions 21-50


21, there are two classes, B inherits A, C inherits B, we can convert C B Why? Such as C = (C) B;

It belongs to the cast, if not converted B Example C type, there will be abnormal
For example, ABC correspond to your animal, cat, black cat.
For example, the transition is up
C c = new C();
B b = c;
You c transition to B, black cat is a cat? Yes ah, so this is ok of.
But in turn,
B b = new B();
C c = (C)b;
This is not ok, just know that b is a cat, he is not necessarily a black cat.
But if it has been determined that b is a black cat, and it may be a transition
B b = new C();
C c = (C)b;
b here has always been black cat ah.

22, which contains the clone class method? Is Cloneable or Object?

java.lang.Cloneable is a mark of the interface does not contain any method, clone method is defined in the object class. And need to know the clone () method is a local method, which means that it is implemented by the c or c ++ or other local languages.

23, Java in the ++ operator is thread safe?

Not thread-safe operations. It involves multiple instructions, such as read a variable value, increase, and then stored back into memory, this process may have multiple threads cross.

24, a = a + b and a + = b distinction

+ = Implicitly result type of the add operation type is cast as a result of holding. If the two are integers the sum, such as byte, short or int, int type will first raised to, and then performs an addition operation. If the result of the addition operation is larger than a maximum value, then a + b will be a compilation error, but
byte a = 127;
byte b = 127;
b = a + b;
// error : cannot convert from int to byte
b += a;
// ok复制代码
(Translator's Note: This place should be expressed is wrong, in fact, no matter how much the value of a + b, the compiler will error, because the operation will be a + b a, b promoted to int type, it will be assigned to the byte int type will compile error)

25, I will be able to assign a double value in the case without a cast to type long variable it?

No, you can not type conversion is not mandatory under the premise of a double value assigned to a variable of type long, because the type double broader than long type, it is necessary to be cast.

What 26,3 == * 0.1 0.3 will be returned? true or false?

false, because some floating point numbers can not be completely accurate representation of it.

27, int and Integer which will take up more memory?

Integer object will take up more memory. Integer is an object, the object needs to store metadata. But int is a primitive type of data, so take up less space.

28, why in Java String is immutable (Immutable)?

String immutable in Java because the Java designers thought very frequently used string, the string is set to be allowed to share the same immutable string between multiple clients.

29, we can use it in the String Switch in?

From Java 7, we can use strings in switch case, but this is just a syntactic sugar. Internal implemented in a switch of the string hash code.

30, what is the constructor chain in Java is?

When you call another constructor from a constructor, Java is the constructor chain. This situation will only appear in the overloaded constructor of the class time.

31,64-bit JVM, int length is the majority?

Java, int type variable length is a fixed value, regardless of the platform, are 32-bit. That means, in 32-bit and 64-bit java virtual machine, int type is the same length.

32, the difference between Serial and Parallel GC?

When Serial and Parallel execution of the GC will cause stop-the-world. The main difference between them is the serial collector default copying collector, when only one thread to perform GC, and GC parallel collector uses multiple threads to execute.

33, 32-bit and 64-bit JVM, int type variable length of the majority?

32-bit and 64-bit JVM, int type variable length are the same, are 32 bits or 4 bytes.

34, Java and in WeakReference SoftReference difference?

Although WeakReference and SoftReference are conducive to improving the efficiency of GC and memory, but WeakReference, once lost the last strong reference, GC will be recycled, while the soft references although it can not stop being recycled, but can be delayed until the JVM memory shortage.

35, WeakHashMap how it works?

WeakHashMap HashMap normal operation is similar, but incorporated a weak key, meaning that when the key object has no references to time, key / value will be recovered.

36, JVM option -XX: + UseCompressedOops What is the role? Why should I use?

When you migrate your applications from 32-bit to 64-bit JVM JVM, because the object pointer from 32 bits to 64 bits, so the heap memory will suddenly increase, to almost double. This data will also CPU cache (memory capacity than the much smaller) adversely affected. Because the migration to 64-bit JVM main motivation is that you can specify the maximum heap size, you can save some memory by compressing OOP. By -XX: + UseCompressedOops option, JVM 32 uses the OOP, rather than 64-bit OOP.

37, how to judge by JVM Java program is 32-bit or 64-bit?

You can check some property to get the system information such as sun.arch.data.model or os.arch.

38,32-bit and 64-bit JVM JVM maximum heap memory are the majority?

In theory the 32-bit JVM heap memory can reach 2 ^ 32, ie 4GB, but in fact will be much smaller than this. The difference between different operating systems, such as Windows system about 1.5 GB, Solaris about 3GB. 64-bit JVM allows you to specify the maximum heap memory, in theory, can be up to 2 ^ 64, which is a very large number, in fact, you can specify the heap size to 100GB. Even some JVM, such as Azul, heap memory to 1000G are possible.

39. What is the difference between JRE, JDK, JVM and JIT?

On behalf of JRE Java Runtime (Java run-time), it is necessary to run Java reference. On behalf of Java development tools JDK (Java development kit), is a Java program development tools such as Java compiler, which also contains the JRE. On behalf of the Java Virtual Machine JVM (Java virtual machine), it is the responsibility of running Java applications. Representative JIT time compilation (Just In Time compilation), when the number of code execution exceeds a certain threshold, will be Java bytecodes into native code, e.g., a hot key code will be replaced with the quasi-native code, is advantageous so large magnitude improve the performance of Java applications.

40, explained Java heap space and GC?

When starting the process of Java through the Java command will allocate memory for it. A portion of memory space for creating a heap, when an object is created in the program, it allocates memory for the space. GC is a process within the JVM, invalid objects recovered memory for future allocation.

41, you can guarantee GC execute it?

No, although you can call System.gc () or Runtime.gc (), but there is no way to guarantee the implementation of the GC.

42, how to get memory Java program uses? Heap percentage to use?

The remaining memory may be acquired, and the maximum total memory and heap memory by an associated method java.lang.Runtime class. You can also get a percentage of the remaining space and heap memory heap used by these methods. By Runtime.freeMemory () method returns the number of bytes of free space () method of the total number of bytes of memory Runtime.totalMemory, Runtime.maxMemory () Returns the maximum number of bytes of memory.

43, Java heap and stack What is the difference?

The JVM heap and stack belong to different memory areas, the use of different purposes. Preservation methods commonly used in the frame stack and local variables, and the object is always allocated on the heap. Heap stack is usually relatively small, it will not be shared among multiple threads, the heap is shared by all threads across the JVM.

44, "a == b" and "a.equals (b)" What is the difference?

If a and b are the object, a == b is the reference to compare two objects, returns true only when the same object in the heap a and b will be directed, and a.equals (b) is a logical comparison, is typically required to override this method to provide a consistent comparison logic. For example, String class overrides equals () method, can be used for two different objects, but the same comparison the letters included.

45, a.hashCode () what is the use? What is the relationship with a.equals (b)?

hashCode () method is the integer corresponding object hash value. It is used to hash-based collections, such as Hashtable, HashMap, LinkedHashMap like. It equals () method is particularly close relationship. The Java specification, using two equal () method to determine equal objects, must have the same hash code.

46, differs from the final, finalize and finally in?

is a final modifier, can be modified variables, methods and classes. If the final modified variables, mean value of the variable can not be changed after initialization. finalize method is a method called before the object is recovered, the last chance to revive the object itself, but when no call finalize guarantee. finally is a keyword, and try and catch together for exception handling. finally block will be executed, regardless of whether an exception occurs in the try block.

47, what compile-time constants in Java is? Use it What are the risks?

Public static immutable (public static final) variable is what we call the compile-time constants, public here is optional. In fact, these variables will be replaced at compile time, because the compiler knows the value of these variables, and know not to change these variables at runtime. One problem with this approach is that you use an in-house or third-party libraries in public compile-time constants, but this value is behind other people changed, but your clients still using the old values, even if you have deployed a new jar. To avoid this, when you update the dependent JAR file, make sure to recompile your program.

48, the difference (answer) between List, Set, Map and Queue

List is an ordered collection of elements allow repeat. Some of its implementation can provide constant access to time-based index values, but this is not a guarantee of the List interface. Set is an unordered collection.

49, () the difference between the poll method and remove () method?

poll () and remove () are removed from a queue element, but poll () Gets the element at the time of failure will return empty, but remove () will throw an exception on failure.

50, what is the difference in Java and LinkedHashMap PriorityQueue is?

PriorityQueue ensure the highest or lowest priority element is always at the head of the queue, but LinkedHashMap order to maintain the order of the elements is inserted. When traversing a PriorityQueue, order no guarantee, however LinkedHashMap class traversal order is the order to ensure the insertion element.

At last

Welcome to the concern I kind of public-ho [programmers] Herd, finishing the 1000 and 2019 a number of companies java face questions over 400-page pdf document, the article will be updated on the inside, data compilation will be on the inside.

Article I remember like the point of a concern like yo, thanks for the support!


Guess you like

Origin juejin.im/post/5def4dc86fb9a0161104c1f2