0% found this document useful (0 votes)
7 views

How many types of Java libraries are there

Java libraries are categorized into core, standard, third-party, logging, JSON/XML processing, testing, and web framework libraries. The Java Class Library includes packages such as java.lang, java.util, java.io, and java.sql. The document also explains JVM memory areas, data encapsulation vs. data hiding, naming conventions, constructors, differences between primitive and reference types, and provides sample Java programs for reading input and maintaining employee data.

Uploaded by

hardikpatil672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

How many types of Java libraries are there

Java libraries are categorized into core, standard, third-party, logging, JSON/XML processing, testing, and web framework libraries. The Java Class Library includes packages such as java.lang, java.util, java.io, and java.sql. The document also explains JVM memory areas, data encapsulation vs. data hiding, naming conventions, constructors, differences between primitive and reference types, and provides sample Java programs for reading input and maintaining employee data.

Uploaded by

hardikpatil672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

How many types of Java libraries are there? What are the packages of Java class library?

Types of Java Libraries

Java libraries can be categorized into several types based on their functionality:

1. Core Libraries – Included in the JDK (e.g., java.lang, java.util).

2. Standard Libraries – Official Java libraries (e.g., JDBC, JavaFX).

3. Third-Party Libraries – External libraries for various needs (e.g., Apache Commons,
Google Guava).

4. Logging Libraries – Used for logging (e.g., Log4j, SLF4J).

5. JSON/XML Processing Libraries – For data parsing (e.g., Jackson, Gson, JAXB).

6. Testing Libraries – For unit testing (e.g., JUnit, TestNG).

7. Web Framework Libraries – For web development (e.g., Spring, Hibernate).

Packages in Java Class Library

The Java Class Library (JCL) consists of several built-in packages, such as:

 java.lang – Core classes (String, Math, System, etc.).

 java.util – Collections framework, utility classes.

 java.io – Input/output handling.

 java.nio – Non-blocking I/O.

 java.net – Networking operations.

 java.sql – Database connectivity (JDBC).

 java.time – Date and time API.

 javax.swing – GUI components.

 java.security – Security and cryptography.


Explain Java Virtual Machine Memory Area. Java Virtual Machine (JVM) Memory Areas

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?

Data Encapsulation vs. Data Hiding in Java

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?

Camel Case in Java Naming Conventions


Camel Case is a naming convention in Java where multiple words are joined together
without spaces, and each word (except the first, in some cases) starts with an uppercase
letter.
Types of Camel Case in Java:
1. Lower Camel Case (camelCase)
o The first word starts with a lowercase letter, and each subsequent word starts
with an uppercase letter.
o Used for: Variables, method names.
int studentAge = 20; // Variable
void calculateTotalMarks() { } // Method
2.Upper Camel Case (PascalCase)
 Every word starts with an uppercase letter.
 Used for: Class names, interface names.
class EmployeeDetails { } // Class
interface PaymentGateway { } // Interface
Explain the relation between class and Object. What do you understand by an instance
variable and a local variable?

Relation Between Class and Object in Java


