Java basic interview questions and answers (a)

Java Fundamentals

1. JDK and JRE What is the difference?

  • JDK: Java Development Kit for short, java development kit that provides java development environment and runtime environment.

  • JRE: Java Runtime Environment abbreviation, java runtime environment, provides the necessary environment for the java runtime.

Specifically JDK actually contains the JRE, also contains the source code to compile java compiler javac, also it contains a lot of java debugging and analysis tools. In short: if you need to run java program, simply install the JRE on it, if you need to write java program, you need to install JDK.

 

2. What is the difference == and equals that?

== Reading

For == basic types of reference types and different effects, as follows:

  • Basic types: the comparison values ​​are identical;

  • Reference types: whether the comparison is a reference to the same;

Code Example:

1 String x = "string";
2 String y = "string";
3 String z = new String("string");
4 System.out.println(x==y); // true
5 System.out.println(x==z); // false
6 System.out.println(x.equals(y)); // true
7 System.out.println(x.equals(z)); // true

Code Interpretation: Because x and y refer to the same reference, so == is true, and the new String () method is to rewrite opened up memory space, so == evaluates to false, and equals comparisons has been the value, The results are true.

Interpretation of equals

On essentially equals ==, but String and Integer, etc. rewrite the equals method, turning it into a comparison value. Look at the following code to understand.

First look Comparative equals a default value of a case where the object of the same, as follows:

 1 class Cat {
 2     public Cat(String name) {
 3         this.name = name;
 4     }
 5 
 6     private String name;
 7 
 8     public String getName() {
 9         return name;
10     }
11 
12     public void setName(String name) {
13         this.name = name;
14     }
15 }
16 
17 Cat c1 = new Cat("王磊");
18 Cat c2 = new Cat("王磊");
19 System.out.println(c1.equals(c2)); // false

The output To our surprise, turned out to be false? This is how it looked equals the source code to know the source code as follows:

1 public boolean equals(Object obj) {
2     return (this == obj);
3 }

== essentially equals the original.

That the question is, String two objects of the same value, why return is true? code show as below:

1 String s1 = new String("老王");
2 String s2 = new String("老王");
3 System.out.println(s1.equals(s2)); // true

Similarly, when we enter the equals method String to find the answer, the code is as follows:

 1 public boolean equals(Object anObject) {
 2     if (this == anObject) {
 3         return true;
 4     }
 5     if (anObject instanceof String) {
 6         String anotherString = (String)anObject;
 7         int n = value.length;
 8         if (n == anotherString.value.length) {
 9             char v1[] = value;
10             char v2[] = anotherString.value;
11             int i = 0;
12             while (n-- != 0) {
13                 if (v1[i] != v2[i])
14                     return false;
15                 i++;
16             }
17             return true;
18         }
19     }
20     return false;
21 }

The original String rewrite the equals method of Object, changed the reference to compare the comparison value.

Summary  : == is a comparison of the value of the basic types, for reference types, it is more of a reference; and equals default is a reference to compare, but many like the equals method again, such as String, Integer and so turn it into a comparison value, generally equals the value of the comparison is equality.

 

hashCode 3. two objects () are the same, equals () certainly is true, right?

Right, hashCode two objects () the same, equals () is not necessarily true.

Code Example:

1 String str1 = "通话";
2 String str2 = "重地";
3 System.out.println(String.format("str1:%d | str2:%d",  str1.hashCode(),str2.hashCode()));
4 System.out.println(str1.equals(str2));

Execution of results:

1
2
3
str1: 1179395  | str2: 1179395
 
false

Code Interpretation: It is clear that "talk" and "powerhouse" of hashCode () the same, however, equals () was false, because in the hash table, hashCode () hash value that is equal to two key-value pairs are equal, but Kazakhstan Xi equal value, not necessarily produce the key-value pairs are equal.

 

4. final in java What is the role?

  • final modified class called final class, which can not be inherited.

  • The final modification method can not be overridden.

  • final modified variable called, const must be initialized, after initialization value can not be modified.

 

5. java in Math.round (-1.5) equal to how much?

Equal to -1, because the value of the number of axes, intermediate value (0.5) rounding to the right, so 0.5 is positive rounding up negative 0.5 is directly discarded.

 

6. String belongs to the basic data type?

String type does not belong to the foundation, there are 8 basic types: byte, boolean, char, short, int, float, long, double, and String objects belong.

 

