How many types of Java libraries are there
How many types of Java libraries are there
Java libraries can be categorized into several types based on their functionality:
3. Third-Party Libraries – External libraries for various needs (e.g., Apache Commons,
Google Guava).
5. JSON/XML Processing Libraries – For data parsing (e.g., Jackson, Gson, JAXB).
The Java Class Library (JCL) consists of several built-in packages, such as:
The Java Virtual Machine (JVM) divides memory into different areas to efficiently manage
execution and resource allocation. These memory areas are:
1. Method Area (Class Area)
o Stores class metadata, static variables, and runtime constant pool.
o Shared among all threads.
o Part of the heap memory in modern JVM implementations (e.g., Metaspace
in Java 8+).
2. Heap Memory
o Stores objects and class instances.
o Managed by the Garbage Collector (GC).
o Divided into:
Young Generation (Eden, Survivor Spaces) – Stores newly created
objects.
Old Generation (Tenured Space) – Stores long-lived objects.
3. Stack Memory
o Stores method execution frames, local variables, and partial results.
o Each thread has its own stack (Thread Stack).
o Grows and shrinks with method calls (push/pop operations).
4. PC (Program Counter) Register
o Holds the address of the currently executing instruction.
o Each thread has its own PC register.
5. Native Method Stack
o Stores native method calls (methods written in languages like C/C++ using
JNI).
o Each thread has its own native stack.
JVM Memory Structure Overview:
Heap (Shared) → Stores objects.
Method Area (Shared) → Stores class metadata.
Stack (Per Thread) → Stores method calls and local variables.
PC Register (Per Thread) → Tracks execution instruction.
Native Stack (Per Thread) → Handles native method execution.
What do you mean by data encapsulation and data hiding with suitable example?
1. Data Encapsulation
Definition: Encapsulation is the process of wrapping data (variables) and code
(methods) together into a single unit (class).
Purpose: It helps in data abstraction and protects data from direct access by outside
code.
Implementation: Achieved using private variables and public getter/setter methods.
Class
BankAccount
{
private double balance; // Private variable (data hiding)
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
account.deposit(500);
System.out.println("Balance: " + account.getBalance()); // Output: 1500
}
}
What do you mean by data hiding with suitable example?
2. Data Hiding
Definition: Data hiding restricts direct access to an object’s data and ensures
controlled access via methods.
Purpose: It protects data integrity and prevents unintended modifications.
Implementation: Achieved by making data members private and only exposing
necessary methods.
class Employee {
private String name; // Data hiding
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
// No setter for salary (cannot be changed externally)
public double getSalary() {
return salary;
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("John", 5000);
System.out.println("Name: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
// emp.salary = 6000; // ERROR: Cannot access private variable directly
}
}
What is Camel Case in Java naming conventions?
}
How getter and setter method help to access private data members of a class?
How Getter and Setter Methods Help Access Private Data Members in Java
Why Use Getters and Setters?
In Java, private data members cannot be accessed directly from outside the class.
Getter and setter methods provide controlled access to these private variables,
ensuring data encapsulation and security.
Getters retrieve the value, and setters modify the value with optional validation.
class Person {
return name;
this.name = newName;
return age;
if (newAge > 0) {
this.age = newAge;
} else {
} }}
p.setName("Alice");
p.setAge(25);
}
What is Constructor? Explain its type.
A constructor is a special method in Java used to initialize objects when they are created. It has the same name
as the class and no return type (not even void).
Initializes instance variables with default values (0, null, false, etc.).
class Car {
String brand;
Car() {
void display() {
c1.display();
}
Type of constructor
2. Parameterized Constructor
class Student {
String name;
int age;
name = studentName;
age = studentAge;
void display() {
s1.display();
}
Type of constructor
3. Copy Constructor
Java does not provide a built-in copy constructor, but we can define one manually.
class Book {
String title;
Book(String bookTitle) {
title = bookTitle;
Book(Book b) {
title = b.title;
void display() {
b2.display();
}
What is the difference between primitive types and reference types in Java?
In Java, data types are categorized into primitive types and reference types based on how
they store and manage data.
1. Primitive Types
Examples:
double b = 5.5;
2. Reference Types
Definition: Reference types store memory addresses (references) of objects instead
of actual data.
Stored in: Reference stored in stack, actual object stored in heap memory.
Default value: null if not initialized.
Examples:
o String, Arrays, Classes, Interfaces, Objects.
String str = "Hello"; // Stores reference to "Hello" in heap memory
int[] numbers = {1, 2, 3}; // Reference to array in heap
Write Java Program to Read The Number From Standard Input.
Java provides different ways to read input from the user. The most common way is using
Scanner.
scanner.close();
How It Works
scanner.close();
}
How It Works
1. Scanner is used to take user input, Handles division by zero to prevent errors
2. Reads two numbers from the user., Displays the results to the user.
3.
Write a java program for maintaining employee data as given empId, empName,
empDepartment, empSalary.
This program defines an Employee class with attributes like empId, empName,
empDepartment, and empSalary. It allows users to enter employee details and display them.
class Employee {
int empId;
String empName;
String empDepartment;
double empSalary;
this.empId = id;
this.empName = name;
this.empDepartment = department;
this.empSalary = salary;
void displayEmployeeDetails() {
System.out.println("\nEmployee Details:");
// Main class
int id = scanner.nextInt();
emp.displayEmployeeDetails();
scanner.close();
}
5.