1. Class:
A class is a blueprint or template for creating objects. It defines attributes (variables) and
behaviors (methods) that objects will have.
2. Object:
An object is an instance of a class. It represents a real-world entity and has its own state
(data) and behavior (methods).
class Car {
String brand; // Instance variable
int speed;
void display() {
System.out.println("Brand: " + brand + ", Speed: " + speed);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(); // Object creation
car1.brand = "Toyota";
car1.speed = 120;
car1.display();
}

}
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 {

private String name;

private int age;

public String getName() {

return name;

public void setName(String newName) {

this.name = newName;

public int getAge() {

return age;

public void setAge(int newAge) {

if (newAge > 0) {

this.age = newAge;

} else {

System.out.println("Age must be positive!");

} }}

public class Main {

public static void main(String[] args) {

Person p = new Person();

p.setName("Alice");

p.setAge(25);

System.out.println("Name: " + p.getName());

System.out.println("Age: " + p.getAge());

}
What is Constructor? Explain its type.

What is a Constructor in Java?

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).

Key Features of a Constructor:

 Automatically called when an object is created.

 Used to initialize object properties.

 Can be parameterized or default.

 If no constructor is defined, Java provides a default constructor.

Types of Constructors in Java

Java has three types of constructors:

1. Default Constructor (No-Argument Constructor)

 A constructor with no parameters.

 Automatically created by Java if no other constructor is defined.

 Initializes instance variables with default values (0, null, false, etc.).

class Car {

String brand;

Car() {

System.out.println("Default constructor called!");

brand = "Toyota"; // Default value

void display() {

System.out.println("Brand: " + brand);

public class Main {

public static void main(String[] args) {

Car c1 = new Car();

c1.display();

}
Type of constructor

2. Parameterized Constructor

 Accepts arguments to initialize instance variables with user-defined values.

class Student {

String name;

int age;

Student(String studentName, int studentAge) {

name = studentName;

age = studentAge;

void display() {

System.out.println("Name: " + name + ", Age: " + age);

public class Main {

public static void main(String[] args) {

Student s1 = new Student("Alice", 20);

s1.display();

}
Type of constructor

3. Copy Constructor

 A constructor that copies the values of one object into another.

 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() {

System.out.println("Book Title: " + title);

public class Main {

public static void main(String[] args) {

Book b1 = new Book("Java Programming");

Book b2 = new Book(b1); // Copying b1 to b2

b2.display();

}
What is the difference between primitive types and reference types in Java?

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

 Definition: Primitive types store actual values in memory.

 Stored in: Stack memory (fast access).

 Default values: Have predefined default values (e.g., 0, false, \u0000).

 Examples:

1.byte, short, int, long (Integer types)

2.float, double (Floating-point types)

3.char (Character type)

4.boolean (Boolean type)

int a = 10; // Stores actual value 10

double b = 5.5;

boolean flag = true;

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 Program to Read a Number from Standard Input

Java provides different ways to read input from the user. The most common way is using
Scanner.

import java.util.Scanner; // Import Scanner class

public class ReadNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

System.out.println("You entered: " + number);

scanner.close();

How It Works

1. Scanner scanner = new Scanner(System.in); → Creates a Scanner object for reading


input.

2. scanner.nextInt(); → Reads an integer from the user.

3. System.out.println(); → Prints the entered number.

4. scanner.close(); → Closes the scanner to free resources.


Write a java program for Arithmetic operations.

Java Program for Arithmetic Operations

This program performs addition, subtraction, multiplication, division, and modulus


operations based on user input.

import java.util.Scanner; // Import Scanner class

public class ArithmeticOperations {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");

double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");

double num2 = scanner.nextDouble();

double sum = num1 + num2;

double difference = num1 - num2;

double product = num1 * num2;

double quotient = (num2 != 0) ? num1 / num2 : Double.NaN; // Handle division by zero

double remainder = (num2 != 0) ? num1 % num2 : Double.NaN;

System.out.println("Addition: " + sum);

System.out.println("Subtraction: " + difference);

System.out.println("Multiplication: " + product);

System.out.println("Division: " + quotient);

System.out.println("Modulus: " + remainder);

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.

Java Program for Maintaining Employee Data

This program defines an Employee class with attributes like empId, empName,
empDepartment, and empSalary. It allows users to enter employee details and display them.

import java.util.Scanner; // Import Scanner class

class Employee {

int empId;

String empName;

String empDepartment;

double empSalary;

Employee(int id, String name, String department, double salary) {

this.empId = id;

this.empName = name;

this.empDepartment = department;

this.empSalary = salary;

void displayEmployeeDetails() {

System.out.println("\nEmployee Details:");

System.out.println("Employee ID: " + empId);

System.out.println("Name: " + empName);

System.out.println("Department: " + empDepartment);

System.out.println("Salary: $" + empSalary);

// Main class

public class EmployeeManagement {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


Write a java program for maintaining employee data as given empId, empName,
empDepartment, empSalary. 2 incomplete prog

System.out.print("Enter Employee ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Employee Name: ");

String name = scanner.nextLine();

System.out.print("Enter Employee Department: ");

String department = scanner.nextLine();

System.out.print("Enter Employee Salary: ");

double salary = scanner.nextDouble();

Employee emp = new Employee(id, name, department, salary);

emp.displayEmployeeDetails();

scanner.close();

}
5.

You might also like