Practical No.
1
Title: Study of JDK, JRE and JVM
Aim:
To study the concepts of JDK, JRE, and JVM.
Theory:
Java is a platform-independent programming language developed by Sun Microsystems and
now owned by Oracle Corporation. Java programs can run on any system that has Java
installed.
Java is object-oriented, robust, secure, and supports automatic memory management. Its
platform-independent nature is possible because of the combination of JVM, JRE, and JDK.
Every Java program is first compiled into bytecode, which can be executed on any machine
with a JVM.
Java works with three main components:
1. JDK (Java Development Kit)
JDK is used to develop Java programs.
It contains:
JRE
Development tools like:
o javac (compiler)
o java (runtime launcher)
o javadoc
o jdb
JDK is required by programmers to write, compile and run Java programs.
Formula:
JDK = JRE + Development Tools
2. JRE (Java Runtime Environment)
JRE is used to run Java programs.
It contains:
JVM
Java class libraries
Supporting files
JRE does not contain development tools like compiler.
Formula:
JRE = JVM + Libraries
3. JVM (Java Virtual Machine)
JVM is responsible for executing Java bytecode.
Functions of JVM:
Converts bytecode into machine code
Manages memory
Performs garbage collection
Provides security
JVM makes Java platform independent.
Relationship Between JDK, JRE and JVM:
JDK = JRE + Tools
JRE = JVM + Libraries
So, JDK is the complete package.
Steps to Download and Install JDK:
Steps to Download and Install JDK:
1. Go to the Oracle JDK download page.
2. Select the latest JDK for your operating system.
3. Click Download and accept the license.
4. Run the installer and complete installation.
5. Set the environment variable:
o Add JDK bin folder to Path.
o Set JAVA_HOME to the JDK folder.
6. Check installation by typing in Command Prompt / Terminal:
7. java -version
8. javac -version
Algorithm:
1. Install JDK on the system.
2. Set environment variables (Path and JAVA_HOME).
3. Write a Java program and save it as [Link].
4. Compile the program using:
5. javac [Link]
6. Run the program using:
7. java HelloWorld
Program Code:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello Java");
}
}
Output:
Hello Java
Result: Thus, the study of JDK, JRE, and JVM was completed successfully, and JDK
was downloaded, installed, and configured properly.
Practical No. 2
Title:
Study of Variables and Data Types in Java
Aim:
To study the concept of variables and different data types used in Java programming
language and to implement them using a simple Java program.
Theory:
Java is a high-level, object-oriented, and platform-independent programming language.
To store and process data in any Java program, variables and data types play a very
important role. Without variables, it is impossible to store data, and without data types, the
compiler cannot understand what type of data is being stored.
Variable in Java
A variable is a named memory location used to store a value that can change during program
execution. Each variable in Java must be declared with a data type before it can be used.
Syntax of variable declaration:
data_type variable_name = value;
Example:
int a = 10;
Here:
int → data type
a → variable name
10 → value
Rules for Naming Variables
1. Variable name must start with a letter, $, or _.
2. It cannot start with a number.
3. It should not be a Java keyword.
4. No spaces are allowed.
5. Variable names are case-sensitive.
Algorithm:
1. Start the program.
2. Declare the main class.
3. Declare variables of different data types such as int, float, char, and boolean.
4. Assign appropriate values to the variables.
5. Use [Link]() to display variable values.
6. Compile the program using Java compiler.
7. Execute the program.
8. Observe the output.
9. Stop the program.
Program Code:
class DataTypes {
public static void main(String[] args) {
int a = 10;
float b = 5.5f;
double c = 25.75;
char d = 'A';
boolean e = true;
[Link]("Integer value: " + a);
[Link]("Float value: " + b);
[Link]("Double value: " + c);
[Link]("Character value: " + d);
[Link]("Boolean value: " + e);
}
}
Output:
Integer value: 10
Float value: 5.5
Double value: 25.75
Character value: A
Boolean value: true
Result:
Thus, the study of variables and different data types in Java was completed successfully and
their implementation was verified using a Java program.
Practical No. 3
Title:
Study of Control Statements in Java
Aim:
To study and implement different control statements in Java programming and understand
how they control the flow of execution of a program.
Theory:
In Java programming, statements are executed sequentially, that is, one after another.
However, in real-world applications, we often need to make decisions, repeat tasks, or
change the normal flow of execution. This is achieved using control statements.
Control statements allow a programmer to control the execution flow of a program based on
certain conditions or repetitions. They make programs logical, flexible, and efficient.
Java control statements are broadly classified into three types:
1. Decision-Making (Conditional) Statements
Decision-making statements are used when a program needs to execute different blocks of
code based on a condition.
a) if Statement
The if statement executes a block of code only when the given condition is true.
Syntax:
if(condition) {
// statements
}
b) if-else Statement
The if-else statement executes one block of code if the condition is true and another block
if the condition is false.
Syntax:
if(condition) {
// true block
} else {
// false block
}
c) else-if Ladder
Used to check multiple conditions.
2. Looping Statements
Looping statements are used to repeat a block of code multiple times until a condition
becomes false.
a) for Loop
Used when the number of iterations is known.
b) while Loop
Used when the condition is checked before execution.
c) do-while Loop
Executes the loop at least once even if the condition is false.
Algorithm:
1. Start the program.
2. Declare the main class.
3. Declare and initialize an integer variable.
4. Check whether the number is even or odd using an if-else statement.
5. If the condition is true, display “Even Number”.
6. If the condition is false, display “Odd Number”.
7. Compile the program using the Java compiler.
8. Execute the program.
9. Observe the output.
10. Stop the program.
Program Code:
class ControlStatements {
public static void main(String[] args) {
int number = 15;
if (number % 2 == 0) {
[Link]("The number is Even");
} else {
[Link]("The number is Odd");
}
}
}
Output:
The number is Odd
Result:
Thus, the study of control statements in Java was completed successfully. The program
demonstrated the use of the if-else decision-making control statement to control the flow of
execution based on a condition.
Practical No. 4
Title:
Study of Strings in Java
Aim:
To study the concept of Strings in Java and perform basic string operations using built-in
String class methods.
Theory:
In Java, a String is a sequence of characters used to store and manipulate text. Java provides
a predefined String class in the [Link] package to handle strings efficiently. Strings are
widely used in almost every Java application such as login systems, form validations, file
handling, and message display.
String Class in Java
A String in Java is an object, not a primitive data type. Once a String object is created, its
value cannot be changed. This property is known as immutability.
Example:
String s = "Java";
Even if we try to modify the string, a new object is created instead of changing the original
one.
String Immutability
Immutability means once a string is created, its value cannot be changed.
Example:
String s = "Java";
s = [Link](" Programming");
Here, a new string object is created instead of modifying the existing one.
Advantages of immutability:
1. Improves security
2. Thread-safe
3. Efficient memory usage
4. Suitable for key-value storage
Common String Methods
Java provides many built-in methods for string manipulation:
Method Description
length() Returns length of string
concat() Joins two strings
toUpperCase() Converts to uppercase
toLowerCase() Converts to lowercase
charAt() Returns character at index
equals() Compares two strings
Algorithm:
1. Start the program.
2. Declare the main class.
3. Create string variables using string literals.
4. Perform string operations such as concatenation and length calculation.
5. Display the results using [Link]().
6. Compile the program using Java compiler.
7. Execute the program.
8. Observe the output.
9. Stop the program.
Program Code:
class StringDemo {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Programming";
String s3 = [Link](" ").concat(s2);
[Link]("Combined String: " + s3);
[Link]("Length of String: " + [Link]());
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
}
}
Output:
Combined String: Java Programming
Length of String: 16
Uppercase: JAVA PROGRAMMING
Lowercase: java programming
Result:
Thus, the study of Strings in Java was completed successfully. Various string operations such
as concatenation, length calculation, and case conversion were implemented and verified.
Practical No. 5
Title:
Study of Classes and Objects in Java
Aim:
To study the concepts of classes and objects in Java and implement them using a simple Java
program.
Theory:
Java is an object-oriented programming (OOP) language. The main aim of OOP is to
represent real-world entities in the form of objects. The fundamental building blocks of OOP
in Java are classes and objects. Understanding these concepts is essential to write efficient,
reusable, and modular programs.
Class in Java
A class is a blueprint or template used to create objects. It defines the data members
(variables) and member functions (methods) that describe the behavior of an object.
Syntax of class:
class ClassName {
// data members
// methods
}
Example:
class Student {
int roll;
String name;
}
Object in Java
An object is an instance of a class. It represents a real-world entity and occupies memory at
runtime. Objects are created using the new keyword.
Syntax of object creation:
ClassName objectName = new ClassName();
Example:
Student s = new Student();
Accessing Class Members
Class members are accessed using the dot (.) operator.
Example:
[Link] = 1;
[Link] = "Raj";
Advantages of Classes and Objects
1. Code reusability
2. Data security
3. Easy maintenance
4. Modularity
5. Real-world representation
Real-World Example
A class Student may contain data such as roll number and name, and an object represents a
specific student. This approach helps in managing large programs easily.
Thus, classes and objects form the foundation of object-oriented programming in Java.
Algorithm:
1. Start the program.
2. Define a class with data members.
3. Declare the main method.
4. Create an object of the class using the new keyword.
5. Assign values to the object variables.
6. Access and display the values using dot operator.
7. Compile the program.
8. Execute the program.
9. Observe the output.
10. Stop the program.
Program Code:
class Student {
int roll;
String name;
public static void main(String[] args) {
Student s = new Student();
[Link] = 101;
[Link] = "Raj";
[Link]("Roll Number: " + [Link]);
[Link]("Name: " + [Link]);
}
}
Output:
Roll Number: 101
Name: Raj
Result:
Thus, the study of classes and objects in Java was completed successfully. A simple program
was implemented to demonstrate object creation and access of class members.
Practical No. 6
Title:
Study of Inheritance in Java
Aim:
To study the concept of inheritance in Java and implement it using a simple Java program.
Theory:
Inheritance is one of the most important features of Object-Oriented Programming (OOP).
In Java, inheritance allows one class to acquire the properties and behaviors of another
class. The main purpose of inheritance is code reusability, which helps reduce duplication of
code and makes programs easier to maintain.
Basic Concept of Inheritance
In inheritance:
The existing class is called Parent class or Superclass
The new class that inherits properties is called Child class or Subclass
Inheritance is implemented using the extends keyword.
Syntax:
class ChildClass extends ParentClass {
// additional members
}
Types of Inheritance in Java
Java supports the following types of inheritance:
1. Single Inheritance
One child class inherits from one parent class.
2. Multilevel Inheritance
A class inherits from a child class, forming a chain.
3. Hierarchical Inheritance
Multiple child classes inherit from a single parent class.
Java does not support multiple inheritance using classes to avoid ambiguity, but it can be
achieved using interfaces.
Advantages of Inheritance
1. Promotes code reusability
2. Reduces redundancy
3. Improves program readability
4. Easier maintenance
5. Supports method overriding
Real-World Example
Consider a class Vehicle with properties like speed and fuel type. A class Car can inherit
these properties and add new features like air conditioning. This reduces the need to rewrite
common code.
Thus, inheritance helps in building efficient and structured Java applications.
Algorithm:
1. Start the program.
2. Create a parent class with a method.
3. Create a child class using the extends keyword.
4. Define the main method in the child class.
5. Create an object of the child class.
6. Access the parent class method using child object.
7. Compile the program using Java compiler.
8. Execute the program.
9. Observe the output.
10. Stop the program.
Program Code:
class Parent {
void display() {
[Link]("This is Parent Class");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
}
}
Output:
This is Parent Class
Result:
Thus, the study of inheritance in Java was completed successfully. The program
demonstrated how a child class inherits the properties of a parent class using the extends
keyword.
Practical No. 7
Title:
Study of Polymorphism in Java
Aim:
To study the concept of polymorphism in Java and implement it using method overloading.
Theory:
Polymorphism is one of the core concepts of Object-Oriented Programming (OOP). The
word polymorphism is derived from two Greek words:
Poly means many
Morph means forms
Thus, polymorphism means one name, many forms. In Java, polymorphism allows the same
method or object to behave differently based on the situation. It improves flexibility,
readability, and maintainability of programs.
Types of Polymorphism in Java
Java supports two types of polymorphism:
1. Compile-Time Polymorphism
Also known as static polymorphism, it is achieved using method overloading.
Method Overloading:
When multiple methods have the same name but different parameters (number, type, or
order), it is called method overloading.
Example:
add(int a, int b)
add(double a, double b)
The compiler decides which method to call at compile time.
2. Run-Time Polymorphism
Also known as dynamic polymorphism, it is achieved using method overriding and
inheritance.
(Method overriding is usually covered along with inheritance.)
Advantages of Polymorphism
1. Improves code reusability
2. Simplifies code structure
3. Enhances flexibility
4. Reduces complexity
5. Supports dynamic method execution
Real-World Example
A person can have different roles—student, employee, or customer—depending on the
situation. Similarly, a method behaves differently depending on parameters.
Thus, polymorphism is an essential feature for developing scalable Java applications.
Algorithm:
1. Start the program.
2. Create a class.
3. Define multiple methods with the same name but different parameters.
4. Declare the main method.
5. Create an object of the class.
6. Call the overloaded methods.
7. Compile the program.
8. Execute the program.
9. Observe the output.
10. Stop the program.
Program Code:
class PolymorphismDemo {
void add(int a, int b) {
[Link]("Sum of integers: " + (a + b));
}
void add(double a, double b) {
[Link]("Sum of doubles: " + (a + b));
}
public static void main(String[] args) {
PolymorphismDemo obj = new PolymorphismDemo();
[Link](10, 20);
[Link](10.5, 20.5);
}
}
Output:
Sum of integers: 30
Sum of doubles: 31.0
Result:
Thus, the study of polymorphism in Java was completed successfully. Method overloading
was implemented to demonstrate compile-time polymorphism.
Practical No. 8
Title:
Study of Abstract Class and Interface in Java
Aim:
To study abstract classes and interfaces in Java and understand how abstraction is achieved.
Theory:
Abstraction is the process of hiding implementation details and showing only essential
features of an object. Java achieves abstraction using abstract classes and interfaces.
Abstraction improves program security and reduces complexity.
Abstract Class
An abstract class is a class that cannot be instantiated. It may contain:
Abstract methods (methods without body)
Non-abstract methods
Variables and constructors
Syntax:
abstract class ClassName {
abstract void methodName();
}
A class that extends an abstract class must implement all abstract methods.
Interface
An interface is a blueprint of a class. It contains only:
Abstract methods
Constants (public, static, final)
Interfaces are used to achieve multiple inheritance, which is not supported by classes in
Java.
Syntax:
interface InterfaceName {
void methodName();
}
Advantages of Abstraction
1. Improves security
2. Reduces complexity
3. Enhances flexibility
4. Promotes loose coupling
5. Improves maintainability
Thus, abstract classes and interfaces are essential for designing robust Java applications.
Algorithm:
1. Start the program.
2. Create an abstract class with an abstract method.
3. Create a subclass that extends the abstract class.
4. Implement the abstract method.
5. Create an object using reference of abstract class.
6. Call the implemented method.
7. Compile the program.
8. Execute the program.
9. Observe the output.
10. Stop the program.
Program Code:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
public static void main(String[] args) {
Shape s = new Circle();
[Link]();
}
}
Output:
Drawing Circle
Result:
Thus, the study of abstract class and interface in Java was completed successfully. The
program demonstrated abstraction using an abstract class.
Practical No. 9
Title:
Study of Exception Handling in Java
Aim:
To study exception handling in Java and handle runtime errors using try–catch blocks.
Theory:
In Java, an exception is an abnormal condition that occurs during the execution of a program
and disrupts the normal flow of execution. Exceptions mainly occur due to errors such as
division by zero, accessing invalid array indexes, or incorrect input. If exceptions are not
handled properly, the program may terminate abnormally.
Java provides a powerful mechanism known as exception handling to handle runtime errors
and maintain normal program flow. Exception handling improves the reliability, robustness,
and fault tolerance of a program.
Types of Errors in Java
1. Compile-time errors – Errors detected during compilation.
2. Runtime errors – Errors that occur during program execution.
3. Logical errors – Errors due to incorrect program logic.
Exception handling mainly deals with runtime errors.
Keywords Used in Exception Handling
Java uses the following keywords:
try – Contains code that may generate an exception
catch – Handles the exception
finally – Executes code regardless of exception occurrence
throw – Explicitly throws an exception
throws – Declares exceptions
Advantages of Exception Handling
1. Prevents abnormal termination of program
2. Helps in debugging
3. Maintains normal program flow
4. Improves program reliability
5. Provides meaningful error messages
Real-World Example
ATM transactions use exception handling to prevent system crashes when invalid inputs
occur, such as insufficient balance or incorrect PIN.
Thus, exception handling is a critical part of Java programming.
Algorithm:
1. Start the program.
2. Declare the main class.
3. Write risky code inside the try block.
4. Handle the exception using catch block.
5. Display an appropriate message.
6. Compile the program.
7. Execute the program.
8. Observe the output.
9. Stop the program.
Program Code:
class ExceptionHandlingDemo {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Arithmetic Exception Handled");
}
[Link]("Program continues...");
}
}
Output:
Arithmetic Exception Handled
Program continues...
Result:
Thus, the study of exception handling in Java was completed successfully. Runtime errors
were handled effectively using try–catch blocks.
Practical No. 10
Title:
Study of File Handling in Java
Aim:
To study file handling in Java and perform file writing operation using FileWriter class.
Theory:
File handling in Java is used to store data permanently in files. Unlike variables, file data is
not lost after program termination. Java provides file handling support through the [Link]
package, which contains classes for reading, writing, and managing files.
File handling is widely used in applications such as log management, report generation,
data storage, and backup systems.
Important File Handling Classes
1. File – Represents a file or directory
2. FileWriter – Writes data into a file
3. FileReader – Reads data from a file
4. BufferedReader – Improves reading efficiency
Steps Involved in File Writing
1. Create a FileWriter object
2. Write data into file
3. Close the file to save changes
Advantages of File Handling
1. Permanent data storage
2. Easy data access
3. Data backup
4. Improved data security
5. Efficient data management
Real-World Example
File handling is used in student record systems where student details are stored in files for
future reference.
Thus, file handling is an essential feature of Java programming.
Algorithm:
1. Start the program.
2. Import [Link] package.
3. Create a FileWriter object.
4. Write data into the file.
5. Close the file.
6. Display confirmation message.
7. Compile the program.
8. Execute the program.
9. Stop the program.
Program Code:
import [Link].*;
class FileHandlingDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Welcome to Java File Handling");
[Link]();
[Link]("File written successfully");
}
}
Output:
File written successfully
Result:
Thus, the study of file handling in Java was completed successfully. File writing operation
was performed using the FileWriter class.
Practical No. 11
Title:
Study of Collections Framework in Java
Aim:
To study the Collections Framework in Java and implement the ArrayList class.
Theory:
The Collections Framework in Java provides a unified architecture for storing and
manipulating groups of objects. It is part of the [Link] package and is designed to
handle data dynamically. Unlike arrays, collections can grow and shrink in size during
program execution.
The framework provides interfaces, classes, and algorithms to perform operations such as
searching, sorting, insertion, deletion, and traversal of data.
Main Components of Collections Framework
1. Interfaces
o List
o Set
o Queue
o Map
2. Classes
o ArrayList
o LinkedList
o HashSet
o TreeSet
o HashMap
3. Algorithms
o Sorting
o Searching
o Shuffling
ArrayList Class
ArrayList is a commonly used class that implements the List interface. It allows:
Dynamic resizing
Duplicate elements
Indexed access
Advantages of ArrayList:
1. Fast random access
2. Dynamic size
3. Easy insertion and deletion
4. Built-in methods
Real-World Example
An ArrayList can be used to store student names, product lists, or employee details where the
number of elements is not fixed.
Thus, the Collections Framework simplifies data management in Java programs.
Algorithm:
1. Start the program.
2. Import [Link] package.
3. Create an ArrayList object.
4. Add elements to the list.
5. Display the elements.
6. Compile the program.
7. Execute the program.
8. Observe the output.
9. Stop the program.
Program Code:
import [Link].*;
class CollectionDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]("C++");
[Link]("Languages List:");
[Link](list);
}
}
Output:
Languages List:
[Java, Python, C++]
Result:
Thus, the study of Collections Framework in Java was completed successfully. The ArrayList
class was implemented to store and display dynamic data.
Practical No. 12
Title:
Study of Multithreading in Java
Aim:
To study multithreading in Java and implement thread creation using Thread class.
Theory:
Multithreading is a process of executing multiple threads simultaneously. A thread is the
smallest unit of execution within a program. Java provides built-in support for multithreading
to improve CPU utilization and application performance.
Multithreading is commonly used in games, animations, server applications, and
multitasking software.
Thread Life Cycle
A thread goes through the following states:
1. New
2. Runnable
3. Running
4. Waiting
5. Terminated
Ways to Create a Thread in Java
1. Extending the Thread class
2. Implementing the Runnable interface
In this experiment, thread creation is done using the Thread class.
Advantages of Multithreading
1. Efficient CPU utilization
2. Faster execution
3. Improved responsiveness
4. Better resource sharing
5. Parallel processing
Real-World Example
Web servers use multithreading to handle multiple client requests simultaneously.
Thus, multithreading plays a crucial role in modern Java applications.
Algorithm:
1. Start the program.
2. Create a class that extends Thread.
3. Override the run() method.
4. Create a thread object.
5. Start the thread using start() method.
6. Compile the program.
7. Execute the program.
8. Observe the output.
9. Stop the program.
Program Code:
class MyThread extends Thread {
public void run() {
[Link]("Thread is running");
}
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
Output:
Thread is running
Result:
Thus, the study of multithreading in Java was completed successfully. A thread was created
and executed using the Thread class.