0% found this document useful (0 votes)
6 views25 pages

Oopj Labmanual

The document is a laboratory manual for the Object Oriented Programming with JAVA course at SAL Institute of Diploma Studies for the academic year 2024-2025. It outlines the institute's vision and mission, as well as the department's goals, and provides a detailed index of experiments, objectives, theories, and exercises related to Java programming. Each experiment focuses on different aspects of Java, including environment setup, control statements, classes and objects, string functions, and the use of keywords.

Uploaded by

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

Oopj Labmanual

The document is a laboratory manual for the Object Oriented Programming with JAVA course at SAL Institute of Diploma Studies for the academic year 2024-2025. It outlines the institute's vision and mission, as well as the department's goals, and provides a detailed index of experiments, objectives, theories, and exercises related to Java programming. Each experiment focuses on different aspects of Java, including environment setup, control statements, classes and objects, string functions, and the use of keywords.

Uploaded by

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

SAL Institute of Diploma Studies, AHMEDABAD

Department of Information Technology

Object Oriented Programming with JAVA


(4341602)

Laboratory Manual

Academic Year: 2024-2025

Prepared By:

Ms. Pooja Vora


Ms. Smita Upadhyay
SAL Education
SAL Institute of Diploma Studies

Institute Vision and Mission

Institute Vision:

To prepare computer and IT professionals for businesses, organizations, society, and form the
backbone of the modern world.

Institute Mission:

1. Empower students through rigorous academic programs and hands-on learning experiences that
cultivate technical expertise and problem-solving skills in addition moralvalues.

2. Foster a culture of innovation by encouraging entrepreneurship, and collaboration among


students, faculty, and industry partners.

3. Engage with the community and stakeholders to understand their needs and challenges, and
develop initiatives that address these through technology-driven solutions and innovations.

Department Vision and Mission

Department Vision:
Nurturing a new generation of visionary engineers, equipped with cutting-edge education and
hands-on experience, to pioneer transformative advancements in technology and lead the digital
evolution of tomorrow.
Department Mission:

1. Providing a rigorous and innovative environment that integrates theoretical knowledge


with practical skills, preparing students to tackle real-world challenges.

2. Cultivating a culture of team work, creativity and exploration, encouraging students to


think critically, innovate boldly, and push the boundaries of technology

3. Facilitating meaningful hands-on experiences, such as internships, research projects, and


industry collaborations, to bridge academic learning with practical application.
INDEX

Page No.
Sr.No. Experiment Date Marks Signature

From To

1. Install JDK and Setup a Java


Programming development
environment and configure and Test
it.
2. Develop programs to demonstrate use
of – Control statement and Looping
Statement.
3. Develop a basic java program that
demonstrates the use of Class &
Object, method overloading.
4. Develop a java program that
demonstrates the string function and
wrapper class.
5. Develop a program to demonstrate
use of static Keyword and this
keyword.
6. Develop a program to demonstrate
different types of constructor.
7. Develop a program to demonstrate
different types of inheritance.
8. Develop a java program that
demonstrates the use of Abstract
class, super and final keyword.
9. Develop a java program that
illustrates interface inheritance,
Package.
10. Develop a java program that
illustrates that read, write a text file
using exception Handling &
synchronization of threads concept.

2
Experiment No: 1 Date: / /

TITLE: Install JDK and Setup a Java Programming development environment and
configure and Test it.

OBJECTIVES:
From this experiment, the student will be able to
 How to install and configure Java(JDK) and Editor
 How to Run a Program and Debug it.

THEORY:
Install JDK and Setup a Java Programming development environment and configure.
Step 1: Download the latest version of the Java JDK from Sun Microsystems.
Step 2: Double-click on the install file and it should open an installer
Step 3: Click next, then read and accept the license
Step 4: The next page you encounter should install (and in some cases download) the Java
Development Kit
Step 5: After the installer is finished, open run by clicking Start > Run... or by typing Windows
Key + R.
Step 6: In the text box, type "cmd" and click "OK". A simple window should be opened with a
black background and a text prompt. This is called the "Command Prompt." My command
prompt background is red, but it could really be any colour
Step 7: After focusing the window, type "javac" and press enter. If the prompt returns something
along the lines of: "javac" is not recognized as an internal or external command, operable
program or batch file" then continue with the next step.
Step 8: Open the properties of "My Computer" by either right-clicking the icon on the desktop or
right-clicking Start > My Computer. When the popup menu opens, scroll to the bottom and select
"Properties".
Step 9: This should open a window named "System Properties". Click on the "Advanced" tab.
Step 10: Click "Environment Variables".
Step 11: Double-click on the "Path" variable on either of the option boxes. It is recommended to
edit the variable in the box "User variables for (your username)".At the end of the text box, add a
semicolon if there is not one already, and add "C:\Program Files\Java\jdk1.6.0\bin" to the text
box. This is assuming you did not change the file path of the installation.
Step 12: Click "Apply" and "OK" to all the windows you have just opened. Open the command
prompt again, while following steps 6-9 to see if that "javac" command works.

