Fundamentals 14 min read

Java Basics Interview Questions and Answers – Part 1

This article presents a curated set of 208 Java interview questions, focusing on the Java Basics module, and provides detailed explanations, code examples, and analyses of concepts such as JDK vs JRE, == vs equals, hashCode, final, abstract classes, and common String operations.

Java Captain
Java Captain
Java Captain
Java Basics Interview Questions and Answers – Part 1

The article introduces a collection of 208 Java interview questions, highlighting the first part that covers fundamental Java concepts and providing accurate answers with code analysis and principle explanations.

1. What is the difference between JDK and JRE?

JDK (Java Development Kit) includes the JRE (Java Runtime Environment) plus development tools such as the compiler javac and debugging utilities. JRE only provides the runtime environment needed to execute Java programs.

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

== interpretation

Primitive types: compares the actual values.

Reference types: compares whether the two references point to the same object.

Code example:

String x = "string";
String y = "string";
String z = new String("string");
System.out.println(x==y); // true
System.out.println(x==z); // false
System.out.println(x.equals(y)); // true
System.out.println(x.equals(z)); // true

The result shows that == is true for x and y because they reference the same interned string, while z is a distinct object; equals compares values, so it returns true for all cases.

equals interpretation

By default, equals behaves like == , but many classes (e.g., String , Integer ) override it to perform value comparison.

Example with a custom class:

class Cat {
    public Cat(String name) {
        this.name = name;
    }
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
Cat c1 = new Cat("王磊");
Cat c2 = new Cat("王磊");
System.out.println(c1.equals(c2)); // false

Source of Object.equals :

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

String overrides equals to compare character arrays, which is why two distinct String objects with the same content are considered equal:

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

3. If two objects have the same hashCode() , must equals() be true?

No. Identical hash codes do not guarantee equality.

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

Both strings produce the same hash code (1179395) but equals returns false.

4. What is the effect of final in Java?

Final class: cannot be subclassed.

Final method: cannot be overridden.

Final variable: becomes a constant; must be initialized once and cannot be changed thereafter.

5. What does Math.round(-1.5) return?

It returns -1 because rounding moves the half‑point toward positive infinity for positive numbers and toward zero for negative numbers.

6. Is String a primitive data type?

No. Java has eight primitive types (byte, boolean, char, short, int, float, long, double); String is an object.

7. Which classes are used for string manipulation and how do they differ?

Classes: String , StringBuffer , StringBuilder . String is immutable; each modification creates a new object. StringBuffer and StringBuilder are mutable, but StringBuffer is thread‑safe while StringBuilder is not, making the latter faster in single‑threaded contexts.

8. Are String str = "i" and String str = new String("i") equivalent?

No. The literal is stored in the string constant pool, whereas new String("i") creates a distinct object on the heap.

9. How to reverse a string?

Use StringBuilder or StringBuffer with the reverse() method.

// StringBuffer reverse
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcdefg");
System.out.println(stringBuffer.reverse()); // gfedcba
// StringBuilder reverse
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("abcdefg");
System.out.println(stringBuilder.reverse()); // gfedcba

10. Common methods of the String class

indexOf() – returns the index of a character.

charAt() – returns the character at a given index.

replace() – replaces characters or substrings.

trim() – removes leading and trailing whitespace.

split() – splits the string into an array.

getBytes() – returns the byte representation.

length() – returns the length.

toLowerCase() / toUpperCase() – case conversion.

substring() – extracts a portion.

equals() – compares for equality.

11. Must an abstract class contain abstract methods?

No. An abstract class can have no abstract methods and still be declared abstract.

abstract class Cat {
    public static void sayHi() {
        System.out.println("hi~");
    }
}

12. Differences between ordinary classes and abstract classes

Ordinary classes cannot contain abstract methods; abstract classes can.

Abstract classes cannot be instantiated directly; ordinary classes can.

13. Can an abstract class be declared final ?

No. final prevents inheritance, which contradicts the purpose of an abstract class.

14. Differences between interfaces and abstract classes

Implementation: abstract classes are extended with extends ; interfaces are implemented with implements .

Constructors: abstract classes can have them; interfaces cannot.

Main method: abstract classes may contain a main method; interfaces cannot.

Multiple inheritance: a class can implement many interfaces but can extend only one abstract class.

Access modifiers: interface methods are implicitly public ; abstract class methods can have any visibility.

15. How many types of Java I/O streams are there?

By function: input streams and output streams. By type: byte streams and character streams. Byte streams handle data in 8‑bit units; character streams handle 16‑bit Unicode characters.

16. Differences among BIO, NIO, and AIO

BIO (Blocking I/O): synchronous, blocking, simple but low concurrency.

NIO (New I/O): synchronous, non‑blocking, uses channels and selectors for multiplexing.

AIO (Asynchronous I/O, also called NIO2): fully asynchronous, based on events and callbacks.

17. Common methods of java.nio.file.Files

exists() – checks if a path exists.

createFile() – creates a file.

createDirectory() – creates a directory.

delete() – deletes a file or directory.

copy() – copies a file.

move() – moves a file.

size() – returns the size of a file.

read() – reads file content.

write() – writes to a file.

— End of the Java Basics interview Q&A section.

JavaJDKinterviewString()basicsequalsHashCodeJRE
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.