0% found this document useful (0 votes)
0 views79 pages

Object Oriented Programming in Java Course Level Coding Cheat Sheet

This document is a course-level coding cheat sheet for object-oriented programming in Java, providing a reference list of code examples and explanations from various topics including classes, access modifiers, encapsulation, constructors, and collections. It includes detailed examples of creating classes and objects, using access modifiers, encapsulation techniques, and implementing constructors. The cheat sheet serves as a resource for writing and debugging Java programs.

Uploaded by

anilbs6364
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)
0 views79 pages

Object Oriented Programming in Java Course Level Coding Cheat Sheet

This document is a course-level coding cheat sheet for object-oriented programming in Java, providing a reference list of code examples and explanations from various topics including classes, access modifiers, encapsulation, constructors, and collections. It includes detailed examples of creating classes and objects, using access modifiers, encapsulation techniques, and implementing constructors. The cheat sheet serves as a resource for writing and debugging Java programs.

Uploaded by

anilbs6364
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/ 79

4/11/25, 6:50 PM about:blank

Course-level coding cheat sheet


This course-level reading provides a reference list of code that you'll encounter as you work with object-oriented coding in Java. Use this cheat sheet code as an ongoing
resource to help you write and debug object-oriented Java programs.

Select the hamburger menu located at the top left of the window to quickly locate code and explanations based on the video name. You can also use the forward and back
arrows to navigate between pages.

This course-level cheat sheet includes code and related explanations from the following videos.

Working with Classes and Objects


Working with Access and Non-access Modifiers
Using Encapsulation
Using Constructors
Inheritance in Java
Polymorphism in Java
Designing interfaces and Abstract Classes in Java
Inner classes in Java
Java Collections Framework (JCF)
Working with lists
HashSet and TreeSet
Implementing Queues in Java
Using HashMap and TreeMap
Using Java collections in the Real World
Java File Handling / Working with File Input and Output Streams
Using Java Byte Streams
Managing Directories in Java
Using Java Date and Time Classes
Formatting Dates in Java
Using Timezones in Java
Parsing Dates from Strings in Java

Working with Classes and Objects


Creating a class

Description Example