Following is a step by step guide to download and install Eclipse IDE:


Step 1. Install Eclipse.
1. Click on “downloads” in Windows file explorer.
2. Click on “eclipse-inst-win64.exe” file.
Step 2. Click on Run button

3
Step 3. Click on “Eclipse IDE for Java Developers”
Step 4. Click on “INSTALL” button
Step 5. Click on “LAUNCH” button.
Step 6. Click on “Create a new Java project” link.
Step 7. Create a new Java Project
1. Write project name.
2. Click on “Finish button”.

Data Types in Java


Data types specify the different sizes and values that can be stored in the variable. There are two types of
data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float
and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

Figure 1.1 Data Type Hierarchy

EXERCISE:
1. Test the java development environment setup by implementing a simple java program
(print: “OOP with JAVA”) and also use primitive data types.
2. Write a Java program to swap two numbers without using a temporary variable and
with using a temporary variable (use command line argument to accept value from
user).
EVALUATION:

Problem Analysis Understanding Timely Mock Total


& Solution Level Completion (2) (10)
(3) (3) (2)

Signature with date:

4
Experiment No: 2 Date: / /

TITLE: Develop programs to demonstrate use of – Control statement and Looping


Statement.

OBJECTIVES:
After completing this experiment students will be able to…
 Learn the basic java program structure.
 Learn how to design a basic java program using simple statements.

THEORY:
Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, Java provides statements that can be used
to control the flow of Java code. Such statements are called control flow statements. It is one of
the fundamental features of Java, which provides a smooth flow of program.
Java provides three types of control flow statements.
Decision Making or Control statements
o if, if_else statements
Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
o switch statement
Syntax
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
case valueN:
statementN;
break;
default:
statementDefault;
}

5
Loop statements
o do while loop
Syntax
do
{
statements..
}
while (condition);
o while loop
Syntax
while (boolean condition)
{
loop statements...
}
o for loop
Syntax
for (initialization Condition; testing Condition; increment/decrement)
{
statement(s)
}
o for-each loop
Syntax
for (type var : array)
{
Statements using var;
}
Jump statements
o break statement
Syntax
break;
o continue statement
Syntax
continue;
EXERCISE:
1. Write a Java program to find maximum and minimum numbers from array elements.
2. Write a Java program to demonstrate switch case statement.
3. Write a Java program to sum of ten numbers using do…While Loop.
EVALUATION:

Problem Analysis Understanding Timely Mock Total


& Solution Level Completion (2) (10)
(3) (3) (2)

Signature with date:


6
Experiment No: 3 Date: / /

TITLE: Develop a basic java program that demonstrates the use of Class & Object, method
overloading.

OBJECTIVES:
After completing this experiment students will be able to…
 In object-oriented programming technique, we design a program
using objects and classes.
 Enhance your understanding through illustrative examples, diving into fundamental
concepts
THEORY:
What is an Object?
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table,
car, etc. It can be physical or logical (tangible and intangible).
o State: represents the data (value) of an object.
o Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
o Identity: An object identity is typically implemented via a unique ID. The value of the ID is
not visible to the external user. However, it is used internally by the JVM to identify each
object uniquely.

What is a class?
A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:


Fields
Methods
Constructors
Blocks
Nested class and interface
Syntax to declare a class:
class <class_name>
{
field;
method;
}

What is variable?
A variable which is created inside the class but outside the method is known as an instance
variable. Instance variable doesn't get memory at compile time.

It gets memory at runtime when an object or instance is created. That is why it is known as an
instance variable.
7
What is Method?
In Java, a method is like a function which is used to expose the behavior of an object.
Advantage of Method
o Code Reusability
o Code Optimization

What is Method Overloading?


If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.

EXERCISE:
1. Write a java program to display employee id using Employee class and object.
2. Write a java program to find the factorial of a given number using a recursive function.
3. Write a java program to find the Area of Rectangle and Circle using method Overloading.

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

8
Experiment No: 4 Date: / /

TITLE: Develop a java program that demonstrates the string function and wrapper class.

