Java basics (Java interview questions)

1. The origin of java?

In January 1991, Sun was established.
In February 1991, it was originally named "Oak".
In July 1994, Java was born. Oak changed its name to Java.
In June 1999, three versions of Java were released: J2SE, J2EE, J2ME.
2009 4 , Oracle acquired Sun

2. The advantages and disadvantages of JAVA?

Four advantages
(1) Cross-platform implementation through the JVM, one-time compilation everywhere
(2) Object-oriented thinking, easy to develop, low learning cost
(3) Garbage collection mechanism automatically manages object recycling
(4) Java restricts direct operations on memory It is completed by the JVM, which improves development efficiency.
Two shortcomings
(1) require a JVM operating environment and are not suitable for developing desktop applications.
(2) The packaged API needs to be transferred to the OS through the JVM, which brings efficiency problems.

3. What is the difference between J2SE, J2EE and J2ME?

J2SE Standard Edition, Application Software Development
J2EE Enterprise Edition, Web Development
J2ME Micro Edition, Mobile Device Development

4. How does java achieve a compile run everywhere?

The bytecode file generated by the Java compiler is a file that can be run everywhere. After the operating system is installed with a virtual machine, the virtual machine can convert the bytecode file into a language that can be recognized by the operating system

5. Three characteristics of java

Encapsulation: provide security,
inheritance: reduce duplicate code, improve reusability
Polymorphism: use parent class references to point to subclass objects, reflecting the flexibility of java

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

JVM: a virtual machine process that can recognize bytecode files, is the core of Java cross-platform
JRE: Java Runtime Environment (Java Runtime Environment), JRE is for Java users, not developers, if you download and install With JRE, the system can only run JAVA programs, including JVM standard implementation and JAVA core class libraries, excluding JAVA development tools (compiler, debugger, etc.)
JDK: JDK (Java Development Kit) (Java development kit), provided Compiler (javac, compile java files into class files) and running environment (JVM, used to parse class files to make them run), you can develop and run JAVA, JDK is the core of JAVA

7. Eight basic data types (to be determined)

See the mind map for details

8. What is packing? What is unboxing? (To be determined)

Starting with JDK1.5, it provides the function of automatic boxing.
Boxing: The process of changing the basic type to the type of wrapper. integer i = Integer.valueOf (int);
Unboxing: the process of changing the wrapper type to a basic type. int i = Integer.intValue ();
(1) Including arithmetic operations will trigger automatic unboxing.
(2) There are a lot of automatic boxing processes. If the packaging object returned by the boxing is not obtained from the cache, many new objects will be created, which consumes more memory.
Integer a = 100; // Boxing, originally
int b = new Integer (100); // Unboxing

9. Nine common Object methods?

(1) HashCode HashCode is the same, the value is not necessarily the same
(2) equals
(3) toString
(4) wait
(5) notify
(6) notifyAll
(7) finalize
(8) getClass
(9) clone method

10. What is the difference between the four access control modifiers?

(1) private can only be accessed by this class
(2) default can only be accessed by the same package
(3) protected can only be accessed by the same package and subclass
(4) public can be accessed

11. The difference between overloading and rewriting?

The method name of the two methods is the same. The number of parameters, the position of the parameter, and the type of the parameter are inconsistent. It is called overloading
. The subclass inherits the parent class, and the modified inherited method is called rewriting.

12. What is polymorphism? Tell me about understanding polymorphism

The parent class reference points to the subclass object The
advantage is flexibility, the disadvantage is that the subclass method cannot be called

13. What is the difference between the four reference types in java?

Strong reference: Object o = new Object (); GC will not collect strongly referenced objects. . . The method has an exception, the method exits the stack, and all local variables will be recycled, so strong references in local variables will be recycled.
Soft references: When the JVM heap memory is full, the soft referenced objects will be recycled. Generally used Do cache (SoftReference object)
weak reference: every time gc will be recycled (WearkReferenve)
virtual reference: Object o = new Object (); o = null; remind our gc to recycle this object

14. What are the common internal classes?

Member internal class
Static internal class
Method internal class
Anonymous internal class

15. What is the difference between reference passing and value passing? (imperfect)

The Java programming language has only value-passing parameters.
Basic data type value passing.
Reference type reference passing.

16. Why is the upward and downward transformation?

Upward transformation (automatic), subclassing into parent class, dynamic binding, is safe Person p = new Student (), because the attribute does not have polymorphism, the overloaded method has polymorphism
downcast (mandatory) It is not safe to convert the parent class into a subclass. You have to go up first before you can go down. After the up transition, the extra attributes and methods of the subclass are gone. It is safe to go down. Will not report an error, there will be unexpected operation

17. What is hashCode ()? (imperfect)

Hash value, int value obtained by hash algorithm for data of any length

18. Can the Constructor be rewritten?

Constructors cannot be inherited and therefore cannot be rewritten, but can be overloaded

19. What is the role of the Math class? What are the commonly used methods?

Contains methods for basic mathematical operations, use
max (), min (), compareTo (), random (), isLetter () together with the Number class to determine whether it is a letter
round: normal conditions return rounding, negative. 5 decimals return larger Integer, such as -1.5 returns -1.
ceil: Returns the larger value between two integers where the decimal is located, such as -1.5 returns -1.
tail: returns the smaller value between two integers where the decimal is located, such as -1.5 returns -2.

20. What is the Number class?

Abstract parent class of all wrapper classes, java.lang package

21. What are the types in Switch?