public class Car {

Create a Car class, which serves as a blueprint for creating Car objects.

String color;
String model;
int year;

Define attributes of the Car class. The variables color, model, and year store the car's color,
model, and year, respectively.

void displayInfo() {

Include the method displayInfo() to print car objects.

Print the car details to the console using the System.out.println() function. System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println(>System.out.println("Car Year: " + year);

about:blank 1/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the Car class definition.

Explanation: This example creates a class named Car and defines three attributes for the Car class: model, color, and year. The displayInfo() method prints the car
details.

Creating an object

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Car myCar = new Car();

Create an object of the Car class.

myCar.color = "Red";
myCar.model = "Toyota";
myCar.year = 2020;

Assign values to the object's attributes.

myCar.displayInfo();

Call the displayInfo() method to print the object details.

Close curly braces to end the main method and class definition. }
}

about:blank 2/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This example declares a reference variable named myCar of type Car. new Car() creates a new object of the Car class and assigns values to the object's
attributes: color, model and year. The displayInfo() method prints the car details.

Working with access and non-access modifiers


Public access modifier

Description Example

public class Car {

A Java statement used to define a class named Car, which acts as a blueprint for creating Car objects. The variable model is declared
as public, meaning it can be accessed directly from outside the class.

public String model;

A Java statement to declare a String variable named model to store the car's model name.

Close curly braces to end the class definition.

Private access modifier

Description Example

public class Car {

A Java statement used to define a class named Car, which acts as a blueprint for creating Car objects.
The variable model is declared as public, meaning that it can be accessed directly from outside the
class.

private String color;

A Java statement to declare a private String variable named color to store the car's color. The private
modifier ensures the color variable can be accessed and modified only within the Car class.

Call the displayColor() method with the private access modifier. This ensures the method can be private void displayColor() {
called only within the Car class and not from other classes.

about:blank 3/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println("Car Color: " + color);

Print the car's color to the console using the System.out.println() function.

}
}

Close curly braces to end the class definition.

Protected access modifier

Description Example

public class Car {

A Java statement used to define a class named Car, which acts as a blueprint for creating Car objects.
The variable model is declared as public, meaning that it can be accessed directly from outside the
class.

private String year;

A Java statement to declare a protected int variable named year to store the car's year. The protected
modifier ensures the year variable is accessible within the same package (default package access) and
by subclasses, even if they are in different packages.

private void displayYear() {

Call the displayYear() method with the protected access modifier. This ensures the method can be
called within the same package and by subclasses, even if they are in different packages.

System.out.println("Car Year: " + year);

Print the car's year to the console using the System.out.println() function.

Close curly braces to end the class definition. }


}

about:blank 4/79
4/11/25, 6:50 PM about:blank

Description Example

Default access modifier

Description Example

class Car {

A Java statement used to define a class named Car, which acts as a blueprint for creating Car objects.

String model;

A Java statement to declare a String variable named model without any access modifier. If no access
modifier is used, the variable is considered default. Default variables are accessible only within their
own package.

void displayModel() {

Call the displayModel() method without any access modifier.

System.out.println("Car Model: " + model);

Print the car's model to the console using the System.out.println() function.

}
}

Close curly braces to end the class definition.

Static non-access modifier

Description Example

public class Car {

A Java statement used to define a class named Car, which acts as a blueprint for creating Car
objects. The variable model is declared as public, meaning that it can be accessed directly
from outside the class.

about:blank 5/79
4/11/25, 6:50 PM about:blank

Description Example

static int numberOfCars = 0;

A Java statement to declare a static int variable named numberOfCars to keep track of the
total number of Car objects created. Since it's static, its value is shared among all instances of
Car.

public Car() {

A Java statement to declare a constructor. Every time a new Car object is created, this
constructor runs.

numberOfCars++;

A Java statement to increment the numberOfCars variable that keeps track of how many cars
have been instantiated.

Close curly braces to end the class definition.

private void displayCount() {

Call the displayCount() method without creating an instance of the Car class. This method
can only access static variables such as numberOfCars, not instance variables.

System.out.println("Total Cars: " + numberOfCars);

Print the total number of cars to the console using the System.out.println() function.

}
}

Close curly braces to end the class definition.

Final non-access modifier

Description Example

A Java statement used to define a final class named Vehicle, which acts as a blueprint for creating public final class Vehicle {
Car objects. The final class cannot be extended (inherited) by any other class. This means no

about:blank 6/79
4/11/25, 6:50 PM about:blank

Description Example
subclasses can be created from Vehicle.

final int maxSpeed = 120;

A Java statement to declare a final int variable named maxSpeed with the value 120. The final
variable is a constant, meaning that its value cannot be changed once it is assigned. Trying to
modify maxSpeed later in the code will cause a compilation error.

final void displayMaxSpeed() {

A Java statement to declare a final method named displayMaxSpeed(). The final method cannot be
overridden by subclasses. This ensures the behavior of displayMaxSpeed remains the same in all
instances.

System.out.println("Max Speed: " + maxSpeed);

Print the maximum car speed to the console using the System.out.println() function.

}
}

Close curly braces to end the class definition.

Abstract non-access modifier

Description Example

public abstract class Shape {

A Java statement used to define an abstract class named Shape. This is an abstract class, meaning that it
cannot be instantiated (you cannot create Shape objects directly). It works as a blueprint from which other
classes can inherit.

abstract void draw();

A Java statement used to define a final class named Vehicle, which acts as a blueprint for creating Car
objects. The final class cannot be extended (inherited) by any other class. This means no subclasses can be
created from Vehicle.

Close curly braces to end the class definition. }

about:blank 7/79
4/11/25, 6:50 PM about:blank

Description Example

public class Circle extends Shape {

A Java statement to describe Circle that extends the Shape class and provides an implementation of the
draw() method.

@Override

A Java annotation to tell the compiler the draw() method in Circle is an override of the abstract method in
Shape.

void draw()

A Java statement saying the draw() method is now fully implemented.

System.out.println("Drawing Circle");

Print the string Drawing Circle to the console using the System.out.println() function.

}
}
}

Close curly braces to end the class definition.

Using encapsulation
Creating an encapsulated class

Description Example

class Person {

Create the Person class, which serves as a blueprint for creating Person objects.

about:blank 8/79
4/11/25, 6:50 PM about:blank

Description Example

private String name;


private int age;

Create private attributes name and age to store the person's name and age. The name and age
attributes cannot be accesse diretly from outside the class.

public Person(String name, int age) {

Use the Java constructor to initialize the name and age variables when a Person object is
created.

this.name = name;
this.age = age;

The keyword this refers to the current object's instance variables. It differentiates instance
variables from method parameters.

Close curly braces to end the class definition.

public String getName() {

Use the Java public method (Getter) to obtain read access to private variables.

return name;

getName() returns the value of name.

Close curly braces to end the class definition.

Use the Java public method (Setter) to obtain write access to private variables. public void setName(String name) {

about:blank 9/79
4/11/25, 6:50 PM about:blank

Description Example

this.name = name;

setName() updates name.

public int getAge() {

Use the Java public method (Getter) to obtain read access to private variables.

return age;

getAge() returns the value of age.

Close curly braces to end the class definition.

public void setAge(int age) {

Use the Java public method (Setter) to obtain write access to private variables.

if (age >= 0) {

Use the Java if statement to ensure age is not negative before assigning.

this.age = age;

Update the age variable.

Use the Java else statement to specify what to do when the age is negative. } else {

about:blank 10/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println("Age cannot be negative.");

Print the string Age cannot be negative to the console using the System.out.println()
function.

}
}

Close curly braces to end the class definition.

Explanation: This example creates a Person class in which the name and age attributes are declared as private, meaning they cannot be accessed directly from outside the
Person class. The constructor Person(String name, int age) initializes the attributes when a new object of the class is created. getName() and getAge() are getter methods
that allow other classes to read the values of name and age. setName(String name) and setAge(int age) are setter methods that allow other classes to modify the values of
name and age. The setter for age includes validation to ensure age cannot be set to a negative number.

Using an encapsulated class

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the
program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This
method is required for execution in Java programs.

Person person = new Person("Alice", 30);

Create a new instance of the Person class. Assign the value "Alice" to the name attribute
and the value "30" to the age attribute.

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

Use the getName() getter to obtain and print the value of the name attribute.

Use the getAge() getter to obtain and print the value of the age attribute. System.out.println("Age: " + person.getAge());

about:blank 11/79
4/11/25, 6:50 PM about:blank

Description Example

person.setName("Bob");
person.setAge(25);

Use the setName() setter to assign the value of name attribute to "Bob" and age attribute to
"25".

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

Use the getName() getter to obtain and print the updated value of the name attribute.

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

The setAge(-5) call attempts to set an invalid age. Since setAge() has validation logic, it
will print "Age cannot be negative."

}
}

Close curly braces to end the class definition.

Explanation: This example creates an instance of the Person class with the name "Alice" and age "30". We call the getName() and getAge() getter methods to print the
values. We then update the name and age attributes usint the setName() and setAge() setter methods. When we attempt to set a negative age with setAge(-5), it prints an
error message because of validation included in the setter method.

Using constructors
Creating a default constructor

Description Example

class Dog {

A Java statement used to define a class named Dog, which acts as a blueprint for creating Dog objects.

A Java statement to declare a String variable named name without any access modifier. If no access String name;
modifier is used, the variable is considered default. Default variables are accessible only within their
own package.

about:blank 12/79
4/11/25, 6:50 PM about:blank

Description Example

Dog() {

This is the default constructor. It takes no arguments.

name = "Unknown";

The default constructor initializes the name variable with the value "Unknown". This ensures every
new Dog object always hasa name, even if the user doesn't provide one.

Close curly braces to end the class definition.

void display() {

Call the display() method without any access modifier.

System.out.println("Dog's name: " + name);

Print the dog's name to the console using the System.out.println() function. Since name was
initialized in the constructor, it always has a value.

}
}

Close curly braces to end the class definition.

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

The main method is declared using public static void main(String[] args). This method is public static void main(String[] args) {
required for execution in Java programs.

about:blank 13/79
4/11/25, 6:50 PM about:blank

Description Example

Dog myDog = new Dog();

Create an instance of the Dog class using the default constructor. The name variable is automatically set
to "Unknown".

myDog.display();

Call the display() method to print the dog's name.

}
}

Close curly braces to end the class definition.

Explanation: This example creates an instance of the Dog class with a default constructor that initializes the name attribute to "Unknown". When we create the instance,
the default constructor is invoked automatically.

Creating a parameterized constructor

Description Example

class Dog {

A Java statement used to define a class named Dog, which acts as a blueprint for creating Dog objects.

String name;

A Java statement to declare a String variable named name without any access modifier. If no access
modifier is used, the variable is considered default. Default variables are accessible only within their
own package.

Dog(String dogName) {

This is the parameterized constructor that takes one argument dogName.

When the Dog object is created, the provided dogName value is assigned to the name variable. name = dogName;
Parameterized constructors let you assign a unique name to each Dog object when it is created.

about:blank 14/79
4/11/25, 6:50 PM about:blank

Description Example

Close curly braces to end the class definition.

void display() {

Call the display() method without any access modifier.

System.out.println("Dog's name: " + name);

Print the dog's name to the console using the System.out.println() function. Since name was
initialized in the constructor, it always has a value.

}
}

Close curly braces to end the class definition.

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is
required for execution in Java programs.

Dog myDog = new Dog("Buddy");

Create an instance of the Dog class. "Buddy" is passed as an argument to the constructor, setting name
to "Buddy".

Call the display() method to print the dog's name. myDog.display();

about:blank 15/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the class definition.

Explanation: This example creates an instance of the Dog class with a parameterized constructor that takes a string parameter dogName. When we create a Dog instance
with the name "Buddy", the constructor initializes the name attribute with that value.

Creating a no-arg constructor

Description Example

class Car {

A Java statement used to define a class named Car, which acts as a blueprint for
creating Car objects.

String model;
int year;

A Java statement to declare a String variable named model and an int variable
named year without any access modifier. If no access modifier is used, the
variable is considered default. Default variables are accessible only within their
own package.

Car() {

This is a no-argument constructor that takes no parameters.

model = "Default Model";


year = 2020;

When the Car object is created, it automatically assigns the value "Default
Model" to model and 2020 to year.

Close curly braces to end the class definition.

Call the display() method without any access modifier. void display() {

about:blank 16/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println("Car Model: " + model + ", Year: " + year);

Print the car's model and year to the console using the System.out.println()
function.

}
}

Close curly braces to end the class definition.

public class Main {

A Java class named Main with a main method. The main method is the entry
point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args).
This method is required for execution in Java programs.

Car myCar = new Car();

Create an instance of the Car class. The no-argument constructor is called,


setting model = "Default Model" and year = 2020.

myCar.display();

Call the display() method to print the model and year of the car.

}
}

Close curly braces to end the class definition.

about:blank 17/79
4/11/25, 6:50 PM about:blank
Explanation: This example creates an instance of the Car class with two attributes model and year. The Car() constructor initializes the model to "Default Model" and year
to 2020. When we create an instance of the Car class with new Car(), the no-arg constructor is called automatically, and the default values are assigned to the attributes.
The display() method prints the model and year of the car.

Constructor overloading

Description Example

class Dog {

A Java statement used to define a class named Dog, which acts as a blueprint for
creating Dog objects.

String name;
int age;

A Java statement to declare a String variable named name and an int variable
named age without any access modifier. If no access modifier is used, the variable
is considered default. Default variables are accessible only within their own
package.

Dog() {

This is the default constructor. It takes no arguments.

name = "Unknown";
age = 0;

The default constructor initializes the name variable with the value "Unknown"
and age variable with the value 0. This ensures every new Dog object always has a
name and age, even if the user doesn't provide one.

Close curly braces to end the class definition.

Dog(String dogName) {

This is the parameterized constructor that takes one argument dogName.

name = dogName;
age = 0;

When the Dog object is created, the provided dogName value is assigned to name
while keeping the age as 0 by default. Parameterized constructors let you assign a
unique name to each Dog object when it is created.

about:blank 18/79
4/11/25, 6:50 PM about:blank

Description Example

Close curly braces to end the class definition.

Dog(String dogName, int dogAge) {

This is the parameterized constructor that takes two arguments dogName and
dogAge.

name = dogName;
age = dogAge;

When the Dog object is created, the constructor allows the user to specify both
name and age.

Close curly braces to end the class definition.

void display() {

Call the display() method without any access modifier.

System.out.println("Dog's name: " + name + ", Age: " + age);

Print the dog's name and age to the console using the System.out.println()
function. Since name and age were initialized in the constructor, they always have
a value.

}
}

Close curly braces to end the class definition.

A Java class named Main with a main method. The main method is the entry point public class Main {
of the program.

about:blank 19/79
4/11/25, 6:50 PM about:blank

Description Example

public static void main(String[] args) {

The main method is declared using public static void main(String[] args).
This method is required for execution in Java programs.

Dog dog1 = new Dog();

Create the dog1 object using the default constructor Dog(). So, name =
"Unknown" and age = 0.

Dog dog2 = new Dog();

Create the dog2 object using the one-parameter constructor Dog("Charlie"). So,
name = "Charlie" and age = 0 (default).

Dog dog3 = new Dog();

Create the dog3 object using the two-parameter constructor Dog("Max", 5). So,
name = "Max" and age = 5.

dog1.display();
dog2.display();
dog3.display();

Call the display() method on each object to print their details.

}
}

Close curly braces to end the class definition.

Explanation: This example has three constructors of the Dog class. Depending on the number of parameters provided when creating an object, the corresponding
constructor is called.

Inheritance in Java
Polymorphism in Java
Interfaces and abstract classes in Java
Inner classes in Java

Keep this summary reading available as a reference as you progress through your course, and refer to this reading as you begin coding with Java after this course!

Inheritance in Java

about:blank 20/79
4/11/25, 6:50 PM about:blank

Creating a superclass

Description Example

class Animal {

Create a superclass named Animal, which serves as a base class for other classes that might inherit from
it.

String name;

Define a String variable name to store the name of the animal.

void eat() {

Include a method eat() to print the message that the animal is eating.

System.out.println(name + " is eating.");

Print the message to the console using the System.out.println() function. The animal name is displayed
dynamically.

}
}

Close curly braces to end the Animal class definition.

Creating a subclass

Description Example

class Dog extends Animal {

The Dog class inherits from the Animal class, meaning it automatically gets all properties and methods
from Animal.

void bark() {

Include a method bark() to print the message that the dog is barking.

about:blank 21/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println(name + " says woof!");

Print the message to the console using the System.out.println() function. The animal name is displayed
dynamically.

}
}

Close curly braces to end the Animal class definition.

Using inheritance

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Dog myDog = new Dog();

Creates an instance of the Dog class. The Dog class inherits from the Animal class.

myDog.name = "Buddy";

Assigns "Buddy" to the name variable inherited from Animal.

myDog.eat();

Calls the eat() method from the Animal class, which prints "Buddy is eating.".

Calls the bark() method from the Dog class, which prints "Buddy says woof!". myDog.bark();

about:blank 22/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the Main class definition.

Using multilevel inheritance

Description Example

class Puppy extends Dog {

The Puppy class inherits from the Dog class. Since Dog already inherits from Animal, Puppy indirectly
inherits all properties and methods from Animal as well.

void weep() {

This method adds a new behavior specific to the Puppy class.

System.out.println(name + " is weeping.");

Print the message to the console using the System.out.println() function. The animal name is
displayed dynamically.

}
}

Close curly braces to end the Puppy class definition.

Explanation: This is an example of multilevel inheritance. Animal (Superclass) → Dog (Subclass) → Puppy (Subclass of Dog). The Animal class has attribute name and
method eat(). The Dog class inherits from Animal and adds the bark() method. Puppy inherits from Dog and adds the weep() method.

Using hierarchical inheritance

Description Example

The Cat class inherits from the Animal class. Since Animal contains the name variable and eat() method, class Cat extends Animal {
Cat inherits those properties.

about:blank 23/79
4/11/25, 6:50 PM about:blank

Description Example

void meow() {

This method adds a new behavior specific to the Cat class.

System.out.println(name + " says meow!");

Print the message to the console using the System.out.println() function. The animal name is displayed
dynamically.

}
}

Close curly braces to end the Cat class definition.

Explanation: This is an example of hierarchical inheritance because multiple subclasses (Dog and Cat) inherit from the same superclass (Animal). Animal has attribute name
and method eat(). Dog and Cat inherit from Animal, but each adds unique behaviors. Dog adds the bark() method and Cat adds the meow() method.

Method overriding

Description Example

class Animal {

Create a superclass named Animal, which serves as a base class for other classes that might inherit
from it.

void sound() {

Include a sound() method. This method is meant to be overridden by subclasses that define more
specific behavors.

System.out.println("Animal makes a sound");

Print the message "Animal makes a sound" to the console using the System.out.println() function.

Close curly braces to end the Animal class definition. }


}

about:blank 24/79
4/11/25, 6:50 PM about:blank

Description Example

Description Example

class Dog extends Animal {

The Dog class inherits from the Animal class.

@Override

Dog overrides the sound() method to provide a specific implementation: "Dog barks". The @Override annotation
tells the compiler that this method replaces the sound() method from Animal.

void sound() {

Include a sound() method to print the message "Dog barks".

System.out.println("Dog barks");

Print the message to the console using the System.out.println() function.

}
}

Close curly braces to end the Dog class definition.

Explanation: In this example, Dog provides its own implementation of sound(), replacing the one in Animal. Method overriding occurs when a subclass provides a specific
implementation of a method already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the
superclass.

Using overridden methods

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

about:blank 25/79
4/11/25, 6:50 PM about:blank

Description Example

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Animal myAnimal = new Animal();

Creates an instance of Animal and stores it in a variable myAnimal.

Animal myDog = new Dog();

The Dog object is stored in an Animal reference. Since Dog overrides the sound() method, Java uses dynamic
method dispatch to call the overridden method in Dog, not in Animal.

myAnimal.sound();

Since myAnimal is a regular Animal object, calling myAnimal.sound() executes the sound() method from the
Animal class.

myDog.sound();

Since myDog refers to a Dog object (even though it's declared as Animal), it calls the overridden sound()
method in Dog due to polymorphism.

}
}

Close curly braces to end the Main class definition.

Explanation: The Dog class inherits from Animal, meaning it gets all non-private properties and methods of Animal. Dog overrides the sound() method from Animal,
providing a more specific implementation. Even though myDog is declared as an Animal, Java determines the method to call at runtime, not compile time. When calling
myDog.sound(), Java looks at the actual object type (Dog) and calls sound() from Dog, not Animal.

Polymorphism in Java
Compile-time polymorphism

Description Example

Create a class MathOperations that contains multiple methods for performing class MathOperations {
addition.

about:blank 26/79
4/11/25, 6:50 PM about:blank

Description Example

int add(int a, int b) {

Include an add method that accepts two int values (a and b).

return a + b;

Add the values of a and b and return the sum to the calling method as an int.

Close curly braces to end the method.

int add(int a, int b, int c) {

Include an add method that accepts three int values (a, b, and c).

return a + b + c;

Add the values of a, b, and c and return the sum to the calling method as an
int. This method overloads the first add() method because it has different
number of parameters.

Close curly braces to end the method.

int add(double a, double b) {

Include an add method that accepts two double values (a and b).

Add the values of a and b and return the sum to the calling method as a double. return a + b;
This method overloads both of the previous add() methods, but it works with
double values instead of int.

about:blank 27/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the method and the MathOperations class definition.

public class Main {

A Java class named Main with a main method. The main method is the entry
point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args).
This method is required for execution in Java programs.

MathOperations math = new MathOperations();

Create an instance of the MathOperations class and assign it to the math object.

System.out.println("Sum of 2 and 3: " + math.add(2, 3));

Calls the method add(int a, int b) to add two integers (2 + 3) and print the
result to the console.

System.out.println("Sum of 2, 3 and 4: " + math.add(2, 3, 4));

Calls the method add(int a, int b, int c) to add three integers (2 + 3 + 4)


and print the result to the console.

System.out.println("Sum of 2.5 and 3.5: " + math.add(2.5, 3.5));

Calls the method add(double a, double b) to add two double values (2.5 +
3.5) and print the result to the console.

Close curly braces to end the Main class definition. }


}

about:blank 28/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: The add() method is overloaded three times in the MathOperations class. Different number of parameters (int a, int b) versus (int a, int b, int c) and different
types of parameters (int versus double). In Java, overloading is based on the method signature, which includes the number and types of parameters. It does not depend on
the return type. The correct method is selected at compile time based on the arguments passed to the add() method. This is an example of compile-time polymorphism (or
static polymorphism).

Using compile-time polymorphism

Description Example

class MathOperations {

Create a class MathOperations that contains multiple methods for performing


addition.

int add(int a, int b) {

Include an add method that accepts two int values (a and b).

return a + b;

Add the values of a and b and return the sum to the calling method as an int.

Close curly braces to end the method.

int add(double a, double b) {

Include an add method that accepts two double values (a and b).

return a + b;

Add the values of a and b and return the sum to the calling method as a double.
This method overloads both of the previous add() methods, but it works with
double values instead of int.

about:blank 29/79
4/11/25, 6:50 PM about:blank

Description Example

Close curly braces to end the method.

int add(int a, int b, int c) {

Include an add method that accepts three int values (a, b, and c).

return a + b + c;

Add the values of a, b, and c and return the sum to the calling method as an
int. This method overloads the first add() method because it has different
number of parameters.

}
}

Close curly braces to end the method and the MathOperations class definition.

public class Main {

A Java class named Main with a main method. The main method is the entry
point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args).
This method is required for execution in Java programs.

MathOperations math = new MathOperations();

Create an instance of the MathOperations class and assign it to the math object.

Calls the method add(int a, int b) to add two integers (2 + 3) and print the System.out.println("Sum of 2 and 3: " + math.add(2, 3));
result to the console.

about:blank 30/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println("Sum of 2.5 and 3.5: " + math.add(2.5, 3.5));

Calls the method add(double a, double b) to add two double values (2.5 +
3.5) and print the result to the console.

System.out.println("Sum of 1, 2 and 3: " + math.add(2, 3, 4));

Calls the method add(int a, int b, int c) to add three integers (2 + 3 + 4)


and print the result to the console.

}
}

Close curly braces to end the Main class definition.

Explanation: In this example, the MathOperations class has three overloaded add methods. Depending on the number and type of arguments passed to add, Java
determines which method to invoke at compile time. This makes our code more flexible and easier to read.

Using runtime polymorphism

Description Example

class Animal {

Create a superclass named Animal, which serves as a base class for other classes that might inherit
from it.

void sound() {

Include a sound() method. This method is meant to be overridden by subclasses that define more
specific behavors.

System.out.println("Animal makes a sound");

Print the message "Animal makes a sound" to the console using the System.out.println() function.

Close curly braces to end the Animal class definition. }


}

about:blank 31/79
4/11/25, 6:50 PM about:blank

Description Example

Description Example

class Dog extends Animal {

The Dog class inherits from the Animal class.

@Override

Dog overrides the sound() method to provide a specific implementation: "Dog barks". The @Override annotation
tells the compiler that this method replaces the sound() method from Animal.

void sound() {

Include a sound() method to print the message "Dog barks".

System.out.println("Dog barks");

Print the message to the console using the System.out.println() function.

}
}

Close curly braces to end the Dog class definition.

Description Example

class Cat extends Animal {

The Cat class inherits from the Animal class.

@Override

Cat overrides the sound() method to provide a specific implementation: "Cat meows". The @Override annotation
tells the compiler that this method replaces the sound() method from Animal.

about:blank 32/79
4/11/25, 6:50 PM about:blank

Description Example

void sound() {

Include a sound() method to print the message "Cat meows".

System.out.println("Cat meows");

Print the message to the console using the System.out.println() function.

}
}

Close curly braces to end the Cat class definition.

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Animal myAnimal = new Animal();

Creates an instance of Animal and stores it in a variable myAnimal.

myAnimal = new Dog();

The Dog object is stored in an Animal reference. Since Dog overrides the sound() method, Java uses dynamic
method dispatch to call the overridden method in Dog, not in Animal.

Since myAnimal is a regular Animal object, calling myAnimal.sound() executes the sound() method from the myAnimal.sound();
Animal class.

about:blank 33/79
4/11/25, 6:50 PM about:blank

Description Example

myAnimal = new Cat();

The Cat object is stored in an Animal reference. Since Cat overrides the sound() method, Java uses dynamic
method dispatch to call the overridden method in Cat, not in Animal.

myAnimal.sound();

Since myAnimal is a regular Animal object, calling myAnimal.sound() executes the sound() method from the
Animal class.

}
}

Close curly braces to end the Main class definition.

Explanation: In this example, Animal is a superclass with a method called sound(). Both Dog and Cat classes extend Animal, providing their own implementation of the
sound() method. When we create an Animal reference and assign it to different subclasses (Dog and Cat), the appropriate sound() method is called at runtime based on the
object type. This allows for more dynamic and flexible code.

Creating virtual methods

Description Example

class Animal {

Create a superclass named Animal, which serves as a base class for other classes that might inherit
from it.

void sound() {

Include a sound() method. This method is meant to be overridden by subclasses that define more
specific behavors.

System.out.println("Animal makes a sound");

Print the message "Animal makes a sound" to the console using the System.out.println() function.

Close curly braces to end the Animal class definition. }


}

about:blank 34/79
4/11/25, 6:50 PM about:blank

Description Example

Description Example

class Dog extends Animal {

The Dog class inherits from the Animal class.

@Override

Dog overrides the sound() method to provide a specific implementation: "Dog barks". The @Override annotation
tells the compiler that this method replaces the sound() method from Animal.

void sound() {

Include a sound() method to print the message "Dog barks".

System.out.println("Dog barks");

Print the message to the console using the System.out.println() function.

}
}

Close curly braces to end the Dog class definition.

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

The main method is declared using public static void main(String[] args). This method is required for public static void main(String[] args) {
execution in Java programs.

about:blank 35/79
4/11/25, 6:50 PM about:blank

Description Example

Animal myAnimal = new Dog();

Creates an instance of Animal and stores it in a variable myAnimal.

myAnimal.sound();

Since myAnimal is a regular Animal object, calling myAnimal.sound() executes the sound() method from the
Animal class.

}
}

Close curly braces to end the Main class definition.

Explanation: In this example, even though myAnimal is an Animal, the sound() method from the Dog class is called, demonstrating virtual method behavior.

Designing interfaces and Abstract Classes in Java


Creating an interface

Description Example

interface Animal {

Declare an Animal interface.

void sound();

Include a method sound(). Any class that implements this interface must provide an implementation of sound().

Close curly braces to end the interface definition.

Description Example

Create a Dog class that implements the Animal interface. class Dog implements Animal {

about:blank 36/79
4/11/25, 6:50 PM about:blank

Description Example

public void sound() {

Include a sound() method for the class.

System.out.println("Bark");

Calling sound() prints "Bark" to the console using the System.out.println() function.

}
}

Close curly braces to end the Dog class definition.

Description Example

class Cat implements Animal {

Create a Cat class that implements the Animal interface.

public void sound() {

Include a sound() method for the class.

System.out.println("Meow");

Calling sound() prints "Meow" to the console using the System.out.println() function.

about:blank 37/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the Cat class definition.

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Animal dog = new Dog();

Create the Dog object and assign it to the variable dog.

Animal cat = new Cat();

Create the Cat object and assign it to the variable cat.

dog.sound();

Call sound() on the dog object. This prints the message "Bark".

cat.sound();

Call sound() on the cat object. This prints the message "Meow".

Close curly braces to end the Main class definition. }


}

about:blank 38/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: In this example, we define an interface Animal with a method sound(). The Dog and Cat classes implement the Animal interface and provide their own
versions of the sound() method. In the Main class, we create instances of Dog and Cat, calling the sound() method on each to demonstrate polymorphism.

Creating an abstract class

Description Example

abstract class Shape {

Create an abstract class Shape that cannot be instantiated directly.

abstract void draw();

Include an abstract method draw() that must be implemented by any subclass.

void display() {

Include a concrete method display() that has a default implementation.

System.out.println("This is a shape.");

Calling the display() method prints "This is a shape." to the console using the System.out.println()
function.

}
}

Close curly braces to end the Dog class definition.

Description Example

class Circle extends Shape {

Create a Circle class that extends the Shape class.

Include a draw() method for the class. public void draw() {

about:blank 39/79
4/11/25, 6:50 PM about:blank

Description Example

System.out.println("Drawing Circle");

Calling the draw() method prints "Drawing Circle" to the console using the System.out.println() function.

}
}

Close curly braces to end the Dog class definition.

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This method is required for
execution in Java programs.

Shape shape = new Circle();

The shape object is instantiated from the Shape class but it refers to a Circle object.

shape.draw();

Calling draw() on the shape object prints "Drawing Circle".

Calling display() on the shape object prints "This is a shape." shape.display();

about:blank 40/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the Main class definition.

Explanation: In this example, we define an abstract class Shape with an abstract method draw() and a concrete method display(). The Circle class extends the Shape
class and provides an implementation for the draw() method. In the Main class, we create an instance of Circle using the Shape reference type to show how it works. The
draw() method executes the overridden version from Circle. The display() method is inherited from Shape and is called as is.

Inner classes in Java


Creating inner classes

Description Example

class OuterClass {

Create an OuterClass that works as a container for the inner class.

int outerVariable = 10;

Set the value of the int outerVariable to 10.

class InnerClass {

Create a classs InnerClass inside the OuterClass.

void display();

Include a method display() that accesses OuterVariable from the outer class.
Inner classes have direct access to the outer class's members (including private
ones).

System.out.println("Outer variable value: " + outerVariable);

Calling the display() method prints the outerVariable value to the console
using the System.out.println() function. The outerVariable value is
generated dynamically.

Close curly braces to end the OuterClass class definition. }


}
}

about:blank 41/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: In this example, OuterClass contains a variable outerVariable. InnerClass is defined inside OuterClass and has a method display(). This method can
access outerVariable directly.

Using inner classes

Description Example

public class Main {

A Java class named Main with a main method. The main method is the entry point of the
program.

public static void main(String[] args) {

The main method is declared using public static void main(String[] args). This
method is required for execution in Java programs.

OuterClass outer = new OuterClass();

Create an instance of the OuterClass. This is necessary because non-static inner classes
require an instance of the outer class to be created first.

OuterClass.InnerClass inner = outer.new InnerClass();

Create a classs InnerClass inside the OuterClass. Since InnerClass is a non-static inner
class, it must be created using an instance of OuterClass.

inner.display();

Call the display() method inside InnerClass.

}
}

Close curly braces to end the Main class definition.

about:blank 42/79
4/11/25, 6:50 PM about:blank
Explanation: In this example, InnerClass is nested inside OuterClass and has access to all outer class's members. The display() method will print the value of the
outerVariable. The code demonstrates encapsulation in Java.

Creating a static nested classes

Description Example

class OuterClass {

Create an OuterClass that works as a container for the inner class.

static int staticVariable = 20;

Set the value of the int outerVariable to 20.

static class StaticNestedClass {

Create a classs InnerClass inside the OuterClass.

void show();

Include a method show() that accesses OuterVariable from the outer class.
Inner classes have direct access to the outer class's members (including
private ones).

System.out.println("Static variable value: " + staticVariable);

Calling the show() method prints the outerVariable value to the console
using the System.out.println() function. The outerVariable value is
generated dynamically.

}
}
}

Close curly braces to end the OuterClass class definition.

Explanation: In this example, OuterClass contains a static variable named staticVariable with a value of 20. Since the variable is static, it belongs to the class itself
rather than an instance. Static nested classes do not require an instance of the outer class. It can access staticVariable without an instance of OuterClass. The nested class
keeps related logic inside OuterClass, improving organization.

Using a static nested classes

about:blank 43/79
4/11/25, 6:50 PM about:blank

Description Example

public class Main {

A Java class named Main with a main method. The main method is
the entry point of the program.

public static void main(String[] args) {

The main method is declared using public static void


main(String[] args). This method is required for execution in Java
programs.

OuterClass.StaticNestedClass nested = new OuterClass.StaticNestedClass();

Create an instance of StaticNestedClass inside the OuterClass.

nested.show();

Include a method nested.show() that prints the value of the


staticVariable from OuterClass.

}
}
}

Close curly braces to end the OuterClass class definition.

Creating a method-local inner class

Description Example

class OuterClass {
void myMethod() {

Create an OuterClass with a method myMethod() that will define and use a
method-local inner class.

Define a class MethodLocalInner inside myMethod(). MethodLocalInner is local to class MethodLocalInner {


the method, meaning that it cannot be accessed outside of myMethod(). Calling void display() {
System.out.println("Inside Method Local Inner Class");
MethodLocalInner prints the message "Inside Method Local Inner Class" to the
}
console using the System.out.println() function. }

about:blank 44/79
4/11/25, 6:50 PM about:blank

Description Example

MethodLocalInner inner = new MethodLocalInner();

The inner class is instantiated within the method where it is defined.

inner.display();

inner.display() calls the display() method, printing "Inside Method Local


Inner Class".

}
}

Close curly braces to end the OuterClass class definition.

Creating an anonymous inner class

Description Example

interface Greeting {
void greet();
}

The Greeting interface defines a single method greet(), which must be


implemented by any class that uses this interface.

public class Main {


public static void main(String[] args) {
Greeting greeting = new Greeting() {
public void greet() {
System.out.println("Hello from Anonymous Inner Class!");
}
};
This creates an anonymous inner class that implements the Greeting interface.
The anonymous class provides an implementation for the greet() method at
the moment of object creation.

greeting.greet();

This calls the overridden greet() method in the anonymous inner class,
printing "Hello from Anonymous Inner Class!".

Close curly braces to end the Main class definition. }


}

about:blank 45/79
4/11/25, 6:50 PM about:blank

Description Example

Using inner classes in the real world

Description Example

class Library {
private String libraryName;
public Library(String name) {
this.libraryName = name;
}

The Library class represents a library and has a private variable libraryName
to store its name. A constructor initializes libraryName.

class Book {
private String title;
private String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
public void displayBookInfo() {
System.out.println("Library: " + libraryName);
Nested inside Library, this class represents a book. It has two private System.out.println("Book Title: " + title);
attributes: title and author. The Book class has a constructor to initialize System.out.println("Author: " + author);
these attributes. The displayBookInfo() method prints the book's title and }
author. It also accesses libraryName from Library, demonstrating how inner }
classes can access private members of the outer class.

public class Main {


public static void main(String[] args) {
Library myLibrary = new Library("City Library");
Library.Book myBook = myLibrary.new Book("1984", "George Orwell");
myBook.displayBookInfo();
This creates a Library instance named "City Library" and creates a Book
instance associated with that library. Since Book is a non-static inner class, it
must be created using an instance of Library. The displayBookInfo()
method in the Book inner class prints out the name of the library along with
the book's title and author.

}
}

Close curly braces to end the Main class definition.

Java Collections Framework (JCF)


Using an ArrayList array

about:blank 46/79
4/11/25, 6:50 PM about:blank

Description Example

import java.util.ArrayList;
import java.util.List;

Import ArrayList and List from the java.util package to use dynamic lists.

public class ListExample {


public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Fruits: " + fruits);
Define a class ListExample that contains the Java main method. Create a List of type String String firstFruit = fruits.get(0);
using the ArrayList implementation. This list will store fruit names as string elements. Add System.out.println("First fruit: " + firstFruit);
elements "Apple", "Banana", and "Cherry" to the list. Print the entire list, showing its elements
in the order they were added. Retrieve the first element Apple from the list using index 0. Print
the retrieved element.

}
}

Close curly braces to end the ListExample class definition.

Explanation: This Java program demonstrates how to use the List interface with an ArrayList implementation to store and manipulate a list of fruit names. ArrayList is a
dynamic array-based implementation of List, allowing for flexible resizing. Elements are added in order and accessed using a zero-based index. The get(index) method
retrieves elements at specific positions.

Using a LinkedList array

Description Example

import java.util.LinkedList;

Import the LinkedList class from the java.util package to use a linked list.

public class LinkedListExample {


public static void main(String[] args) {
LinkedList<String> animals = new LinkedList<>();
animals.add("Dog");
animals.add("Cat");
animals.add("Elephant");
System.out.println("Animals: " + animals);
Define a class LinkedListExample that contains the Java main method. Create a LinkedList of
type String to store animal names. Add elements "Dog", "Cat", and "Elephant" to the list. Print
the contents of the LinkedList, displaying all elements.

Close curly braces to end the LinkedListExample class definition. }


}

about:blank 47/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This Java program demonstrates how to use a LinkedList to store and manipulate a list of animal names. LinkedList is a doubly linked list implementation
in Java, meaning that elements are linked using pointers. In LinkedList, insertions and deletions are faster compared to ArrayList (especially for large lists).

Using a HashSet collection

Description Example

import java.util.HashSet;

Import the HashSet class from the java.util package to store a collection of unique elements.

public class HashSetExample {


public static void main(String[] args) {
HashSet<String> colors = new HashSet<>();
colors.add("Red");
colors.add("Green");
animals.add("Blue");
colors.add("Red");
Define a class HashSetExample that contains the Java main method. Create a HashSet of type String to System.out.println("Colors: " + colors);
store color names. Add elements "Red", "Green", and "Blue" to the HashSet. Add "Red" again to the
HashSet. HashSet does not allow duplicate values. If a duplicate is added, it is ignored. Print the
contents of the HashSet, displaying all elements.

}
}

Close curly braces to end the HashSetExample class definition.

Explanation: This Java program demonstrates the usage of a HashSet, which is a part of the Java Collections Framework and is used to store a collection of unique
elements. HashSet does not maintain any specific order and ignores duplicates. It is useful when you need a collection of distinct elements with fast lookup times.

Using a HashMap collection

Description Example

import java.util.HashMap;

Import the HashMap class from the java.util package to store key-value pairs.

Define a class HashMapExample that contains the Java main method. Create a HashMap<String, public class HashMapExample {
Integer> named ageMap. The keys are names (String), and the values are ages (Integer). Add public static void main(String[] args) {
HashMap<String, Integer> ageMap = new HashMap<>();
key-value pair to the HashMap using the put() method. The System.out.println() statement ageMap.put("Alice", 30);
prints the entire HashMap but does not maintain any order because HashMap does not maintain ageMap.put("Bob", 25);
insertion order. The program retrieves Alice's age using ageMap.get("Alice") and stores it in ageMap.put("Charlie", 35);
aliceAge. System.out.println("Age Map: " + ageMap);
int aliceAge = ageMap.get("Alice");
System.out.println("Alice's Age: " + aliceAge);

about:blank 48/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the HashMapExample class definition.

Explanation: This Java program demonstrates the usage of a HashMap, which is a part of the Java Collections Framework and is used to store key-value pairs. Keys are
unique (if a duplicate key is added, it replaces the old value). Values can be duplicated. HashMap does not maintain any specific order. It provides fast access to values using
keys.

Working with lists


Creating an ArrayList

Description Example

import java.util.ArrayList;

Import ArrayList from the java.util package to use dynamic lists.

public class ArrayListExample {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
Define a class ArrayListExample that contains the Java main method. Create an fruits.add("Cherry");
System.out.println("First fruit: " + fruits.get(0));
ArrayList<String> named fruits to store a list of fruit names. This list will store fruit fruits.remove("Banana");
names as string elements. Add elements "Apple", "Banana", and "Cherry" to the list. Print System.out.println("Fruits List: " + fruits);
the entire list, showing its elements in the order they were added. Retrieve the first element
Apple from the list using index 0 and print the retrieved element. Call
fruits.remove("Banana") to remove "Banana" from the list. Print the remaining elements of
ArrayList.

}
}

Close the curly braces to end the ArrayListExample class definition.

Explanation: This Java program demonstrates the usage of an ArrayList, which is a part of the Java Collections Framework and is used to store a resizable list of
elements. ArrayList elements are added in order and accessed using a zero-based index. The get(index) method retrieves elements at specific positions. ArrayList allows
duplicates and removing elements shifts subsequent elements left (affecting performance for large lists).

Creating a LinkedList

Description Example

Import the LinkedList class from the java.util package to create a linked list. import java.util.LinkedList;

about:blank 49/79
4/11/25, 6:50 PM about:blank

Description Example

public class LinkedListExample {


public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Green");
animals.add("Blue");
System.out.println("First color: " + colors.get(0));
Define a class LinkedListExample that contains the Java main method. Create a LinkedList colors.remove("Green");
of type String to store a list of color names. Add elements "Red", "Green", and "Blue" to System.out.println("Colors List: " + colors);
the list using the add() method. Retrieve the first element of the list using colors.get(0).
Remove the first occurrence of "Green" from the list using colors.remove("Green"). Print
the remaining elements of the LinkedList.

}
}

Close the curly braces to end the LinkedListExample class definition.

Explanation: This Java program demonstrates the usage of a LinkedList, which is a part of the Java Collections Framework. LinkedList stores elements in nodes, where
each node contains a reference to the next node. It allows efficient insertion and removal of elements from both ends: addFirst(), addLast(), removeFirst(), and
removeLast(). Accessing elements by index get(index) is slower than in ArrayList, because it requires traversing the list from the beginning. Duplicates are allowed, and
order is maintained. Unlike ArrayList, elements are not shifted after removal (only the references are updated), which can improve performance for certain operations.

HashSet and TreeSet


Creating a HashSet

Description Example

import java.util.HashSet;

Import the HashSet class from the java.util package to store a collection of unique
elements.

public class HashSetExample {


public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Banana");
Define a class HashSetExample that contains the Java main method. Create a HashSet of System.out.println("Fruits in the HashSet: " + fruits);
if (fruits.contains("Apple")) {
type String to store fruit names. Add elements "Apple", "Banana", and "Cherry" to the System.out.println("Apple is present in the set.");
HashSet. Add "Banana" again to the HashSet. Since HashSet does not allow duplicate }
values, it is ignored. Print the contents of the HashSet, displaying all elements. Checks if fruits.remove("Cherry");
"Apple" is in the set by calling fruits.contains("Apple"). If found, the message System.out.println("After removal: " + fruits);
"Apple" is present in the set is printed. The method fruits.remove("Cherry") removes
"Cherry" from the set.

about:blank 50/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the HashSetExample class definition.

Explanation: This Java program demonstrates the usage of a HashSet, which is a part of the Java Collections Framework and is used to store a collection of unique
elements. The contains() method provides fast lookup to check if an element exists. The remove() method efficiently removes elements. HashSet does not maintain any
specific order and ignores duplicates. It is useful when you need a collection of distinct elements with fast lookup times.

Creating a TreeSet

Description Example

import java.util.TreeSet;

Import the TreeSet class from the java.util package to store a collection of unique
elements.

public class TreeSetExample {


public static void main(String[] args) {
TreeSet<Integer> numbers = new TreeSet<>();
numbers.add(5);
numbers.add(3);
numbers.add(8);
numbers.add(1);
numbers.add(3);
Define a class TreeSetExample that contains the Java main method. Create a System.out.println("Numbers in the TreeSet: " + numbers););
TreeSet<Integer> named numbers to store a set of integer values. Add the numbers if (numbers.contains(5)) {
5, 3, 8, and 1 using the add() method. Add 3 again to the TreeSet. Since TreeSet System.out.println("5 is present in the set.");
}
does not allow duplicate values, it is ignored. Print the contents of the TreeSet,
numbers.remove(8);
displaying all elements. Checks if 5 is in the set by calling numbers.contains(5). If System.out.println("After removal: " + numbers);
found, the message "5 is present in the set" is printed. The method
numbers.remove(8) removes 8 from the set.

}
}

Close curly braces to end the HashSetExample class definition.

Explanation: This Java program demonstrates the usage of a TreeSet, which is used to store a collection of unique elements. TreeSet elements are always sorted in
ascending order. The contains() method provides fast lookup (uses a Balanced Tree structure). The remove() methokd efficiently deletes elements while maintaining
order. TreeSet is useful when you need a sorted set with fast operations.

TreeSet versus HashSet: need for order

Description Example

Use TreeSet: When you need the elements to be sorted in a specific order. For example: If you want to store a TreeSet<Integer> grades = new TreeSet<>();
list of student grades and display them in ascending order, a TreeSet will automatically sort them. grades.add(85);
grades.add(90);
grades.add(70);
// Output: [70, 85, 90]
System.out.println(grades);

about:blank 51/79
4/11/25, 6:50 PM about:blank

Description Example

HashSet<String> userIds = new HashSet<>();


userIds.add("user1");
userIds.add("user2");
userIds.add("user3");
// Output: Order may vary
System.out.println(userIds);

Use HashSet: When the order of elements does not matter. For example: If you are storing unique user IDs and
do not care about their order.

HashSet versus TreeSet: Need for performance

Description Example

HashSet<String> collectedItems = new HashSet<>();


collectedItems.add("Sword");
collectedItems.add("Shield");
<boolean>boolean hasSword = collectedItems.contains("Sword"); // Fast check

Use HashSet: For faster performance when adding, removing, or searching


for elements. For Example: In a game, if you need to quickly check if a
player has collected a unique item.

TreeSet<Integer> scores = new TreeSet<>();


scores.add(300);
scores.add(150);
scores.add(200);
System.out.println(scores); // [150, 200, 300]

Use TreeSet: When you can afford slower operations but need the elements
sorted. For Example: If you are maintaining a leaderboard that requires
sorted scores, a TreeSet is suitable even if it's slightly slower.

HashSet versus TreeSet: Avoidance of duplicates

Description Example

HashSet<String> fruits = new HashSet<>();


fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicate will not be added
System.out.println(fruits); // Output: [Banana, Apple] </String>
Using HashSet to avoid duplicates. A HashSet<String> named fruits is created.
"Apple" and "Banana" are added. A duplicate "Apple" is added but ignored because
HashSet does not allow duplicates. The output may appear as [Banana, Apple] or
[Apple, Banana], but the order is NOT guaranteed, since HashSet is unordered.

Using TreeSet to avoid duplicates. A TreeSet<String> named sortedFruits is created. TreeSet<String> sortedFruits = new TreeSet<>();
"Apple" and "Banana" are added. A duplicate "Apple" is added but ignored because sortedFruits.add("Apple");
sortedFruits.add("Banana");
TreeSet also does not allow duplicates. Unlike HashSet, TreeSet automatically sorts
sortedFruits.add("Apple"); // Duplicate will not be added
elements in ascending order. The output is always [Apple, Banana], since TreeSet System.out.println(sortedFruits); // Output: [Apple, Banana]
maintains sorted order.

about:blank 52/79
4/11/25, 6:50 PM about:blank

Description Example

Implementing queues in Java


Creating a simple queue using LinkedList

Description Example

import java.util.LinkedList;
import java.util.Queue;

Import the java.util.LinkedList and java.util.Queue packages to use the Queue interface
with a LinkedList implementation.

public class QueueExample {


public static void main(String[] args) {
// Creating a Queue
Queue<String> queue = new LinkedList<>();
// Enqueue operation
queue.offer("Apple");
queue.offer("Banana");
queue.offer("Cherry");
// Displaying the Queue
System.out.println("Queue: " + queue);
Create an instance of Queue<String> named queue using new LinkedList<>(). Add three
// Dequeue operation
elements ("Apple", "Banana", "Cherry") to the queue using offer(), which inserts String removedItem = queue.poll();
elements at the end of the queue. Print the queue to show its contents. The poll() method System.out.println("Removed Item: " + removedItem);
removes and returns the front element ("Apple") from the queue. Print the removed // Displaying the Queue after Dequeue
element ("Apple") and display the state of the queue again after removing the front System.out.println("Queue after dequeue: " + queue);
element.

}
}

Close curly braces to end the QueueExample class definition.

Explanation: This Java program demonstrates the use of a Queue data structure using the LinkedList class. The method offer() adds an element to the queue (enqueue),
poll() removes and returns the front element (dequeue). LinkedList as a queue implements FIFO (First-In-First-Out) behavior.

Creating a priority queue

Description Example

import java.util.PriorityQueue;

Import the java.util.PriorityQueue package to use the PriorityQueue class.

about:blank 53/79
4/11/25, 6:50 PM about:blank

Description Example

public class PriorityQueueExample {


public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
// Adding elements
priorityQueue.offer(20);
priorityQueue.offer(15);
priorityQueue.offer(30);
Create an instance of PriorityQueue<Integer> named priorityQueue using // Displaying the Priority Queue
System.out.println("Priority Queue: " + priorityQueue);
new PriorityQueue<>(). Add three elements: 20, 15, and 30 using the offer() // Removing elements in priority order
method. The PriorityQueue maintains a min-heap structure (smallest element while (!priorityQueue.isEmpty()) {
has the highest priority). Print the queue; its order may not be in the exact System.out.println("Removed Item: " + priorityQueue.poll());
insertion order due to the heap-based priority structure. Remove elements in }
priority order (ascending order for integers). A while loop continuously
removes and prints the smallest element until the queue is empty.

}
}

Close curly braces to end the PriorityQueueExample class definition.

Explanation: This Java program demonstrates the usage of a PriorityQueue, which is a type of queue where elements are processed based on their priority (natural order
by default for numbers). The method offer() adds an element to the queue (enqueue), poll() removes and returns the element with the highest priority (smallest number
in this case). Heap-based Implementation ensures efficient insertions and deletions.

Implementing a queue in the real world

Description Example

import java.util.LinkedList;
import java.util.Queue;

Import the java.util.LinkedList and java.util.Queue packages to


create and manage the queue.

public class CustomerServiceQueue {


public static void main(String[] args) {
// Creating a queue to represent customers waiting for service
Queue<String> customerQueue = new LinkedList<>();
// Customers arrive and join the queue
customerQueue.offer("Customer 1");
customerQueue.offer("Customer 2");
customerQueue.offer("Customer 3");
// Displaying the current queue
System.out.println("Current Customer Queue: " + customerQueue);
Create an instance of Queue<String> named customerQueue using // Serving the first customer in the queue
String servedCustomer = customerQueue.poll();
new LinkedList<>() to store customers. Add "Customer 1",
System.out.println("Serving: " + servedCustomer);
"Customer 2", and "Customer 3" are added to the queue using // Displaying the queue after serving one customer
offer(). Prints the queue to show the customers waiting in order. System.out.println("Customer Queue after serving one: " + customerQueue);
The poll() method removes and returns the first customer // Serving another customer
("Customer 1") from the queue. Display the remaining customers in servedCustomer = customerQueue.poll();
System.out.println("Serving: " + servedCustomer);
the queue. Call poll() again to serve the next customer and print the
// Final state of the queue
final state of the queue. System.out.println("Final Customer Queue: " + customerQueue);

Close curly braces to end the CustomerServiceQueue class definition. }


}

about:blank 54/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This Java program simulates a customer service queue using a Queue (FIFO - First In, First Out) implemented with a LinkedList. It models how customers
arrive, wait, and are served in order. The method offer() adds customers to the queue and poll() removes customers in FIFO order. LinkedList as a queue mimics a real
world waiting line. This approach can be extended to simulate bank queues, call centers, or ticket counters.

Using HashMap and TreeMap


Creating a HashMap

Description Example

import java.util.HashMap;

Import the HashMap class from the java.util package, which is a


part of Java's Collection Framework.

public class HashMapExample {


public static void main(String[] args) {
// Creating a HashMap
HashMap<String, Integer> map = new HashMap<>();
// Adding key-value pairs to the HashMap
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
Initialize a HashMap<String, Integer> named map to represent // Accessing values
fruit names as keys and their corresponding numeric values System.out.println("Value for key 'Apple': " + map.get("Apple")); // Output: 1
as values. Add key-value pairs using the put method. "Apple" // Iterating through the HashMap
for (String key : map.keySet()) {
is mapped to 1, "Banana" to 2, and "Cherry" to 3. Keys are
System.out.println(key + ": " + map.get(key));
unique: If the same key is added again, its value gets updated. }
The map.get("Apple") method fetches and prints the value // Checking if a key exists
associated with "Apple". The keySet() method returns all the if (map.containsKey("Banana")) {
keys in the HashMap, and the for loop prints each key-value System.out.println("Banana exists in the map.");
}
pair. Order is NOT guaranteed in a HashMap. The
// Removing a key-value pair
containsKey() method checks whether "Banana" is present in map.remove("Cherry");
the map and the remove() method deletes "Cherry" from the
HashMap.

}
}

Close curly braces to end the TreeMapExample class definition.

Explanation: This Java program demonstrates the usage of a HashMap, a data structure that stores key-value pairs and allows fast access to values using keys. put(K key,
V value) adds or updates a key-value pair, get(K key) retrieves the value for a key, keySet() returns all keys, containsKey(K key) checks if a key exists, and remove(K
key) deletes a key-value pair.

Using a HashMap

about:blank 55/79
4/11/25, 6:50 PM about:blank

Description Example

HashMap<String, Integer> wordCount = new HashMap<>();


String text = "apple banana apple orange banana apple";
String[] words = text.split(" ");

Initialize a HashMap<String, Integer> named wordCount, where the keys are words for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
(Strings) and the values are the count of occurrences of each word (Integers). Define the }
input text containing a string with multiple repeated words. The split() method splits the
text string into a words array based on spaces. A for loop iterates over each word in the
words array. The wordCount.getOrDefault(word, 0) method retrieves the current count of
the word if it exists. If the word is not yet in the map, it defaults to 0. The +1 increments
the count for each occurrence and put(word, newCount) updates the count in the HashMap.

