Answer JAVA
Answer JAVA
A class in Java is a blueprint for creating objects. It de nes properties ( elds) and behaviors
(methods) of the objects.
Example:
java
Copy
public class Car {
// Fields
String color;
String model;
int year;
// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
A constructor in Java is a special method used to initialize objects. It is called when an object of a
class is created. It can be used to set initial values for the object’s elds.
java
Copy
fi
fi
fi
fi
fi
fi
fi
fi
class Person {
String name;
int age;
// Parameterized constructor
Person(String n, int a) {
name = n;
age = a;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
Java provides the following access speci ers to de ne the visibility of classes, methods, and elds:
1. Public (public): Accessible from anywhere (inside the same package or other packages).
2. Private (private): Accessible only within the same class.
3. Protected (protected): Accessible within the same package and by subclasses.
4. Default (no modi er): Accessible only within the same package.
• Static Method: A method declared with the static keyword. It can be called without
creating an instance of the class. Static methods belong to the class rather than an instance of
the class.
• Instance Method: A method that operates on instances (objects) of the class. It requires
creating an object of the class to be called.
Example:
java
Copy
class MyClass {
static void staticMethod() {
System.out.println("This is a static method.");
fi
fi
fi
fi
fi
}
void instanceMethod() {
System.out.println("This is an instance method.");
}
JavaDoc comments (/** */) are used to generate documentation for Java classes, methods, and
elds. These comments are extracted to produce HTML-formatted documentation using the
javadoc tool.
Example:
java
Copy
/**
* This class represents a Car.
*/
public class Car {
/**
* Model of the car.
*/
String model;
/**
* Starts the car engine.
*/
public void startEngine() {
System.out.println("Engine started.");
}
}
java
Copy
import java.io.Serializable;
A wrapper class in Java is a class that wraps around primitive data types to provide object
representation. Java provides wrapper classes for each primitive data type (e.g., Integer for
int, Double for double).
Example:
java
Copy
public class WrapperExample {
public static void main(String[] args) {
int num = 5;
Integer wrappedNum = Integer.valueOf(num); //
Wrapping int to Integer
System.out.println(wrappedNum);
}
}
fi
9. What is the difference between StringBuilder and StringBuffer?
A switch statement evaluates an expression and matches the result with multiple case values.
When a match is found, the corresponding block of code executes.
Example:
java
Copy
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
• Break: Exits from the loop or switch statement immediately, skipping the remaining
iterations.
• Continue: Skips the current iteration of the loop and continues with the next iteration.
12. Explain the difference between primitive and reference data types.
• Primitive Data Types: These are basic types like int, char, float, etc., that hold
simple values directly.
• Reference Data Types: These store references (memory addresses) to objects. Examples
include arrays, classes, and interfaces.
Example:
java
Copy
import java.util.regex.*;
• Final: Used to de ne constants (variables), prevent method overriding, and prevent class
inheritance.
• Static: Used to de ne class-level members that are shared across instances. Static members
belong to the class, not instances.
fi
fi
fi
fl
fi
fl
fl
fi