Java plays a crucial role in modern technology, powering everything from household devices like DTH to large-scale enterprise applications. Given its importance, Java knowledge is frequently tested in placement interviews. To help you ace your Java interviews, we have already covered Set 1 and Set 2 of commonly asked questions. In this article, we will discuss Set 3, featuring more in-depth Java interview questions along with detailed explanations.
Java provides 8 primitive data types:boolean, byte, char, int, float, double, long, and short.
For each of these, Java has a corresponding wrapper class that encapsulates the primitive data type inside an object. These are known as Wrapper Classes.
ArrayList
, HashSet
, HashMap
, etc.).Below is the list of primitive data types and their corresponding wrapper classes:
Primitive Type | Wrapper Class |
---|---|
boolean | Boolean |
byte | Byte |
char | Character |
int | Integer |
float | Float |
double | Double |
long | Long |
short | Short |
A Singleton Class is a class that restricts the instantiation of an object to only one instance during the program’s lifetime.
javaCopyEditclass Singleton {
private static Singleton singleInstance; // Static instance
private Singleton() { } // Private constructor
public static Singleton getInstance() {
if (singleInstance == null) {
singleInstance = new Singleton();
}
return singleInstance;
}
}
.equals()
and ==
in JavaBoth .equals()
and ==
are used for comparison in Java, but they serve different purposes:
Feature | == (Equality Operator) | .equals() Method |
---|---|---|
Comparison Type | Compares memory address (reference comparison) | Compares content (value comparison) |
Works on | Primitives & Objects | Objects only |
Example Usage | if (obj1 == obj2) | if (obj1.equals(obj2)) |
javaCopyEditpublic class EqualsTest {
public static void main(String[] args) {
String str1 = new String("Java");
String str2 = new String("Java");
System.out.println(str1 == str2); // Output: false (different memory locations)
System.out.println(str1.equals(str2)); // Output: true (same content)
}
}
Feature | Stack Memory | Heap Memory |
---|---|---|
Usage | Stores method calls & local variables | Stores objects & instance variables |
Access | Limited to a single thread | Globally accessible |
Memory Management | Automatically managed (LIFO) | Garbage collected |
Speed | Faster | Slower |
Lifetime | Exists during method execution | Exists throughout the application |
JSP pages go through three stages during their lifecycle:
jspInit()
– Initializes resources (database connections, files, lookup tables)._jspService()
– Handles client requests and generates responses.jspDestroy()
– Cleans up resources when the page is no longer needed.javaCopyEditpublic void jspInit() {
// Initialization code
}
public void _jspService(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// Request processing code
}
public void jspDestroy() {
// Cleanup code
}
Variable Type | Scope & Usage |
---|---|
Member Variable | Defined inside a class but outside methods. Accessible throughout the class. |
Local Variable | Defined inside a method. Only accessible within that method. |
Loop Variable | Defined inside loops. Accessible only within the loop block {} . |
javaCopyEditclass VariableScopeExample {
int memberVariable = 10; // Class-level scope
public void methodExample() {
int localVariable = 5; // Method-level scope
for (int i = 0; i < 3; i++) {
int loopVariable = i * 2; // Loop-level scope
System.out.println(loopVariable);
}
}
}
this
Keyword in Java?The this
keyword is a reference variable that refers to the current object.
this
in Java:this()
).javaCopyEditclass Example {
int num;
Example(int num) {
this.num = num; // Using 'this' to refer to instance variable
}
void display() {
System.out.println("Number: " + this.num);
}
public static void main(String[] args) {
Example obj = new Example(10);
obj.display(); // Output: Number: 10
}
}
Mastering these Java interview concepts will help you perform well in placement interviews. Make sure to practice coding these concepts, as interviewers often ask candidates to implement solutions on the spot.