Explanation: This Java code snippet demonstrates how to use a HashMap to count the occurrences of words in a given text string. This approach is useful for word
frequency analysis in text processing. The split(" ") function splits text into words. HashMap efficiently tracks word occurrences. getOrDefault(key, defaultValue)
avoids null values.

Creating a TreeMap

Description Example

import java.util.TreeMap;

Import the TreeMap class from the java.util package to


store key-value pairs in sorted order.

public class TreeMapExample {


public static void main(String[] args) {
// Creating a TreeMap
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding key-value pairs to the TreeMap
treeMap.put("Banana", 2);
treeMap.put("Apple", 1);
treeMap.put("Cherry", 3);
// Accessing values
Initialize a TreeMap<String, Integer> named treeMap to System.out.println("Value for key 'Apple': " + treeMap.get("Apple")); // Output: 1
store fruit names (keys) and their corresponding values
(integers). The TreeMap automatically sorts the keys in // Iterating through the TreeMap
for (String key : treeMap.keySet()) {
ascending order (Apple → Banana → Cherry). The System.out.println(key + ": " + treeMap.get(key));
treeMap.get("Apple")) call fetches and prints the value }
associated with "Apple". The for loop calls the keySet() // Checking if a key exists
method to iterate over all keys (which are sorted) and print if (treeMap.containsKey("Cherry")) {
their associated values. The containsKey() method checks System.out.println("Cherry exists in the TreeMap.");
}
if "Cherry" is present and prints a message. The // Removing a key-value pair
treemap.remove() method removes the "Banana" entry treeMap.remove("Banana");
from the TreeMap.

}
}

