Java based

1. What is the difference between JDK and JRE?

  • JDK: Short for Jave Development Kit, the Java Development Kit contains the Java Development environment and runtime environment
  • JRE: Short for Java Runtime Environment. It provides an Environment for Running Java

The JDK includes the JRE, which not only contains the Java runtime ring, but also contains the compiler Javac that compiles Java source code, as well as Java program debugging and analysis tools. Simply put, if you want to run Java programs, you only need to install the JRE, and if you want to write Java programs, you need to install the JDK

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

== compares values for primitive types, references for objects, and equals compares references by default. We override the Euqals method to make it compare values, like the String class

3. Two objects have the same hashCode() and the same equals value?

Not necessarily the same

String str1 = "Call";
String str2 = "Important";
System.out.println(String.format("str1: %d, str2: %d\n", str1.hashCode(), str2.hashCode()));
System.out.println(str1.equals(str2));

// Execution result
// str1: 1179395, str2: 1179395
// false
Copy the code

4. Usage of final in Java

Final in Java can be used to modify classes, methods, and variables

  1. befinalThe modified class cannot be inherited
  2. befinalThe decorated method cannot be overridden
  3. befinalThe modified variable must be initialized. If it is a primitive type, the initialized value cannot be modified. If it is an object, the initialized reference cannot be modified

5. How much is math.round (-1.5) in Java?

Is equal to -1, which is rounded.

Math. Floor (2.2) = 2.0; Math.ceil(2.2) = 3.0; Math.round(2.2) = 2.0; // Round offCopy the code

6. Is String a basic data type?

String does not belong to the basic data types: byte, short, int, long, float, double, Boolean, char, String belong to objects

7. What are the classes for manipulating strings in Java? What are the differences?

Java classes that operate on strings are String, stringBuffer.stringBuilder

Strings declare immutable objects each time and each operation generates a new object and then points to that new object again, whereas StringBuffer and StringBuilder operate on the original object with better performance

The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe and StringBuilder is non-thread-safe. StringBuilder is recommended for single-threaded applications and StringBuffer for multi-threaded applications

8. Is String STR = “I” the same as String STR = new String(” I “)?

No, the difference is in the way memory is allocated. The first JVM allocates it to the constant pool, while the second allocates it to Java heap memory

9. How to reverse a string?

StringBuffer sb = new StringBuffer("abcdefg");
sb.reverse();
Copy the code

10. Common String methods

IndexOf (): takes subscripts of characters charAt(): takes values of subscripts replace(): replaces string trim(): removes whitespace on both sides of string Split (): cuts string, returns string array getBytes(): Convert to byte array length(): length toLowerCase(): convert to write toUpperCase(): convert toUpperCase substring(): intercept substring euqals(): string comparisonCopy the code

11. Must abstract classes have abstract methods?

not

public abstract class Test {
    public static void test(a) {
        System.out.println("test"); }}Copy the code

12. What is the difference between ordinary classes and abstract classes?

  • You can’t have abstract methods in a normal class, you can have abstract methods in an abstract class
  • Abstract classes cannot be instantiated directly; ordinary classes can be instantiated directly

13. Can abstract classes be modified by Final?

No, abstract classes need to be subclassed before they can be instantiated, and final classes cannot be inherited and therefore cannot be subclassed

14. What is the difference between abstract classes and interfaces?

  • Classes are extents and interfaces are implements
  • Constructors: Abstract classes can use constructors, not interfaces
  • Main: Abstract classes can have and run main methods; interfaces cannot have main methods
  • Access modifiers: Methods in interfaces are public by default. Methods in abstract classes can use other modifiers

Java I/O classification:

By function: input stream and output stream

By type: byte stream and character stream

The difference between a byte stream and a character stream is that a byte stream transmits input and output data in bytes in 8 bits, while a character stream transmits input and output data in bytes in 16 bits

16. BIO, NIO and AIO concepts

  • BIO: Block IO is a traditional synchronous blocking IO. It is characterized by simple usage mode and poor concurrent processing capability
  • NIO: New IO, an upgraded version of BOI, implements synchronous non-blocking IO. The server and client transmit through channels, realizing multiplexing
  • AIO: Asychronous IO, an advanced version of NIO, implements asynchronous non-blocking I/O. Asynchronous non-blocking I/O operations are based on events and callbacks

17 Files method

Files.exists();
Files.create();
Files.createDirectory();
Files.delete();
Files.copy();
Files.move();
Files.size();
Files.write();
Files.read();
Copy the code