7. java strings are operating in what classes? What is the difference between them?

String class operation are: String, StringBuffer, StringBuilder.

And the StringBuffer String, String difference lies StringBuilder statements are immutable objects, each operation generates a new String object, then the pointer to the new String object, and the StringBuffer, StringBuilder may be operated on the basis of the original object, so it is best not to use string in the case of frequent changes in the contents of strings.

StringBuffer and StringBuilder biggest difference is that, StringBuffer is thread-safe, but StringBuilder is not thread-safe, but the performance is higher than StringBuffer StringBuilder, so in single-threaded environment is recommended to use StringBuilder, StringBuffer is recommended to use a multithreaded environment.

 

8. String str = "i" and the String str = new String ( "i") the same?

Not the same, because the memory is allocated differently. String str = "i" way, java virtual machine to be assigned to the constant pool; and String str = new String ( "i") will be assigned to the heap.

 

9. How to reverse a string?

Use StringBuilder or stringBuffer the reverse () method.

Sample code:

1 // StringBuffer reverse
2 StringBuffer stringBuffer = new StringBuffer();
3 stringBuffer.append("abcdefg");
4 System.out.println(stringBuffer.reverse()); // gfedcba
5 // StringBuilder reverse
6 StringBuilder stringBuilder = new StringBuilder();
7 stringBuilder.append("abcdefg");
8 System.out.println(stringBuilder.reverse()); // gfedcba

 

10. String class common method have those?

  • indexOf (): Returns the index of the specified character.

  • charAt (): Returns the character at the specified index.

  • replace (): replacement string.

  • trim (): removing a blank string ends.

  • split (): Split string, returns an array of strings after the division.

  • getBytes (): returns a string type byte array.

  • length (): returns the length of the string.

  • toLowerCase (): turn into lowercase string.

  • toUpperCase (): turn a string to uppercase characters.

  • substring (): string taken.

  • equals (): comparison string.

 

11. abstract class must be abstract way?

No, there is not necessarily an abstract class abstract methods.

Sample code:

1 abstract class Cat {
2     public static void sayHi() {
3         System.out.println("hi~");
4     }
5 }

The above code, an abstract method of the abstract class, but not fully functional.

 

12. The ordinary classes and abstract classes What are the differences?

  • Common classes can not contain abstract methods, abstract class may contain abstract methods.

  • An abstract class can not be directly instantiated general class can be instantiated directly.

 

13. abstract class can be modified using the final it?

No, define an abstract class is to make the other class inheritance, if defined as a final class can not be inherited, this will lead to conflict with each other, so the final abstract class can not be modified, as shown below, the editor will prompt an error message:

 

14. interfaces and abstract classes What is the difference?

  • Implementation: a subclass of abstract class to use extends inheritance; implements the interface must be used to implement the interface.

  • Constructor: abstract class constructor can; can not have the interface.

  • main methods: the main method of abstract class can have, and we can run it; the interface can not have a main method.

  • Achieve Quantity: class can implement many interfaces; it can only inherit an abstract class.

  • Access modifiers: public interface method using the default modification; abstract class method may be any access modifiers.

 

15. java in IO stream is divided into several?

Divided by function: input stream (input), the output stream (output).

Divided by type: byte stream and character stream.

The difference between a byte stream and character stream are: 8-bit byte stream transmitted at a data input and output bytes, 16-bit transmission character stream in units of characters input and output data.

 

16. BIO, NIO, AIO What is the difference?

  • BIO: Block IO synchronous blocking IO, is what we usually use the traditional IO, which is characterized by simple and easy to use mode, low concurrent processing capability.

  • NIO: New IO synchronous non-blocking IO, IO is the traditional upgrade, the client and the server through Channel (channel) communication, to achieve multiplexing.

  • AIO: Asynchronous IO is upgraded NIO, also known as NIO2, implements asynchronous non-blocking IO, event-based asynchronous IO operation and callback mechanism.

 

17. Files are common methods of what?

  • Files.exists (): detects file path exists.

  • Files.createFile (): create the file.

  • Files.createDirectory (): Create a folder.

  • Files.delete (): Delete a file or directory.

  • Files.copy (): Copy the file.

  • Files.move (): move the file.

  • The number to view the file: Files.size ().

  • Files.read (): read the file.

  • Files.write (): write to the file.

Guess you like

Origin www.cnblogs.com/donleo123/p/11621007.html