Close curly braces to end the TreeMapExample class


definition.

Explanation: This Java program demonstrates the use of a TreeMap, a data structure that stores key-value pairs in sorted order based on keys. TreeMap maintains sorted
order (ascending by default).

Using a TreeMap

about:blank 56/79
4/11/25, 6:50 PM about:blank

Description Example

TreeMap<String, Integer> leaderboard = new TreeMap<>();


leaderboard.put("Alice", 150);
leaderboard.put("Bob", 200);
leaderboard.put("Charlie", 100);
// Displaying sorted leaderboard
for (String player : leaderboard.keySet()) {
Initialize a TreeMap<String, Integer> named leaderboard where Keys (String) System.out.println(player + ": " + leaderboard.get(player));
represent player names and Values (Integer) represent player scores. TreeMap }
automatically sorts keys in ascending order. Add three players and their scores to the
TreeMap. Since TreeMap maintains sorted order by key (name), the stored order will be:
Alice → Bob → Charlie. Display the sorted leaderboard using the keySet() method.

Explanation: This Java code snippet demonstrates the use of a TreeMap to store and display a sorted leaderboard of players and their scores. TreeMap stores entries in key-
sorted order (ascending). put(K key, V value) adds key-value pairs, get(K key) retrieves the value for a given key, and keySet() returns keys in sorted order.

