Just talk about 3 Java interview questions

Even experienced developers may find these to be tricky questions to ask during an interview:

1. What is the purpose of the "transient" keyword in Java? How can this be achieved?

In Java, the "transient" keyword is used to indicate that a specific field of a class should not be included in the serialized form of the object.

This means that when an object is serialized, its state is converted into a sequence of bytes that can be written to a file or sent over the network.

By marking a field as transient, you tell Java that the field's value should not be included when serializing the object.

There are several reasons why you might want to use the "transient" keyword. For example, you might have a field that contains a temporary value that you don't need to persist when serializing the object. Or, there might be a field that contains sensitive data that should not be included in the serialized form of the object for security reasons.

public class MyClass implements Serializable {
    
      
    private int myInt;  
    private transient String myTransientString;  
    // 构造器、getters & setters
    // 其他方法……  
}

In this example, the field "myTransientString" is marked as transient, which means that when the instance of MyClass is serialized, its value will not be included.

2. Can you explain the difference between "inheritance" and "composition"? for example.

Inheritance and composition are two basic ways of creating relationships between classes in object-oriented programming. Both approaches support code reuse and abstraction, but differ in their implementation and the types of relationships created between classes.

Here is a brief overview of each method:

  • Inheritance: This is a mechanism for creating new classes by deriving properties and characteristics from existing classes. A new class (called a subclass or derived class) inherits the methods and fields of an existing class (called a superclass or base class). Subclasses can also override superclass methods to provide their own implementations. Inheritance creates an "is-a" relationship between a superclass and a subclass.
  • Composition: It is a mechanism in which a class contains one or more instances of another class as its fields. The contained classes are called component or part classes. The class that contains the component class is called the container or the entire class. This combination creates a "has-a" relationship between the container class and the component class.

image.png

In the diagram, we have two classes: "Vehicle" and "Engine". The Vehicle class can be combined with the Engine class using inheritance or composition.

  • Inheritance Example: Vehicle class extends Engine class, which means it inherits all the fields and methods of Engine class. This creates an "is-a" relationship between the Vehicle and Engine classes, where Vehicle "is a" type of Engine.
public class Vehicle extends Engine {
    
      
  
}
  • Composition example: The Vehicle class contains an instance of the Engine class as its field. This creates a "has a" relationship between the vehicle and engine classes, where the vehicle "has an" engine.
public class Vehicle {
    
      
    private Engine engine;  
    public Vehicle(Engine engine) {
    
      
    this.engine = engine;  
}  
...
}

In general, inheritance is more appropriate when there is a clear "is-a" relationship between classes and the subclass can be considered a specialized version of the superclass. Composition is more appropriate when there is a "has-a" relationship between classes, and when a container class needs to use or manage one or more instances of another class.

3. Can you explain the difference between HashSet and TreeSet in Java? Also, explain how the data is stored internally.

image.png

Suppose we have the following integer data: {7, 3, 9, 4, 1, 8}.

  • For HashSet, the data is stored internally in a hash table. Hash tables use each element's hashCode() method to determine the unique index at which that element should be stored.

Insert image description here

In the example diagram above, the hash table has eight buckets, labeled "51" through "56". Each bucket is a collection containing elements with a hash code mapped to that bucket. For example, the set at index "53" contains elements 3 and 4, both of which have hash codes [197]. The set at index "56" contains elements 7, 8, and 9, which all have hash codes [195].

  • For TreeSet, data is stored internally in a red-black tree. The tree is sorted according to the natural ordering of the elements or the order defined by a custom comparator passed to the TreeSet constructor. Here is an example of how to store data in a red-black tree:

Insert image description here

In this example, the red-black tree has six nodes, each containing one of the elements {1,3,4,7,8,9}. The color of the node is red or black. The red node indicates that a violation of the red-black tree properties has occurred. Elements are stored in sorted order in the tree, with smaller elements on the left and larger elements on the right. For example, element 1 is the smallest element and is stored in the leftmost leaf node, while element 9 is the largest element and is stored in the rightmost leaf node.

Guess you like

Origin blog.csdn.net/qq_41340258/article/details/133076861
Recommended