OBJECTIVES:
After completing this experiment students will be able to…
 Learn to format numbers into strings.
 Learn about commonly used methods Understand immutability of strings.
 Learn about literal strings.

THEORY:
Java String class
In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string.
Java String class provides a lot of methods to perform operations on strings such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. The
java.lang.String class implements Serializable, Comparable and CharSequence interfaces.

Java StringBuffer Class


Java StringBuffer class is used to create mutable (modifiable) String objects. The StringBuffer
class in Java is the same as String class except it is mutable i.e. it can be changed.

Wrapper classes in Java


The wrapper class in Java provides the mechanism to convert primitive into object and object
into primitive.

Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects
into primitives automatically. The automatic conversion of primitive into an object is known as
autoboxing and vice-versa unboxing.

The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight
wrapper classes is given below:
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

9
Use of Wrapper classes in Java
Java is an object-oriented programming language, so we need to deal with objects many times
like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where
we need to use the wrapper classes.
o Change the value in Method: Java supports only call by value. So, if we pass a
primitive value, it will not change the original value. But, if we convert the primitive
value in an object, it will change the original value.
o Serialization: We need to convert the objects into streams to perform the serialization. If
we have a primitive value, we can convert it in objects through the wrapper classes.
o Synchronization: Java synchronization works with objects in Multithreading.
o java.util package: The java.util package provides the utility classes to deal with objects.
o Collection Framework: Java collection framework works with objects only. All classes
of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet,
TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

EXERCISE:
1. Write a Java program that demonstrates the string function.
2. Write a Java program for implementation of Wrapper Class to convert primitive value
into object (Boxing) and object into primitive value (Un-boxing).

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

10
Experiment No: 5 Date: / /

TITLE: Develop a program to demonstrate use of static Keyword and this keyword.

OBJECTIVES:
After completing this experiment students will be able to…
 How to save memory in java.
 How to refer current instance in Java.

THEORY:
Static Keyword
The static keyword in Java is mainly used for memory management. The static keyword in
Java is used to share the same variable or method of a given class. The users can apply static
keywords with variables, methods, blocks, and nested classes. The static keyword belongs to
the class than an instance of the class.
The static keyword is a non-access modifier in Java that is applicable for the following:
1. Blocks
2. Variables
3. Methods
4. Classes
Characteristics of static keyword
 Shared memory allocation
 Accessible without object instantiation
 Associated with class, not objects
 Cannot access non-static members
 Can be overloaded, but not overridden

this Keyword
In Java, „this‟ is a reference variable that refers to the current object, or can be said “this” in
Java is a keyword that refers to the current object instance.

It can be used to call current class methods and fields, to pass an instance of the current class
as a parameter, and to differentiate between the local and instance variables.

Using “this” reference can improve code readability and reduce naming conflicts.

Methods to use ‘this’ in Java


Following are the ways to use the „this‟ keyword in Java mentioned below:
 Using the „this‟ keyword to refer to current class instance variables.
 Using this() to invoke the current class constructor
 Using „this‟ keyword to return the current class instance
 Using „this‟ keyword as the method parameter
 Using „this‟ keyword to invoke the current class method
 Using „this‟ keyword as an argument in the constructor call

11
EXERCISE:
1. Write a java program that demonstrates the static keyword.
2. Write a java program that demonstrates this keyword. Check whether „this‟ can access
the private members of the class or not.

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

12
Experiment No: 6 Date: / /

TITLE: Develop a program to demonstrate different types of constructor.

OBJECTIVES:
After completing this experiment students will be able to…
 How to initialize the object with the default or initial state.
 How to use multiple constructors in a one class.

THEORY:
In Java, a Constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling the constructor, memory for the object is allocated in
the memory. It is a special type of method that is used to initialize the object. Every time an
object is created using the new() keyword, at least one constructor is called.

How Java Constructors are Different from Java Methods?


 Constructors must have the same name as the class within which it is defined it is not
necessary for the method in Java.
 Constructors do not return any type while method(s) have the return type or void if does
not return any value.
 Constructors are called only once at the time of Object creation while method(s) can be
called any number of times.

Types of Constructors in Java


There are three types of constructors in Java are mentioned below:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor

1. Default Constructor
A constructor that has no parameters is known as default the constructor.
2. Parameterized Constructor
A constructor that has parameters is known as parameterized constructor.
3. Copy Constructor
Copy constructor is passed with another object which copies the data available from the
passed object to the newly created object.

Constructor Overloading
The constructor overloading can be defined as the concept of having more than one constructor
with different parameters so that every constructor can perform a different task.