Using Java collections in the real world


Managing books in a library management system

Description Example

import java.util.ArrayList;
public class Library {
private ArrayList<String> books;

public Library() {
books = new ArrayList<>();
}

public void addBook(String book) {


books.add(book);
}

Import the ArrayList class from the java.util package, which is a part of Java's Collection public void displayBooks() {
System.out.println("Books in the Library:");
Framework and is used to store a dynamic list. Create the Library class to represent a collection of
for (String book : books) {
books. The books variable is a private ArrayList<String>, meaning it stores book titles as strings and System.out.println(book);
it cannot be accessed directly from outside the class. The Library() constructor initializes the books }
list when a Library object is created. The addBook() method adds a new book to the books list. The }
displayBooks() method prints all books stored in the books list using a for-each loop. The main
public static void main(String[] args) {
method creates a Library object named myLibrary, adds two books: "The Great Gatsby" and "To Kill
Library myLibrary = new Library();
a Mockingbird", and calls the displayBooks() method to print the book list. myLibrary.addBook("The Great Gatsby");
myLibrary.addBook("To Kill a Mockingbird");
myLibrary.displayBooks();

}
}

Close curly braces to end the main and Library class definition.

Managing customer orders in an e-commerce application

Description Example

Import the HashMap class from the java.util import java.util.HashMap;


package, which is a part of Java's Collection
public class OrderManagement {
Framework and is used to store a dynamic list. private HashMap<Integer, String> orders;
Create the OrderManagement class to manage
orders. The orders variable is private, meaning public OrderManagement() {

about:blank 57/79
4/11/25, 6:50 PM about:blank

Description Example
it cannot be accessed directly from outside the orders = new HashMap<>();
class. It is encapsulated to ensure data integrity. }
The Java constructor OrderManagement() public void addOrder(int orderId, String customerName) {
initializes the orders HashMap when an orders.put(orderId, customerName);
OrderManagement object is created. The }
addOrder() method adds a new order using the
.put(orderId, customerName) method. If the public void displayOrders() {
System.out.println("Customer Orders:");
same orderId is added again, it overwrites the for (int orderId : orders.keySet()) {
previous entry. The displayOrders() method System.out.println("Order ID: " + orderId + ", Customer Name: " + orders.get(orderId));
iterates over the HashMap using keySet() to get }
all order IDs, retrieves and prints the }
corresponding customer names. The main
public static void main(String[] args) {
method creates an instance of OrderManagement, OrderManagement orderManagement = new OrderManagement();
adds two orders: Order #101 for Alice and Order orderManagement.addOrder(101, "Alice");
#102 for Bob, and calls the displayOrders() orderManagement.addOrder(102, "Bob");
method to show all orders. orderManagement.displayOrders();

}
}

Close curly braces to end the main and


OrderManagement class definition.

Explanation: This Java program implements a basic Order Management system using a HashMap to store and manage customer orders. The program uses
HashMap<Integer, String>, which stores Keys (Integer) to represent Order IDs and Values (String) to represent Customer Names.

Managing employee information in an employee management system

Description Example

import java.util.HashSet;

public class EmployeeManager {


private HashSet<String> employees;

public EmployeeManager() {
employees = new HashSet<>();
}

public void addEmployee(String employee) {


Import the HashSet class from the java.util package, which is a part of Java's employees.add(employee);
}
Collection Framework and is used to store a dynamic list. Create the
EmployeeManager class with a private variable named employee that stores public void displayEmployees() {
employee names. Encapsulation ensures the set is only modified through class System.out.println("Employees in the Company:");
methods. The constructor EmployeeManager() initializes the employees set when for (String employee : employees) {
an EmployeeManager object is created. The addEmployee() method adds an System.out.println(employee);
}
employee name to the HashSet. If the employee already exists, the HashSet
}
prevents duplicate entries. The displayEmployees() method iterates over the
HashSet to display all employees. The order is not guaranteed because HashSet public static void main(String[] args) {
does not maintain insertion order. The Java main method creates an instance of EmployeeManager manager = new EmployeeManager();
EmployeeManager and adds three employees: "John Doe", "Jane Smith", and manager.addEmployee("John Doe");
manager.addEmployee("Jane Smith");
"John Doe". Because "John Doe" is a duplicate, it is ignored by HashSet.
manager.addEmployee("John Doe"); // Duplicate will not be added
Calling displayEmployees() shows all employees. manager.displayEmployees();

Close curly braces to end the main and EmployeeManager class definition. }
}

about:blank 58/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This Java program implements a basic Employee Management system using a HashSet to store and manage employee names. It uses LinkedHashSet to
maintain insertion order and TreeSet to store employees in sorted order.

Managing tasks in a task management system

Description Example

import java.util.LinkedList;

public class TaskManager {


private LinkedList<String> tasks;

public TaskManager() {
tasks = new LinkedList<>();
}

public void addTask(String task) {


tasks.add(task);
}

public void completeTask() {


if (!tasks.isEmpty()) {
String completedTask = tasks.removeFirst();
Import the LinkedList class from the java.util package, which is a part of Java's System.out.println("Completed Task: " + completedTask);
} else {
Collection Framework and is used to store a dynamic list. Create the TaskManager System.out.println("No tasks to complete.");
class with a private variable named tasks that stores tasks. Encapsulation ensures }
the set is only modified through class methods. The constructor TaskManager() }
initializes the tasks list when a TaskManager object is created. The addTask()
method adds a task to the end of the list using add() and preserves the insertion public void displayTasks() {
System.out.println("Current Tasks:");
order (LinkedList maintains order). The completeTask() method removes the first for (String task : tasks) {
task using removeFirst(), prevents errors by checking isEmpty() before removal, System.out.println(task);
and prints the completed task. The displayTasks() method iterates over the }
LinkedList and prints all tasks. Tasks remain ordered by insertion. The Java main }
method creates an instance of TaskManager, adds two tasks: "Finish report" and
public static void main(String[] args) {
"Email client", displays tasks, completes the first task, and displays remaining tasks. TaskManager manager = new TaskManager();
manager.addTask("Finish report");
manager.addTask("Email client");
manager.displayTasks();

manager.completeTask();
manager.displayTasks();

}
}

