You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 9 |[What is the difference between abstract class and interface](#what-is-the-difference-between-abstract-class-and-interface)|
17
18
| 10 |[What are marker interfaces](#what-are-marker-interfaces)|
18
19
19
-
1.### How does JVM works
20
+
<!-- TOC_END -->
21
+
22
+
<!-- QUESTIONS_START -->
23
+
1.### What are the differences between JVM, JRE and JDK?
24
+
25
+
The main differences between JVM, JRE and JDK are listed below,
26
+
27
+
**JVM:** Java Virtual Machine(JVM) is an abstract machine that provides a runtime environment for the execution of Java ByteCode. i.e, It is part of Java Runtime Environment(JRE) and responsible for converting bytecode into machine-readable code. JVM is platform dependent but it interprets the bytecode which is platform independent. As a result, Java applications to run on different platforms and operating systems.
28
+
29
+
**JRE:** Java Runtime Environment (JRE) is an installation package that provides Java Virtual Machine (JVM), class libraries and other components to run the Java program or application on any machine.
30
+
31
+
**JDK:** Java Development Kit (JDK) is a software development kit used to develop and execute Java programs. It includes tools for developing, monitoring and debugging java programs, along with JRE to execute those respective programs.
32
+
33
+
**[⬆ Back to Top](#table-of-contents)**
34
+
35
+
2.### Why Java is platform-independent language
36
+
37
+
Java is platform-independent language because java programs are compiled to a bytecode that can be run on any device or OS which has a Java Virtual Machine(JVM). This is possible due to JVM's "Write Once, Run Anywhere"(WORA) capability. Due to this reason, you can write a Java program on one platform such as Windows machine and then run it on a different platform such as macOS or Linux machine without making any modifications to the code.
38
+
39
+
Below diagram explains the platform independence feature of Java,
40
+
41
+

42
+
43
+
**[⬆ Back to Top](#table-of-contents)**
44
+
45
+
3.### How does JVM works
20
46
21
47
JVM(Java Virtual Machine) is an abstract runtime engine to run java applications. It is part of Java Runtime Environment(JRE) to execute the bytecode generated by java compilers. JVM brings WORA(Write Once Read Anywhere) behavior in which you can write java program on one system and expected to run on any java enabled systems without any modifications. Upon compiling .java file to .class file(contains byte code), .class file goes into severl steps described by JVM.
4. Whether .class is related to Class, Interface or Enum.
41
67
42
68
43
-
JVM also creates an object of **Class** type(available from **java.lang** package) to represent the file in the heap memory. But it will be created only on the very first time when the class file is loaded into JVM. This object is helpful for the developers to get class level information.
69
+
JVM also creates an object of **Class** type(available from **java.lang** package) to represent the file in the heap memory. But it will be created only on the very first time when the class file is loaded into JVM. This object is helpful for the developers to get class level information.
44
70
45
71
2.**Linking:** Linking is the process of taking a class or interface and combining it into the runtime state of the Java virtual machine, preparing it for execution. It involves the following steps:
46
72
47
-
1.**Verification:** This phase ensure the correctness of `.class` file by checking whether the file is properly formatted or not, and the code generated by a valid compiler or not. If the validation is failed, you will receive `java.lang.VerifyError` through **ByteCodeVerifier**.
73
+
1.**Verification:** This phase ensure the correctness of `.class` file by checking whether the file is properly formatted or not, and the code generated by a valid compiler or not. If the validation is failed, you will receive `java.lang.VerifyError` through **ByteCodeVerifier**.
48
74
49
-
2.**Preparation:** In this step, JVM allocates memory for all static variables within classes/interfaces and assign them with default values.
75
+
2.**Preparation:** In this step, JVM allocates memory for all static variables within classes/interfaces and assign them with default values.
50
76
51
-
3.**Resolution:** In this step, all symbolic references to classes or interfaces are replaced with their actual memory locations. This transformation is known as **Dynamic Linking**.
77
+
3.**Resolution:** In this step, all symbolic references to classes or interfaces are replaced with their actual memory locations. This transformation is known as **Dynamic Linking**.
52
78
53
79
3.**Initialization:** This is the last phase of class loading, where all static variables are replaced with their original values and static block gets executed.
String constant pool is a storage space inside the heap memory area where string literals are stored. It is also known as String pool or String Intern Pool. This is privately maintained by String class and it is empty by default. Whenever you create a new string object, JVM checks for presence of string object in the string pool. If the string value is already present, the same object reference is shared with the variable, else a new string is created in the pool with the variable reference stored in stack area.
169
195
170
196
**[⬆ Back to Top](#table-of-contents)**
171
197
172
-
4.### Why strings are immutable
198
+
6.### Why strings are immutable
173
199
174
200
Strings are immutable, that means the contents of string objects can't be modified after their creation. i.e, When you try to alter the string, it creates a new string. Due to this behavior, the internal state of a string remains the same throughout the execution of the program. This characteristic of immutability helps with the benefits of caching, security, synchronization, and performance.
175
201
176
202
**[⬆ Back to Top](#table-of-contents)**
177
203
178
-
5.### What is the difference between StringBuffer and StringBuilder
204
+
7.### What is the difference between StringBuffer and StringBuilder
179
205
180
206
String is an immutable class to represent sequence of characters. Java also provided mutable version of String class through StringBuffer and StringBuilder. Even though both these classes are mutable, there are many differences between StringBuffer and StringBuilder.
6.### What is the importance of hashCode() and equals() methods
218
+
8.### What is the importance of hashCode() and equals() methods
219
+
220
+
Since both `equals()` and `hashCode()` methods are available in Object class(i.e, Java.lang.object), every java class gets the default implementation of equals and hashCode methods. These methods work together to verify if two objects have the same value or not.
221
+
222
+
**1. equals:** The `equals()` method is used to compare equality of two Objects. By default their implementation compares the identities of the object. The Object class defined `equals()` method as follows,
223
+
224
+
```java
225
+
publicboolean equals(Object obj) {
226
+
return (this== obj);
227
+
}
228
+
```
229
+
230
+
There are some rules need to be followed to use equals method:
231
+
1.**Reflexive:**For any object x, `x.equals(x)` should return `true`.
232
+
2.**Symmetric:**For any two objects x and y, `x.equals(y)` should return `true` if and only if `y.equals(x)` returns `true`.
233
+
3.**Transitive:**For multiple objects x, y, and z, `if x.equals(y)` returns `true` and `y.equals(z)` returns `true`, then `x.equals(z)` should return `true`.
234
+
4.**Consistent:**For any two objects x and y, multiple invocations of `x.equals(y)` should return same result(`true` or `false`), unless any of the object properties is modified that is being used in the equals() method implementation.
235
+
236
+
**2. hashCode:**The `hashCode()` method returns the integer hash code value of the object. This method must be overridden in every classwhich overrides `equals()` method. The general contract of hashCode is:
237
+
238
+
1. While execution of the application, the multiple invocations of `hashCode()` on the object should return the same integer value, unless the object property used in the `equals()` method is being modified.
239
+
2. During the multiple executions of the application, the object's hashCode can change.
240
+
3. If two objects are equal based on `equals()` method, then their object's hash code must be same.
241
+
4. If two objects are unequal based on `equals()` method, their hash code value may or may not be equal.
242
+
243
+
Together, the implementation of `equals()` and `hashCode()` should follow these rules.
244
+
245
+
1. If `x.equals(y)` is true, then `x.hashCode() === y.hashCode()` should always be true.
246
+
2. If `x.hashCode() === y.hashCode()` is true, then it doesn't required to be `x.equals(y)` always true.
193
247
194
248
**[⬆ Back to Top](#table-of-contents)**
195
249
196
-
7.### What is the difference between checked and unchecked expceptions
250
+
9. ### What is the difference between checked and unchecked expceptions
197
251
198
252
An exception is an event that interrupts the normal flow the program execution. There are two types of exceptions,
Wrapper classes provides the mechanism to convert primitive types into object types and vice versa. Since Java is an object-oriented programming language, and many APIs and libraries in Java require objects instead primitive types. For example, data structures in collection framework, utility classes from `java.utils.*`,cloning process, Serialization, Synchronization etc require object type.
9. ### What is the difference between abstract class and interface
375
+
11. ### What is the difference between abstract class and interface
322
376
323
377
Both Abstract class and interface are used to define contracts in object-oriented programming. But there are some key differences between them as shown below,
Marker interfaces are interfaces that don't have any fields, methods, or constants inside of it. They are also known as empty interfaces or tag interfaces. Examples of marker interfaceare Serializable, Cloneable and Remote interface. The purpose of marker interfaces are to provide run-time type information about an object to JVM and Compiler. They are mainly used in API development and in frameworks like Spring to provide additional information to the class.
1.**Internal flags:** They can be used in place of marker interfaceto indicate any specific operation.
389
443
2. **Annotations:** They are used as tags to represent the metadata attached to classes, interfaces, or methods to indicate additional information required by JVM.
390
444
391
-
**[⬆ Back to Top](#table-of-contents)**
445
+
**[⬆ Back to Top](#table-of-contents)**
446
+
447
+
13. ### What are collections in Java?
448
+
449
+
Collections in Java is a unified framework that provides architecture for storing and manipulating a group of objects.
450
+
451
+
**[⬆ Back to Top](#table-of-contents)**
452
+
453
+
14. ### What are the differences between arraylist and vector?
454
+
455
+
Both Vector and ArrayList use dynamically resizable array as their internal data structure and implement the List interface. But there are couple of differences between them as listed below,
| This data structure is part of Collection framework, introduced in JDK 1.2 | This is a legacy class |
460
+
| The capacity increment of ArrayList is 50% if the number of elements exceeds the current size| The capacity increment of Vector is 100% if the number of elements exceeds the current size |
461
+
| It is not synchronized by default. That means multiple threads can access it concurrently. | It is synchronized by default. That means only one thread can access it at a time. |
462
+
| It is quite fast because it is not synchronized | It is quite slow due to synchronization |
463
+
| ArrayList uses the Iterator interfaceto traverse over the elements. | Vector uses both the Iterator and Enumeration interfaces to traverse over the elements. |
0 commit comments