(1) byte, short, int, no problem, it will be automatically converted to int
(2) long, float, double will report an error, but you can convert them to int in parentheses, there is no problem
(3) char, JDK1. 7 Start String can also
(4) boolean can not be converted to int, no

22、

23. What happens when two objects of Integer type are compared with ==?

(1) Values ​​are not equal: false
(2) Values ​​are equal, ranging from -128 to 127: true
(3) Values ​​are equal, ranging from -128 to 127: false
(4) Because the JVM automatically maintains eight basic Constant pools of data types will be automatically unboxed for value comparison in the int range, and reference types are not used for comparison in the range
Integer.valueOf () and xx.intValue ()

24. What is the difference between & and &&?

(1) Both & and && can be used as a logical AND operation, the entire operation result is true when both sides are true, and && has a short-circuit function
(2) When both sides of & are a type, they are converted into a binary system, and bitwise AND is performed Operation

25. short s1 = 1; s1 = s1 + 1; what's wrong?

The short type + int type will be automatically converted to int type, and an error requiring forced conversion will be reported

26、

27、

28. The difference between static and final?

1. Static modification means static or global. The modified properties and methods belong to the class, which can be accessed by class name. Static property / method name
2. Static modified code block means static code block, when the Java Virtual Machine (JVM) loads the class The code block will be executed when it is executed. It will only be executed once.
3. The static-modified attribute, that is, the class variable, is created and initialized when the class is loaded. It will only be created once.
4. This method cannot be used in the static method. And the super keyword, only static methods or variables can be called
5, static methods must be implemented, not abstract abstract
6, static methods can only be overridden by static methods

1. The final modification means a constant. Once created, it cannot be changed.
2. The member variable of the final tag must be assigned at the same time as the declaration, or in the constructor of the class. It cannot be reassigned.
3. The final method cannot be rewritten by subclasses.
4. The final class cannot be inherited and has no subclasses. The methods in the final class are final by default.
5. final cannot be used to modify the constructor.
6. The private type methods are by default.

Static can modify the code block of the class, not final.
Static can not modify local variables in the method, final can.

29. The abstract method cannot be used with static, why?

Static modification is a static method, and abstract modification is an abstract method, no method body method, can not call the
abstract method requires subclass rewriting, and static methods cannot be rewritten, the two contradict

30. What are the types of code blocks? When is it executed? what's the effect? (To be determined)

Static code block> construct code block> construct method;

31. What is the difference between deep cloning and shallow cloning?

The cloned object needs to implement the Cloneable interface. The shallow clone only copies the reference type and basic type of the reference type in the object, and the deep clone copy all, that is, the reference type object in the object will also be copied.

32. How to deep clone and shallow clone? What scene will be used?

33. The role of instanceof

Left instanceof right: determine whether the left is an instance of the right.
Compilation status: the right side can be the left subclass of the parent class itself.
Running status: the right side can only be the left parent class or its own class, otherwise false

34. What is the difference between String, StringBuffer and StringBuilder?

String can not change the object, the change will re-create the object, StringBuffer, StringBuilder can be changed, StringBuffer thread safety, StringBuilder
thread is not safe, but single-threaded StringBuilder efficiency is relatively high

35. What is the difference between equals and ==?

Equals is a comparison value. When inheriting the Object class, the equals method should be rewritten appropriately. In the String class, equals is to use == to compare the addresses. The same value of the address must be the same. Use instanceof to determine whether it is of the String type. Determine whether the characters are the same length and the same length, then compare whether each character is equal,

== is the comparison address

36. What is the difference between replace and replaceAll in String objects?

Replace method: supports the replacement of characters and strings.
replaceAll method: string replacement based on regular expressions.
It was once believed that replace is the first matching character or string replacement, and replaceAll is the replacement of all matching characters or strings in the target string.

37. What did the new String () virtual machine do?

(1) To find the current object class
(2) Cannot find the current object class (because it is the first time to load), load the current object class
(3) Open up space for the current object
(4) Initialize attributes (parent class attributes It will also be initialized before the parent class)
(5) Perform the constructor

38. What are the commonly used methods of String?

split (String regex) splits this string
charAt () into a char [] array
equals ( ) according to the matching of the given regular expression, compares whether the two strings are equal
getBytes () returns byte [] array
length () returns characters string length
indexOf (String i) returns the index
hashCode () returns the hash code of
the substring (the beginIndex int, int endIndex)
the toLowerCase () lowercase
toUpperCase () are converted to upper
trim () ignore leading and trailing whitespace

39. String s = "a" + "b" + "c" + "d" How many objects were created?

An object
javac compilation can directly add and subtract string constants

40. Three kinds of string inversion

(1)new StringBuffer(str).reverse().toString()
(2)str.subString(1)+str.chatAt(0)递归
(3)new String(char[])

41. Is there a length () method for arrays? Is there a length () method for Strings?

Arrays do not have a length () method and have a length property. String has length () method.

42. What is the role of transient?

Prevent attributes from being serialized

43. What is an enumerated type? what is the benefit?

44. What are the benefits of using binary computing?

45. What is an iterator?

Not very clear, just know that it is a traversal method

46. ​​Have you used JDK8? What are the characteristics?

There are some simple Lambdas (expressions) to simplify code with annotations

47. What are the advantages of lambda expressions? Besides simplifying the code?

Published 52 original articles · Likes2 · Visits 1857

Guess you like

Origin blog.csdn.net/qq_42972645/article/details/105642987