Close curly braces to end the main and TaskManager class definition.

Explanation: This Java program implements a simple Task Manager using a LinkedList to store and manage tasks. It supports fast insertions/removals at both ends for
addFirst() and removeFirst().

Managing followers in a social media application

Description Example

Import the HashSet class from the java.util package, which is a part of Java's Collection import java.util.HashSet;
Framework and is used to store a dynamic list. Create the SocialMedia class with a HashMap
public class SocialMedia {
where Key (String) represents a user and Value (HashSet<String>) stores a set of followers private HashMap<String, HashSet<String>> userFollowers;
(ensuring uniqueness). The constructor SocialMedia() initializes userFollowers as an empty

about:blank 59/79
4/11/25, 6:50 PM about:blank

Description Example
HashMap. The addFoower() method ensures the user exists in the HashMap using the public SocialMedia() {
putIfAbsent(user, new HashSet<>()) method and adds the follower to the user's HashSet userFollowers = new HashMap<>();
}
(no duplicates allowed). The displayFollowers() method checks if the user exists, prints all
followers of the user, and handles missing users by displaying "No followers found". The public void addFollower(String user, String follower) {
Java main method creates an instance of SocialMedia class, adds followers: "Bob" follows userFollowers.putIfAbsent(user, new HashSet<>());
"Alice", "Charlie" follows "Alice", and displays Alice's followers. userFollowers.get(user).add(follower);
}

public void displayFollowers(String user) {


System.out.println("Followers of " + user + ":");
HashSet<String> followers = userFollowers.get(user);
if (followers != null) {
for (String follower : followers) {
System.out.println(follower);
}
} else {
System.out.println("No followers found.");
}
}

public static void main(String[] args) {


SocialMedia socialMedia = new SocialMedia();
socialMedia.addFollower("Alice", "Bob");
socialMedia.addFollower("Alice", "Charlie");
socialMedia.displayFollowers("Alice");

}
}

Close curly braces to end the main and EmployeeManager class definition.

Explanation: This Java program implements a basic social media follower system using HashMap and HashSet. HashSet ensures no user follows the same person twice. If a
user has no followers, it prints "No followers found". HashMap provides average time complexity for lookups. Followers cannot be accessed directly, only through class
methods.

Java File Handling / Working with File Input and Output Streams
Using the File class

Description Example

import java.io.File;

Import the File class, which provides methods for file and directory operations.

Define a class FileExample that contains the Java main method. Create a File object representing public class FileExample {
a file named example.txt. This does not create the actual file, just a reference to it. Call the public static void main(String[] args) {
File myFile = new File("example.txt");
exists() method on the File object to check whether the file physically exists in the specified
location. If the file exists, prints "File exists.", otherwise print "File does not exist.". // Check if the file exists
if (myFile.exists()) {
System.out.println("File exists.");
} else {
System.out.println("File does not exist.");
}

about:blank 60/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the FileExample class definition.

Explanation: This Java program demonstrates how to check whether a file exists in the filesystem using the File class from the java.io package.

Writing to Files

Description Example

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

Import the FileWriter class for writing character data to a file, the
BufferedWriter class that wraps FileWriter to provide efficient writing
operations, and the IOException class to handle input/output exceptions.

public class WriteToFile {


public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("output.txt");
BufferedWriter bufferedWriter = new BufferedWriter(writer);

bufferedWriter.write("Hello, World!");
bufferedWriter.newLine(); // Adds a new line
Define a class WriteToFile that contains the Java main method. Create a bufferedWriter.write("This is a Java file handling example.");
FileWriter class to write to the file "output.txt". A BufferedWriter is
wrapped around FileWriter for more efficient writing. Write text to the file bufferedWriter.close(); // Always close the writer
using the write() method. The newLine() method inserts a newline System.out.println("Data written to file successfully.");
} catch (IOException e) {
character (\n). The close() method closes the writer to ensure all data is System.out.println("An error occurred: " + e.getMessage());
flushed to the file. A confirmation message is printed to the console. The }
catch() call catches IOException if any file operation fails (for example,
permission issues, disk space) and prints an error message.

}
}

Close curly braces to end the WriteToFile class definition.

Explanation: This Java program demonstrates how to write text to a file using the FileWriter and BufferedWriter package. It writes multiple lines to the file, handles
exceptions properly, and closes the file to prevent resource leaks.

Reading from Files

Description Example

Import the FileReader class that reads character-based data from a file, the import java.io.BufferedReader;
BufferedReader class that provides efficient reading capabilities by buffering import java.io.FileReader;
import java.io.IOException;
input, and the IOException class to handle errors that may occur during file
operations.

about:blank 61/79
4/11/25, 6:50 PM about:blank

Description Example

public class ReadFromFile {


public static void main(String[] args) {
try {
FileReader reader = new FileReader("output.txt");
BufferedReader bufferedReader = new BufferedReader(reader);

String line;
while ((line = bufferedReader.readLine()) != null) {
Define a class ReadFromFile that contains the Java main method. Create a System.out.println(line);
FileReader class to read the file "output.txt". A BufferedReader is wrapped }
around FileReader for more efficient reading. Call readLine() reads one line at
a time from the file. The loop continues until readLine() returns null bufferedReader.close();
} catch (IOException e) {
(indicating the end of the file). Each line is printed to the console. The System.out.println("An error occurred: " + e.getMessage());
bufferedReader.close() method ensures the file resource is released after }
reading is complete. The catch() call catches IOException if any file operation
fails (for example, permission issues, disk space) and prints an error message.

}
}

Close curly braces to end the FileExample class definition.

Explanation: This Java program reads a file line by line using FileReader and BufferedReader and prints its content to the console. It reads and prints lines the file line by
line, handles exceptions properly, and closes the file to prevent resource leaks.

Using Java Byte Streams


Reading bytes

Description Example

import java.io.FileInputStream;
import java.io.IOException;

Import the FileInputStream class for reading raw byte data from a file and the
IOException class to handle input/output exceptions.