EXERCISE:
1. Write a Java program to demonstrate the use of private constructor and also write a
method which will count the number of instances created using default constructor only.
2. Write a Java program with an overloaded constructor. Also develop the copy constructor
to create a new object with the state of the existing object.

13
EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

14
Experiment No: 7 Date: / /

TITLE: Develop a program to demonstrate different types of inheritance.

OBJECTIVES:
After completing this experiment students will be able to…
 Learn about how to create a new class from an existing class.
 Why it is very useful?

THEORY:
Inheritance is the most powerful feature of object-oriented programming. It allows us to inherit
the properties of one class into another class. Inheritance is a mechanism of driving a new class
from an existing class. The existing (old) class is known as base class or super class or parent
class. The new class is known as a derived class or sub class or child class. It allows us to use the
properties and behavior of one class (parent) in another class (child).

Why we use Inheritance?


Inheritance provides the reusability of code especially when there is a large scale of code to
reuse. It also establishes the relationship between different classes that is known as a Is-
a relationship. We can also use it if we want to achieve method overriding.

Types of Inheritance
Java supports the following four types of inheritance:
1. Single Inheritance
2. Multi-level Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance

Method Overriding in Java


If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.

Rules for Java Method Overriding


 The method must have the same name as in the parent class
 The method must have the same parameter as in the parent class.
 There must be an IS-A relationship (inheritance).

Static Binding and Dynamic Binding


Connecting a method call to the method body is known as binding.
There are two types of binding
1. Static Binding (also known as Early Binding).
When type of the object is determined at compiled time(by the compiler), it is known as
static binding.
2. Dynamic Binding (also known as Late Binding).
When type of the object is determined at run-time, it is known as dynamic binding.

15
EXERCISE:
1. Write a Java program to demonstrate multilevel inheritance.
2. Write a Java program with one class named shape which has two member functions
named erase () and draw (). In the program we have three other subclasses: circle,
triangle and square. Override methods of the super class into subclasses.
3. Write a Java program for implementation of Dynamic method dispatch.

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

16
Experiment No: 8 Date: / /

TITLE: Develop a java program that demonstrates the use of Abstract class, super and final
keyword.

OBJECTIVES:
After completing this experiment students will be able to…
 How to “shows” only essential attributes and “hides” unnecessary information in
java.
 Access parent class when we're working with objects.
THEORY:
Abstraction is a process of hiding the implementation details and showing only functionality
to the user.
Abstract class in Java
A class which is declared as abstract is known as an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
 An abstract class must be declared with an abstract keyword.
 It can have abstract and non-abstract methods.
 It cannot be instantiated.
 It can have constructors and static methods also.
 It can have final methods which will force the subclass not to change the body of the
method.
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an
abstract method.

Super Keyword in Java


The super keyword in Java is a reference variable which is used to refer immediate parent class
object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly
which is referred by super reference variable.

Usage of Java super Keyword


 super can be used to refer immediate parent class instance variable.
 super can be used to invoke immediate parent class method.
 super() can be used to invoke immediate parent class constructor.

Final Keyword in Java


The final keyword in java is used to restrict the user. The java final keyword can be used in many
contexts. Final can be:
 variable
 method
 class
The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.
17
EXERCISE:
1. Write a java program that demonstrates the use of Abstract class.
2. Write a java program to demonstrate the use of „super‟ and „final‟ keywords.

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

18
Experiment No: 9 Date: / /

TITLE: Develop a java program that illustrates interface inheritance, Package.

OBJECTIVES:
After completing this experiment students will be able to…
 How to achieve abstraction in java.
 How to define package, access package in java.

THEORY:
Interface:
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not the method body. It is used to achieve abstraction
and multiple inheritances in Java using Interface. In other words, you can say that interfaces
can have abstract methods and variables. It cannot have a method body. Java Interface
also represents the IS-A relationship.

When we decide on a type of entity by its behavior and not via attribute we should define it as
an interface.

