-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJava_questions.txt
More file actions
80 lines (60 loc) · 3.39 KB
/
Java_questions.txt
File metadata and controls
80 lines (60 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
1. Checked vs Unchecked Exceptions in Java
2. Why interface cannot have constructor while abstract base classes can have?
3. How to create an immutable class in Java?
4. Design a thread safe singleton class
5. Volatile and synchronized meaning?
6. https://javarevisited.blogspot.com/2021/09/microservices-design-patterns-principles.html
7. Optional interfaces in java 8
2. Why interface cannot have constructor while abstract base classes can have?
The main reason why an interface cannot have a constructor while an abstract base class can have one is that they serve different purposes. A constructor is used to initialize the state of an object, whereas an interface is not an object and does not have any state to initialize.
Furthermore, an interface does not provide any implementation of its methods, so it does not need a constructor to initialize any instance variables or perform any other initialization tasks. An abstract base class, on the other hand, may have instance variables and implementation details that need to be initialized in the constructor.
3. How to create an immutable class in Java?
Declare the class as final to prevent inheritance.
Declare all instance variables as private and final to prevent modification.
Provide no setter methods for the instance variables.
If the class contains mutable objects, make sure to create defensive copies of them in the constructor to prevent external modification.
Avoid exposing any internal mutable objects in the public API.
Here's an example implementation of an immutable class called Person:
public final class Person {
private final String name;
private final int age;
private final List<String> hobbies;
public Person(String name, int age, List<String> hobbies) {
this.name = name;
this.age = age;
this.hobbies = new ArrayList<>(hobbies);
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public List<String> getHobbies() {
return new ArrayList<>(hobbies);
}
}
4. Design a thread safe singleton class
public class ThreadSafeSingleton {
private static volatile ThreadSafeSingleton instance;
private ThreadSafeSingleton() {
// private constructor to prevent instantiation from outside the class
}
public static ThreadSafeSingleton getInstance() {
if (instance == null) {
synchronized (ThreadSafeSingleton.class) {
if (instance == null) {
instance = new ThreadSafeSingleton();
}
}
}
return instance;
}
}
5. volatile and synchronized meaning?
In summary, volatile is used to ensure that a variable's' value is always read directly from memory, while synchronized is used to ensure that only one thread can execute a block of code or method at a time, preventing multiple threads from accessing the same shared data at the same time and ensuring thread safety in multi-threaded programs.
6. What is the difference between String, StringBuilder, and StringBuffer?
Answer:
String: Immutable, not thread-safe.
StringBuilder: Mutable, not thread-safe, and faster.
StringBuffer: Mutable, thread-safe due to synchronized methods.