Define a class ReadBytes that contains the Java main method. Declare a public class ReadBytes {
FileInputStream variable, but don't initialize it. Open "example.txt" for reading. Read public static void main(String[] args) {
FileInputStream fileInputStream = null;
one byte at a time until the end of the file is reached. The method byteData() converts try {
the byte into a character and prints it. If an I/O error occurs, an error stack trace is // Create a FileInputStream to read from a file
printed. The finally block ensures the file stream is closed, preventing resource leaks. fileInputStream = new FileInputStream("example.txt");
The method fileInputStream.close() closes the file to free system resources.
// Variable to hold the byte data
int byteData;
// Read bytes until end of file
while ((byteData = fileInputStream.read()) != -1) {
// Print the byte data as characters
System.out.print((char) byteData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the stream to free resources
if (fileInputStream != null) {
try {
fileInputStream.close();

about:blank 62/79
4/11/25, 6:50 PM about:blank

Description Example
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

Close curly braces to end the FileExample class definition.

Explanation: This Java program reads a file byte by byte using FileInputStream and prints its contents to the console.

Writing bytes

Description Example

import java.io.FileOutputStream;
import java.io.IOException;

Import the FileOutputStream class for writing raw byte data to a file and the
IOException class to handle input/output exceptions.

public class WriteBytes {


public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
try {
// Create a FileOutputStream to write to a file
fileOutputStream = new FileOutputStream("output.txt");

// Data to write
String data = "Hello, World!";
// Convert the string to bytes
byte[] byteData = data.getBytes();

// Write bytes to the file


Define a class WriteBytes that contains the Java main method. Declare a fileOutputStream.write(byteData);
FileOutputStream variable but don't initialize it. Open a FileOutputStream for the file } catch (IOException e) {
"output.txt". If the file does not exist, create a new one. Define a String ("Hello, e.printStackTrace();
World!") to write to the file. Convert the string into a byte array using .getBytes(). } finally {
// Close the stream to free resources
Write the byte array to the file using fileOutputStream.write(byteData). The
if (fileOutputStream != null) {
IOException method catches and prints any exceptions during file writing. The try {
finally block ensures that the FileOutputStream is properly closed to free system fileOutputStream.close();
resources and uses a null check before calling .close(), preventing a } catch (IOException e) {
NullPointerException. If closing the stream fails, it prints the exception. e.printStackTrace();
}
}
}

Close curly braces to end the FileExample class definition. }


}

about:blank 63/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This Java program, writes the string "Hello, World!" to a file named output.txt using a FileOutputStream. It uses exception handling to catch possible file
operation errors and uses a finally block to ensure the file stream is always closed.

Byte streams example

Description Example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

Import the FileInputStream class for reading faw byte data from a file,
FileOutputStream class for writing raw byte data to a file, and the IOException
class to handle input/output exceptions.

public class FileCopy {


public static void main(String[] args) {
FileInputStream inputFile = null;
FileOutputStream outputFile = null;

try {
// Create FileInputStream to read from "source.txt"
inputFile = new FileInputStream("source.txt");
// Create FileOutputStream to write to "destination.txt"
outputFile = new FileOutputStream("destination.txt");

int byteData;
// Read bytes from source and write them to destination
while ((byteData = inputFile.read()) != -1) {
Decleare FileInputStream inputFile to reads data from source.txt and outputFile.write(byteData);
FileOutputStream outputFile to write data to destination.txt. The try block }
initializes inputFile to read from source.txt, initializes outputFile to write to
System.out.println("File copied successfully!");
destination.txt, reads bytes from source.txt one byte at a time using
inputFile.read(), writes each byte to destination.txt using } catch (IOException e) {
outputFile.write(byteData), continues until reaching the end of the file (-1), and e.printStackTrace();
prints "File copied successfully!" after completion. The catch block prints the stack } finally {
// Close both streams
trace if an IOException occurs (for example, file not found, read/write error). The
try {
finally bock ensures both inputFile and outputFile are closed to free system if (inputFile != null) inputFile.close();
resources. It uses null checks to prevent NullPointerException. if (outputFile != null) outputFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

Close curly braces to end the FileCopy class definition.

Explanation: This Java program copies the contents of a file named source.txt into another file named destination.txt using FileInputStream and FileOutputStream. It
reads and writes files one byte at a time and uses finally to always close file streams. The program catches IOException to prevent crashes.

Managing Directories in Java


Creating a directory
about:blank 64/79
4/11/25, 6:50 PM about:blank

Description Example

import java.io.File;

Import the java.io.File package to represent file and


directory paths.

public class CreateDirectory {


public static void main(String[] args) {
// Define the directory path
String directoryPath = "Projects/Java";
Define a class CreateDirectory that contains the Java
// Create a File object
main method. The String directoryPath = File directory = new File(directoryPath);
"Projects/Java" specifies the directory to be created.
This means that the program will try to create a folder // Create the directory
named "Java" inside a folder named "Projects". if (!directory.exists()) {
Create a File object for the directory by calling the boolean created = directory.mkdirs(); // Use mkdirs() to create nested directories
if (created) {
File(directoryPath) method. The File object System.out.println("Directory created successfully: " + directoryPath);
represents the directory but does not create it yet. The } else {
if (!directory.exists()) method ensures the System.out.println("Failed to create directory.");
directory is created only if it does not already exist. }
The method mkdirs() ensures all parent directories } else {
System.out.println("Directory already exists: " + directoryPath);
are also created. If creation is successful, the message }
"Directory created successfully: Projects/Java" is
printed to the console. If creation fails, the message
"Failed to create directory" is printed to the console.
If the directory already exists, the message "Directory
aready exists: Projects/Java" is printed to the console.

}
}

Close curly braces to end the CreateDirectory class


definition.

Explanation: This Java program creates a directory (including nested directories) if it does not already exist. It handles success and failure cases gracefully.

Listing directory contents

Description Example

import java.io.File;

Import the java.io.File package to represent file and directory paths.

Define a class ListDirectoryContents that contains the Java main method. public class ListDirectoryContents {
The String directoryPath = "Projects/Java" specifies the directory public static void main(String[] args) {
String directoryPath = "Projects/Java";
whose contents will be listed. Create a File object for the directory by File directory = new File(directoryPath);
calling the File(directoryPath) method. The File object represents the
directory but does not perform any operations yet. The directory.list() // List all files and directories in the specified directory
method returns an array of filenames that exist in the directory. If the String[] contents = directory.list();
directory does not exist or is empty, list() returns null. The if (contents
if (contents != null) {
!= null) method ensures the directory exists and is not empty before
System.out.println("Contents of " + directoryPath + ":");
proceeding. If contents is null, it prints: "The directory is empty or does for (String fileName : contents) {
not exist." If the directory contains files/subdirectories, the program prints System.out.println(fileName);
"Contents of Projects/Java:", iterates through the contents array and }
prints each filename. } else {
System.out.println("The directory is empty or does not exist.");
}

about:blank 65/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the ListDirectoryContents class definition.

Explanation: This Java program lists all files and subdirectories inside a directory and handles cases where the directory is empty or does not exist
It uses the File.list() method to retrieve directory contents efficiently.

Deleting a directory

Description Example

import java.io.File;

Import the java.io.File package to represent file and directory paths.

public class ListDirectoryContents {


public static void main(String[] args) {
String directoryPath = "Projects/Java";
File directory = new File(directoryPath);

// List all files and directories in the specified directory


String[] contents = directory.list();
Define a class ListDirectoryContents that contains the Java main method.
The String directoryPath = "Projects/Java" specifies the directory to if (contents != null) {
be deleted. Create a File object for the directory by calling the System.out.println("Contents of " + directoryPath + ":");
File(directoryPath) method. The File object represents the directory but for (String fileName : contents) {
does not perform any operations yet. The if (directory.exists() method System.out.println(fileName);
}
ensures the directory exists before attempting deletion. The .delete()
} else {
method deletes the directory only if it is empty. If successful, it prints System.out.println("The directory is empty or does not exist.");
"Directory deleted successfully: Projects/Java". If it fails (for example, }
because it contains files/subdirectories), it prints "Failed to delete
directory. It may not be empty.". If the directory is missing, it prints:
"Directory does not exist: Projects/Java".

}
}

Close curly braces to end the ListDirectoryContents class definition.

Explanation: This Java program uses the File.delete() method to delete a specified directory if it exists. The program handles success and failure cases gracefully.

Creating a directory with NIO

Description Example

Import Java class java.nio.file.Files for file and import java.nio.file.Files;


directory operations, java.nio.file.Path to represent import java.nio.file.Path;
import java.nio.file.Paths;

about:blank 66/79
4/11/25, 6:50 PM about:blank

Description Example
file and directory paths in a platform-independent import java.io.IOException;
way, java.nio.file.Paths to create Path instances,
and java.io.IOException to handle potentila I/O
errors.

public class CreateDirectory {


public static void main(String[] args) {
// Define the directory path
String directoryPath = "Projects/Java";

// Create a File object


Define a class CreateDirectory that contains the Java File directory = new File(directoryPath);
main method. The method
Paths.get("Projects/NioExample") creates a Path // Create the directory
object representing the directory to be created. The if (!directory.exists()) {
try block uses Files.createDirectories() instead of boolean created = directory.mkdirs(); // Use mkdirs() to create nested directories
if (created) {
File.mkdirs() to creates all necessary parent
System.out.println("Directory created successfully: " + directoryPath);
directories if they don't exist. It does not throw an } else {
error if the directory already exists and stores the System.out.println("Failed to create directory.");
created directory path in createdDir. The program }
prints "Directory created successfully: } else {
System.out.println("Directory already exists: " + directoryPath);
Projects/NioExample" if it is successful. The catch }
block catches IOException if directory creation fails
(for example, insufficient permissions) and prints an
error message: "Failed to create directory:
<error_message>".

}
}

Close curly braces to end the CreateDirectory class


definition.

Explanation: This Java program creates a directory using Java NIO (New Input/Output) instead of the traditional File class. It handles success and failure cases gracefully
and works cross-platform.

Real World example of Document Management System

Description Example

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Import Java class java.nio.file.Files for import java.io.IOException;
file and directory operations, import java.util.Scanner;
java.nio.file.Path to represent file and
directory paths in a platform-independent
way, java.nio.file.Paths to create Path
instances, java.io.IOException to handle
potentila I/O errors, and java.util.Scanner
for handling user input.

Define a class DocumentManagementSystem public class DocumentManagementSystem {


that contains the Java main method. All private static final String BASE_DIRECTORY = "Documents";
directory operations will occur within the public static void main(String[] args) {
"Documents" folder defined by the String Scanner scanner = new Scanner(System.in);
BASE_DIRECTORY. The main method String command;
continuously prompts the user to choose an
option and calls the corresponding method while (true) {
System.out.println("1. Create directory\n2. List documents\n3. Delete directory\n4. Exit");
based on user input: "1" creates a new command = scanner.nextLine();
directory inside "Documents", "2" lists
contents of a specified directory, "3" deletes switch (command) {
case "1": createDirectory(scanner); break;

about:blank 67/79
4/11/25, 6:50 PM about:blank

Description Example
a specified directory, and "4" exits the case "2": listDirectory(scanner); break;
program. case "3": deleteDirectory(scanner); break;
case "4": scanner.close(); return;
default: System.out.println("Invalid choice.");
}
}
}

private static void createDirectory(Scanner scanner) {


System.out.print("New directory name: ");
Path path = Paths.get(BASE_DIRECTORY, scanner.nextLine());
try {
System.out.println("Created: " + Files.createDirectories(path));
The createDirectory() method creates a } catch (IOException e) {
new directory and reads directory name System.err.println("Error: " + e.getMessage());
from user input. It uses }
Files.createDirectories(path) to create }
the directory (including missing parent
directories). If successful, it prints "Created:
path". If an error occurs, it prints "Error:
message".

private static void listDirectory(Scanner scanner) {


System.out.print("Directory to list: ");
Path path = Paths.get(BASE_DIRECTORY, scanner.nextLine());
try {

Files.list(path).forEach(System.out::println);
The listDirectory() method lists the } catch (IOException e) {
contents of a directory and reads directory System.err.println("Error: " + e.getMessage());
name from user input. It uses }
}
Files.list(path) to retrieve the directory
and prints each file/subdirectory. If the
directory doesn't exist or an error occurs, it
prints "Error: message".

private static void deleteDirectory(Scanner scanner) {


System.out.print("Directory to delete: ");
Path path = Paths.get(BASE_DIRECTORY, scanner.nextLine());
try {
Files.delete(path);
The deleteDirectory() method deletes a System.out.println("Deleted: " + path);
directory and reads directory name from } catch (IOException e) {
user input. It uses Files.delete(path) to System.err.println("Error: " + e.getMessage());
delete the specified directory. If successful, }
it prints "Deleted: path". The
Files.delete(path) will fail if the directory
is not empty. It only works on empty
directories.

}
}

Close curly braces to end the main class


definition.

Explanation: This Java program provides a simple command-line interface for managing directories inside a "Documents" folder. It allows users to create, list, and delete
directories using Java NIO (New Input/Output).

about:blank 68/79
4/11/25, 6:50 PM about:blank

Using Java Date and Time Classes


Using the LocalDate class

Description Example

import java.time.LocalDate;

Import the LocalDate class, which is part of the Java Date and Time API.

public class LocalDateExample {


public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);

Define a public class LocalDateExample that contains the Java main method. Use LocalDate.now() to
retrieve the current date and print it in the "YYYY-MM-DD" format, which is the default format of
LocalDate.toString().

}
}

Close curly braces to end the LocalDateExample class definition.

Explanation: This Java program demonstrates the use of the LocalDate class from the java.time package to get and display the current date.

Using the LocalTime class

Description Example

import java.time.LocalTime;

Import the LocalTime class, which is part of the Java Date and Time API.

public class LocalTimeExample {


public static void main(String[] args) {
LocalTime currentTime = LocalTime.now();
System.out.println("Current time: " + currentTime);
Define a public class LocalTimeExample that contains the Java main method. Use
LocalTime.now() to retrieve the current system time and print it in the "HH:mm:ss.SSS"
(hours, minutes, seconds, and milliseconds/nanoseconds) format, which is the default format
of LocalTime.toString().

about:blank 69/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the LocalTimeExample class definition.

Explanation: This Java program demonstrates the use of the LocalTime class from the java.time package to get and display the current time.

Using the LocalDateTime class

Description Example

import java.time.LocalDateTime;

Import the LocalDateTime class, which is part of the Java Date and Time API.

public class LocalDateTimeExample {


public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Current date and time: " + now);
Define a public class LocalDateTimeExample that contains the Java main method. Use
LocalDateTime.now() to retrieve the current system date and time. Print the current date and
time in the default format "YYYY-MM-DDTHH:MM:SS.SSS" (year, month, day, hours,
minutes, seconds, and milliseconds/nanoseconds), which is the default format of
LocalDateTime.toString().

}
}

Close curly braces to end the LocalDateTimeExample class definition.

Explanation: This Java program demonstrates the use of the LocalDateTime class from the java.time package to get and display the current date and time. LocalDateTime
is an immutable class that represents both date and time without a time zone.

Using the ZonedDateTime class

Description Example

import java.time.ZonedDateTime;

Import the ZonedDateTime class, which is part of the Java Date and Time
API.

Define a public class ZonedDateTimeExample that contains the Java main public class ZonedDateTimeExample {
method. Use ZonedDateTime.now() to retrieve the current system date and public static void main(String[] args) {
ZonedDateTime zonedNow = ZonedDateTime.now();
time, including the time zone. Print the current date, time, and zone in the System.out.println("Current date and time with zone: " + zonedNow);
default ISO-8601 format.

about:blank 70/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the ZonedDateTimeExample class definition.

Explanation: This Java program demonstrates how to use the ZonedDateTime class from the java.time package to retrieve and display the current date and time along with
the time zone. It is useful when working with time zones in applications such as scheduling, logging, and internationalization.

Real World example of an Event Management System

Description Example

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Scanner;
Import the LocalDate, LocalTime,
LocalDateTime, ZoneId, ZonedDateTime, and
Scanner classes that are part of the Java
Date and Time API.

public class EventManagement {

static class Event {


String name;
LocalDate date;
LocalTime time;
ZoneId timeZone;

public Event(String name, LocalDate date, LocalTime time, ZoneId timeZone) {


this.name = name;
Define an EventManagement class to this.date = date;
this.time = time;
represent an event with name, date, time, this.timeZone = timeZone;
and timeZone. The method }
getEventDateTime() converts LocalDate and public ZonedDateTime getEventDateTime() {
LocalTime into LocalDateTime. Then LocalDateTime localDateTime = LocalDateTime.of(date, time);
converts LocalDateTime into ZonedDateTime return ZonedDateTime.of(localDateTime, timeZone);
}
using the specified time zone. }

Define a public class with the Java main public static void main(String[] args) {
method and use it to accept user input for Scanner scanner = new Scanner(System.in);
event details. This class captures name, date, // Input event details
time, and timeZone from user input. The System.out.println("Enter event name:");
method Event(name, date, time, String name = scanner.nextLine();
timeZone) creates an event object through
user input. The method getEventDateTime() System.out.println("Enter event date (YYYY-MM-DD):");
String dateInput = scanner.nextLine();
displays the event date an time in the LocalDate date = LocalDate.parse(dateInput);
specified time zone. The method
ZonedDateTime converts eventDateTime to System.out.println("Enter event time (HH:MM):");
the system's local time zone. The method String timeInput = scanner.nextLine();
scanner.close() closes the scanner to free LocalTime time = LocalTime.parse(timeInput);
up resources. System.out.println("Enter time zone (e.g., America/New_York):");
String zoneInput = scanner.nextLine();
ZoneId timeZone = ZoneId.of(zoneInput);

about:blank 71/79
4/11/25, 6:50 PM about:blank

Description Example
// Create the event
Event event = new Event(name, date, time, timeZone);

// Display event details


System.out.println("Event created: " + event.name);
ZonedDateTime eventDateTime = event.getEventDateTime();
System.out.println("Event Date and Time: " + eventDateTime);

// Display in system's default time zone


ZonedDateTime defaultZonedDateTime = eventDateTime.withZoneSameInstant(ZoneId.systemDefault());
System.out.println("Event Date and Time in your local time zone: " + defaultZonedDateTime);

scanner.close();

}
}

Close curly braces to end the


EventManagement class definition.

Explanation: This Java program is a simple event management system that allows users to enter an event's details, including its name, date, time, and time zone. It then
converts and displays the event time in both the specified time zone and the system's default time zone.

Formatting Dates in Java


Formatting a date using LocalDate

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Import the LocalDate class to represent a date (year, month, day)


without time or a time zone and DateTimeFormatter class to define a
custom format for displaying dates.

public class DateFormattingExample {


public static void main(String[] args) {
// Get the current date
LocalDate currentDate = LocalDate.now();

// Define the format


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Define a public class DateFormattingExample that contains the Java
// Format the date
main method. Use LocalDate.now() to retrieve the current date in the String formattedDate = currentDate.format(formatter);
"YYYY-MM-DD" format, which is the default format of
LocalDate(). Define a date format using // Print the formatted date
DateTimeFormatter.ofPattern("dd/MM/yyyy"). Format the date using System.out.println("Formatted Date: " + formattedDate);
currentDate.format(formatter) to convert the current date into the
specified format and print the formatted date to the console.

Close curly braces to end the DateFormattingExample class definition. }


}

about:blank 72/79
4/11/25, 6:50 PM about:blank

Description Example

Explanation: This Java program demonstrates how to format a date using DateTimeFormatter from the java.time package. It formats dates into a human-friendly format.

Real World example of formatting birthdates in a User Registration System

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

Import the LocalDate class to represent a date (year,


month, day) without time or a time zone, the
DateTimeFormatter class to define a custom format for
displaying dates, and the Scanner class to get user input.

public class UserRegistration {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Get user's name


System.out.print("Enter your name: ");
String name = scanner.nextLine();

// Get user's birthdate


System.out.print("Enter your birthdate (yyyy-MM-dd): ");
Define a public class UserRegistration that contains the String birthdateInput = scanner.nextLine();
Java main method. Create a Scanner object to read user
input. Get the user name and store it in the name variable. // Parse the input string into a LocalDate object
Get the user birthdate in the "YYYY-MM-DD" format. LocalDate birthdate = LocalDate.parse(birthdateInput);
The input string birthdateInput is converted into a
// Define the desired output format
LocalDate object using LocalDate.parse(). Format the DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy");
birthdate using the "EEEE, MMM dd, yyyy" pattern,
where EEEE is the full weekday name, such as // Format the birthdate using the defined formatter
"Monday"; MMM is the abbreviated month name, such String formattedBirthdate = birthdate.format(formatter);
as Mar, dd is the two-digit day, such as 11, and "yyyy" is
// Display the result
the four-digit year, such as 2025. Use the System.out.println("Hello " + name + "! Your birthdate is: " + formattedBirthdate);
birthdate.format(formartter) method to convert the
date into a readable format. Print a personalized message // Close the scanner
with the formatted birthdate and close the scanner. scanner.close();

}
}

Close curly braces to end the UserRegistration class


definition.

Explanation: This Java program prompts the user to enter their name and birthdate, then formats and displays the birthdate in a more readable format.

Using Timezones in Java


Creating a ZoneId

Description Example

Import ZoneId which is part of the Java Date and Time API class to represent a time zone, import java.time.ZoneId;
such as "America/New_York", "Asia/Tokyo", and "Europe/London".

about:blank 73/79
4/11/25, 6:50 PM about:blank

Description Example

public class TimeZoneExample {


public static void main(String[] args) {
// Creating a ZoneId for New York
ZoneId newYorkZone = ZoneId.of("America/New_York");
System.out.println("Time Zone ID: " + newYorkZone);
Define a public class TimeZoneExample that contains the Java main method. Use
ZoneId.of("America/New_York") to create a ZoneId object for New York and display the Time
Zone ID to the console.

}
}

Close curly braces to end the TimeZoneExample class definition.

Explanation: This Java program demonstrates how to create and display a time zone ID using the java.time package.

Creating a ZoneDateTime

Description Example

import java.time.ZonedDateTime;
import java.time.ZoneId;

Import the ZonedDateTime and ZoneId classes which are part of


the Java Date and Time API class to represent a date-time with a
time zone.

public class ZonedDateTimeExample {


public static void main(String[] args) {
// Getting the current date and time in New York
ZonedDateTime newYorkTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("Current Date and Time in New York: " + newYorkTime);
Create a time zone object for New York by calling
ZoneId.of("America/New_York") and retrieve the current date
and time in that time zone. Display the current date and time in
New York.

}
}

Close curly braces to end the ZoneDateTimeExample class


definition.

Explanation: This Java program demonstrates how to create and display a time zone ID using the java.time package.

about:blank 74/79
4/11/25, 6:50 PM about:blank

Real World example of Scheduling Meeting across Time Zones

Description Example

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

Import the ZonedDateTime, ZoneId, and DateTimeFormatter


classes which are part of the Java Date and Time API class to
represent a date-time with a time zone and format the date-
time in a custom pattern.

public class ConferenceScheduler {


public static void main(String[] args) {
// Define the meeting time in UTC
ZonedDateTime meetingTimeUTC = ZonedDateTime.parse("2024-12-30T15:00:00Z");

// Define participant time zones


String[] participantTimeZones = {
Define the meeting time in UTC. "America/New_York", // Eastern Standard Time (EST)
ZonedDateTime.parse("2024-12-30T15:00:00Z") parses the "Europe/London", // Greenwich Mean Time (GMT)
"Asia/Kolkata", // Indian Standard Time (IST)
fixed UTC time (2024-12-30 15:00:00 UTC) into a "Australia/Sydney" // Australian Eastern Daylight Time (AEDT)
ZonedDateTime object. Create an array of time zones for };
participants in New York, London, Kolkata, and Sydney.
These time zones are later used to convert the UTC time to // Format for displaying the date and time
each participant's local time. Create a custom formatter for DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
displaying the date and time in the pattern: // Print the meeting time in each participant's local time zone
DateTimeFormatter.ofPattern("yyyy-MM-dd HH꞉mm꞉ss z"). System.out.println("Meeting Time in UTC: " + meetingTimeUTC.format(formatter));
Print the meeting time in UTC using the defined format. For for (String timeZone : participantTimeZones) {
each time zone, use ZonedDateTime localTime = meetingTimeUTC.withZoneSameInstant(ZoneId.of(timeZon
meetingTimeUTC.withZoneSameInstant(ZoneId.of(timeZone)) System.out.println("Meeting Time in " + timeZone + ": " + localTime.format(for
to convert the meeting time from UTC to the local time of }
that participant's time zone and print the meeting time in the
participant's local time zone using the custom formatter.

}
}

Close curly braces to end the ConferenceScheduler class


definition.

Explanation: This Java program simulates scheduling a meeting across different time zones. It converts a fixed UTC meeting time to the local times of participants in
various time zones and displays it in a formatted way.

Parsing Dates from Strings in Java


Parsing dates with DateTimeFormatter

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Import the LocalDate and DateTimeFormatter classes, which are part


of the Java Date and Time API class and used to represent dates
without a time zone and define a pattern for parsing and formatting
dates.

Create a public class DateParsingExample that contains the Java main public class DateParsingExample {
method and define a string variable dateString to represent date in public static void main(String[] args) {
// Define a date string to parse
the format "yyyy-MM-dd". Create a date formatter using the String dateString = "2025-01-23";

about:blank 75/79
4/11/25, 6:50 PM about:blank

Description Example
DateTimeFormatter.ofPattern("yyyy-MM-dd") method. Use
LocalDate.parse(dateString, formatter) to convert the dateString // Create a DateTimeFormatter to define the expected format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
into a LocalDate object and print the parsed date.
// Parse the string into a LocalDate object
LocalDate date = LocalDate.parse(dateString, formatter);

// Output the parsed date


System.out.println("Parsed date: " + date);

}
}

Close curly braces to end the DateParsingExample class definition.

Explanation: This Java program demonstrates how to parse a date string into a LocalDate object using the DateTimeFormatter class.

Using custom date formats

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

Import the LocalDate and DateTimeFormatter classes, which are part


of the Java Date and Time API class and used to represent dates
without a time zone and define a pattern for parsing and formatting
dates.

public class CustomDateParsing {


public static void main(String[] args) {
String dateString = "23/01/2025";

// Define the pattern for parsing


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Create a public class CustomDateParsing that contains the Java main
LocalDate date = LocalDate.parse(dateString, formatter);
method and define a string variable dateString to represent date in
the format "dd/MM/yyyy". Create a date formatter using the System.out.println("Parsed date: " + date);
DateTimeFormatter.ofPattern("dd/MM/yyyy") method. Use
LocalDate.parse(dateString, formatter) to convert the dateString
into a LocalDate object and print the parsed date.

}
}

Close curly braces to end the CustomDateParsing class definition.

Explanation: This Java program demonstrates how to parse a date string with a custom format into a LocalDate object using the DateTimeFormatter class.

Parsing LocalDateTime

about:blank 76/79
4/11/25, 6:50 PM about:blank

Description Example

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Import the LocalDateTime and DateTimeFormatter classes,


which are part of the Java Date and Time API class and used to
represent dates without a time zone and define a pattern for
parsing and formatting dates.

public class DateTimeParsingExample {


public static void main(String[] args) {
String dateTimeString = "2025-01-23 15:30";

// Define the pattern for date and time


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
Create a public class DateTimeParsingExample that contains the
Java main method and define a string variable dateString to LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
represent date in the "yyyy-MM-dd" format. Create a date
System.out.println("Parsed date and time: " + dateTime);
formatter using the DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm") method. Use LocalDateTime.parse(dateTimeString,
formatter) to convert the dateTimeString into a LocalDateTime
object using the formatter and print the parsed date.

}
}

Close curly braces to end the DateTimeParsingExample class


definition.

Explanation: This Java program demonstrates how to parse a date string with a custom format into a LocalDateTime object using the DateTimeFormatter class.

Example of extracting date from a simple sentence

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Import the LocalDate, DateTimeFormatter, and
DateTimeParseException classes, which are part
of the Java Date and Time API class and used to
represent dates without a time zone, define a
pattern for parsing and formatting dates, and
handle errors if the date format is incorrect.

Create a public class ExtractDateFromSentence public class ExtractDateFromSentence {


that contains the Java main method and define a public static void main(String[] args) {
String sentence = "The event will take place on 2025-01-23.";
sentence containing a date formatted as "yyyy-
MM-dd". Extract the date substring using // Define the date pattern
sentence.substring(sentence.indexOf("on") + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
3, sentence.indexOf(".")). The
sentence.indexOf("on") + 3 method finds the // Extract the date part from the string
position of "on" and moves three characters String dateString = sentence.substring(sentence.indexOf("on") + 3, sentence.indexOf("."));
forward to skip "on " (with the space), try {
sentence.indexOf(".") identifies the position of LocalDate date = LocalDate.parse(dateString, formatter);
the period (".") at the end of the date, and System.out.println("Extracted date: " + date);
substring(...) extracts the portion of the string } catch (DateTimeParseException e) {
that contains the date. Parse the extracted date System.out.println("Error parsing date: " + e.getMessage());
}
using LocalDate.parse(dateString, formatter)
and convert the extracted string into a LocalDate
object. The try-catch block prints the extracted
date if successful. If parsing fails due to an
incorrect format, the block catches
DateTimeParseException and displays an error
message.

about:blank 77/79
4/11/25, 6:50 PM about:blank

Description Example

}
}

Close curly braces to end the


ExtractDateFromSentence class definition.

Explanation: This Java program extracts a date from a given sentence, parses it into a LocalDate object, and displays it in a structured format. It also gracefully handles
potential parsing errors.

Example of extracting multiple dates from a text string

Description Example

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Import the LocalDate, DateTimeFormatter, and
DateTimeParseException classes, which are part of the Java Date and
Time API class and used to represent dates without a time zone,
define a pattern for parsing and formatting dates, and handle errors
if the date format is incorrect.

public class ExtractMultipleDates {


public static void main(String[] args) {
String text = "Important dates: 2025-01-23, 2025-02-14, and 2025-03-01.";

// Define the date pattern


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Create a public class ExtractMultipleDates that contains the Java
// Split the string to find dates
main method and define a text string containing three dates in the
String[] parts = text.split(", | and ");
"yyyy-MM-dd" format. These dates are separated by commas and
the word "and". Define the date format using for (String part : parts) {
DateTimeFormatter.ofPattern("yyyy-MM-dd"). Use regular try {
expressions (", | and ") to split the string by comma followed by a LocalDate date = LocalDate.parse(part.trim(), formatter);
System.out.println("Extracted date: " + date);
space (", ") and the word "and" followed by a space ("and "). This } catch (DateTimeParseException e) {
extracts the date strings from the text. Iterate over the extracted parts System.out.println("Error parsing date: " + part.trim());
and parse dates. For each extracted part, trim() removes any leading }
or trailing spaces and LocalDate.parse(part.trim(), formatter) }
converts the string into a LocalDate object. If parsing is successful, it
prints the extracted date. If parsing fails, the catch block handles the
error and prints an error message.

}
}

Close curly braces to end the ExtractMultipleDates class definition.

Explanation: This Java program extracts multiple dates from a given text, parses them into LocalDate objects, and prints them in a structured format. It also handles
potential errors if any part of the text is not in the expected date format.

Example of extracting dates from mixed content

Description Example

Import the LocalDate, DateTimeFormatter, and import java.time.LocalDate;


DateTimeParseException classes, which are part of import java.time.format.DateTimeFormatter;

about:blank 78/79
4/11/25, 6:50 PM about:blank

Description Example
the Java Date and Time API class and used to import java.time.format.DateTimeParseException;
represent dates without a time zone, define a
pattern for parsing and formatting dates, and
handle errors if the date format is incorrect.

public class ExtractDatesFromMixedContent {


public static void main(String[] args) {
String mixedContent = "Please note that our deadlines are on 2025-01-23 and 2025-02-28.";
Create a public class
ExtractDatesFromMixedContent that contains the // Define the date pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Java main method and define a string named
mixedContent containing a mixture of text and two // Split based on spaces and check each part
dates (2025-01-23 and 2025-02-28). The dates are String[] words = mixedContent.split(" ");
in the "yyyy-MM-dd" format. These dates are
separated by commas and the word "and". Define for (String word : words) {
if (word.matches("\\d{4}-\\d{2}-\\d{2}")) { // Check if it matches a date pattern
the date format using
try {
DateTimeFormatter.ofPattern("yyyy-MM-dd"). LocalDate date = LocalDate.parse(word, formatter);
Splits the input string by spaces into individual System.out.println("Extracted date: " + date);
words. The resulting words[] array contains both } catch (DateTimeParseException e) {
text and possible date strings. Iterate over each System.out.println("Error parsing date: " + word);
}
word using the regex word.matches("\\d{4}-
}
\\d{2}-\\d{2}") and check if it matches the date }
pattern (yyyy-MM-dd). If a word matches the
pattern, attempt to parse it into a LocalDate using
the previously defined formatter. If parsing is
successful, prints the extracted date. If there is a
parsing error (invalid date), the try-catch block
handles it and prints an error message.

}
}

Close curly braces to end the


ExtractDatesFromMixedContent class definition.

Explanation: This Java program extracts dates from a string containing mixed content (text and dates), parses them into LocalDate objects, and prints the valid dates. If
any date format is invalid, the program gracefully handles the error.

Author(s)
Ramanujam Srinivasan
Lavanya Thiruvali Sunderarajan

about:blank 79/79

You might also like