Syntax
interface
{
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use the interface keyword. It is used to provide total abstraction. That
means all the methods in an interface are declared with an empty body and are public and all
fields are public, static, and final by default.
A class that implements an interface must implement all the methods declared in the interface.
To implement the interface, use the implements keyword.
Uses of Interfaces in Java are mentioned below:
 It is used to achieve total abstraction.
 Since java does not support multiple inheritances in the case of class, by using an
interface it can achieve multiple inheritances.
 Any class can extend only 1 class, but can any class implement an infinite number of
interfaces.
 It is also used to achieve loose coupling.
 Interfaces are used to implement abstraction.

Package
In Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.
Packages are used for:
 Preventing naming conflicts.

19
 Making searching/locating and usage of classes, interfaces, enumerations and annotations
easier.
 Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in the same package only.
 Packages can be considered as data encapsulation (or data-hiding).
Example
import java.util.*;
util is a subpackage created inside java package.

Types of Package
There are two types of package.
1. Built-in Packages
These packages consist of a large number of classes which are a part of Java API. Some
of the commonly used built-in packages are:
1. java.lang:
Contains language support classes (e.g classed which defines primitive data types,
math operations). This package is automatically imported.
2. java.io:
Contains classed for supporting input / output operations.
3. java.util:
Contains utility classes which implement data structures like Linked List,
Dictionary and support; for Date / Time operations.
4. java.applet:
Contains classes for creating Applets.
5. java.awt:
Contain classes for implementing the components for graphical user interfaces (like
button, menus etc).
6. java.net:
Contain classes for supporting networking operations.

2. User-defined packages
These are the packages that are defined by the user.

EXERCISE:
1. Write a java program that illustrates interface inheritance. Interface „A1‟ and „A2‟ are
extended from interface „A‟. Interface „A12‟ inherited from both „A1‟ and „A2‟. Each
interface declares one method and one constant. Class „Interface_Imple‟ implements
„A12‟. Instantiate „Interface_Imple‟ and invoke each of its methods. Each method
displays one of the constants.
2. Write a Java program to create a Package and demonstrate how packages are used in
java. And use java access modifier to demonstrate the access rules in a package.

20
EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

21
Experiment No: 10 Date: / /

TITLE: Develop a java program that illustrates that read, write a text file and exception
Handling & synchronization of threads concept.

OBJECTIVES:
After completing this experiment students will be able to…
 How to handle exception.
 How to synchronize thread.
 How to handle file operation in java.

THEORY:
Exception Handling in Java
In Java, an exception is an event that disrupts the normal flow of the program. It is an object
which is thrown at runtime.

The Exception Handling in Java is one of the powerful mechanisms to handle the runtime
errors so that the normal flow of the application can be maintained. Exception Handling is a
mechanism to handle runtime errors such as ClassNotFoundException, IOException,
SQLException etc.

Types of Java Exceptions


The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two
subclasses: Exception and Error.

There are mainly two types of exceptions: checked and unchecked. An error is considered as the
unchecked exception. However, according to Oracle, there are three types of exceptions namely:
1. Checked Exception
2. Unchecked Exception
3. Error

Java try..catch...finally block

Java try block is used to enclose the code that might throw an exception. It must be used within
the method. If an exception occurs at the particular statement in the try block, the rest of the
block code will not execute. So, it is recommended not to keep the code in try block that will not
throw an exception.

Java try block must be followed by either catch or finally block.

Syntax of Java try-catch


try{
//code that may throw an exception
}catch(Exception_class_Name ref)
{}

22
Syntax of try-finally block
try{
//code that may throw an exception
}finally{}

Synchronization in Java
Synchronization in Java is the capability to control the access of multiple threads to any shared
resource. Java Synchronization is better option where we want to allow only one thread to access
the shared resource.
Why use Synchronization?
The synchronization is mainly used to
 To prevent thread interference.
 To prevent consistency problem.
Types of Synchronization
There are two types of synchronization
1. Process Synchronization
2. Thread Synchronization

File Handling in JAVA


Java I/O (Input and Output) is used to process the input and produce the output. Java uses the
concept of a stream to make I/O operation fast. The java.io package contains all the classes
required for input and output operations. We can perform file handling in Java by Java I/O API.
Stream
A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream
because it is like a stream of water that continues to flow. In Java, 3 streams are created for us
automatically. All these streams are attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
OutputStream
Java application uses an output stream to write data to a destination; it may be a file, an array,
peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source; it may be a file, an array,
peripheral device or socket.

Figure 10.1 working of Java OutputStream and InputStream

23
EXERCISE:

1. Write a Java program to create a Package and demonstrate how packages are used in
java. And use a java access modifier to demonstrate the access rules in a package.
2. Write a Java programs to create, write, modify, read operations on Text files.
3. Write a Java program that executes two threads. One thread will print the even numbers
and the another thread will print odd numbers between 1 to 10.(Create the thread by
implementing runnable interface).
4. Write a Java program that executes two threads. One thread displays “Java
Programming” every 2 seconds, and the other displays “Semester - 4th” every 5
seconds.(Create the threads by extending the Thread class)

EVALUATION:

Problem Understanding Timely Mock Total


Analysis & Level Completion (2) (10)
Solution (3) (2)
(3)

Signature with date:

24

You might also like