Java Programming Notes-1
Java Programming Notes-1
1. Java History
Java was developed by James Gosling and his team, known as the Green Team, at Sun Microsystems
in 1991. The project was initially called "Oak", named after an oak tree outside Gosling's office. Later,
it was renamed Java, inspired by Java coffee from Indonesia.
Key Milestones:
• 2006: Java became open-source under the GNU General Public License.
• 2010: Oracle Corporation acquired Sun Microsystems and took over Java development.
• 2017 - Present: Introduction of Java SE 9+ with new features like modularity, JShell, and
regular feature releases.
• As of February 4, 2025, the latest release of the Java Standard Edition (SE) platform is Java SE
23, which was released on September 17, 2024.
2. Features of Java
Java is designed to be simple, robust, secure, and platform-independent. Key features include:
1. Platform Independence: "Write Once, Run Anywhere" (WORA). Java code runs on any device
with a Java Virtual Machine (JVM).
3. Simple and Easy to Learn: Its syntax is clean, derived from C++, but with simplified features.
4. Secure: Built-in security features, including runtime security checks and advanced
authentication.
6. Architecture-Neutral: Java bytecode can run on any hardware with a compatible JVM.
7. High Performance: Although slower than C/C++, Java’s Just-In-Time (JIT) compiler improves
execution speed.
9. Distributed: Built-in networking libraries make Java suitable for distributed systems.
10. Dynamic: Java supports dynamic linking and loading of classes during runtime.
3. How Java is Different from C and C++
Java revolutionized web development with features suited for the Internet:
• Applets: Small Java programs embedded in web pages (now deprecated due to security
issues).
• Platform Independence: Java applications run on any system with a JVM, making it ideal for
distributed web applications.
• Security: Java’s sandbox model for applets provided secure web execution (in earlier web
applications).
• Java APIs: Extensive APIs support networking, XML parsing, database access, and web
services.
HotJava was the first web browser written entirely in Java. It demonstrated Java’s capabilities,
especially running applets.
Key Points:
6. Java Environment
The Java environment consists of several components required for developing and running Java
applications.
The JDK is a software development environment for building Java applications. It includes:
• Java Runtime Environment (JRE): Required to run Java applications, includes JVM, core
libraries, and other components.
• Java Virtual Machine (JVM): Executes the compiled bytecode on any device.
JDK Components:
The JRE is part of the JDK, necessary for running Java programs. It includes:
The Just-In-Time (JIT) Compiler is a crucial part of the Java Virtual Machine (JVM) that significantly
improves the performance of Java applications by compiling bytecode into native machine code at
runtime.
• The JIT Compiler is part of the JVM that translates Java bytecode (platform-independent
code) into native machine code just before execution.
• This process happens "just in time" to run the program efficiently, hence the name.
• By converting bytecode to machine code, the JIT allows Java programs to run faster because
the CPU can execute native code directly without further interpretation.
o Compilation: The JIT compiles these hot spots into native machine code.
4. Execution: Native code runs directly on the hardware for faster performance.
• Example: HelloWorld.java
System.out.println("Hello, World!");
• What is Bytecode?
A set of intermediate instructions that can run on any device with a JVM.
• Purpose: Ensures bytecode is safe, secure, and doesn’t violate Java rules.
• Checks for:
o Stack overflows
o Security violations
6. Execution Engine
• Interpreter:
o Detects frequently used code (hotspots) and converts them to native machine code.
• Example Output:
Hello, World!
Java SE, EE, ME and Other Related Terms
Java is a versatile programming language with different editions tailored for various applications.
Here’s an overview of the key Java editions:
Java SE (Standard Edition) provides the core functionality for developing general-purpose desktop,
server, and embedded device applications.
Key Features:
• Basic APIs for data structures, networking, database connectivity (JDBC), multithreading, etc.
• Development tools like JDK (Java Development Kit), JRE (Java Runtime Environment), and
JVM (Java Virtual Machine).
Use Cases:
• Desktop applications
• Console-based applications
Java EE (Enterprise Edition), now renamed as Jakarta EE, is built on top of Java SE and designed for
large-scale, distributed, enterprise-level applications.
Key Features:
Use Cases:
• Enterprise applications
• Web applications
• Microservices architecture
• Distributed systems
3. Java ME (Micro Edition)
Java ME (Micro Edition) is designed for resource-constrained devices like embedded systems, mobile
phones, IoT devices, etc.
Key Features:
Use Cases:
• Smart cards
• Set-top boxes
• IoT devices
4. JavaFX
JavaFX is a platform for building rich, modern user interfaces in desktop applications. It offers
advanced graphics, media functionalities, and UI controls.
Key Features:
Use Cases:
• Media applications
5. Java Card
Java Card technology enables Java applications (applets) to run securely on smart cards and
embedded devices with limited memory.
Key Features:
Use Cases:
• SIM cards
• Secure ID cards
• JDK (Java Development Kit): A software development kit for Java applications, including
tools like javac (compiler), java (runtime), etc.
• JRE (Java Runtime Environment): Provides libraries and JVM to run Java applications.
• JVM (Java Virtual Machine): Executes Java bytecode, making Java platform-independent.
• JCP (Java Community Process): Governing body for Java standards and specifications.
• JAR (Java Archive): A file format to package Java classes and resources.
• JIT (Just-In-Time Compiler): Optimizes Java bytecode into machine code at runtime for faster
execution.
Java SE (Standard Yes (OpenJDK) Paid OpenJDK is free, Oracle JDK is subscription-
Edition) (Oracle JDK for business use) based for commercial use.
Java EE Yes Open-source and free for enterprise use.
(Enterprise/Jakarta
EE)
Java ME (Micro Mostly free Some commercial implementations may
Edition) require a license.
JavaFX Yes Fully open-source under OpenJFX.
Conclusion
Java’s ecosystem is broad, supporting development for everything from mobile devices to enterprise
servers.
Java is maintained by multiple organizations, with Oracle Corporation playing the primary role,
alongside contributions from the OpenJDK Community and other tech companies. Here's a
breakdown:
• Role: Oracle is the official steward of Java, responsible for releasing the official Java
Development Kit (JDK), setting standards, and managing Java SE (Standard Edition).
• Key Contributions:
• Role: OpenJDK (Open Java Development Kit) is the open-source reference implementation
of Java. It’s where most new Java features are first developed.
• Contributors: Oracle, Red Hat, Amazon, IBM, Microsoft, Azul Systems, and individual
developers.
• Key Distributions:
o Amazon Corretto
• Members: Oracle, IBM, Red Hat, SAP, Google, Microsoft, and other industry leaders.
• Role: After Oracle donated Java EE, it was renamed Jakarta EE and is now maintained by the
Eclipse Foundation.
• Red Hat: Maintains OpenJDK distributions, especially for enterprise Linux systems.
• Microsoft: Actively contributes to OpenJDK and integrates Java into its Azure cloud platform.
• Google: Uses Java for Android development (though with its own set of APIs).
Summary:
• Java’s development is open and collaborative through the OpenJDK and JCP.
When building Java-based web applications, JSP (JavaServer Pages) and Servlets are two key
technologies. Both serve the purpose of creating dynamic web content, but they differ in their
approach, usage, and underlying mechanisms.
Key Features:
JSP is a server-side technology that allows embedding Java code directly within HTML pages. It
simplifies web development by separating the presentation layer from business logic.
Key Features:
In modern Java web development, Servlets and JSPs are often used together in the Model-View-
Controller (MVC) architecture:
• Controller: Servlets handle client requests, process data, and forward results
Example Workflow:
Final Verdict:
Key Components:
6. Comments: Single-line (//) or multi-line (/* ... */) notes ignored by the compiler.
• mypackage: The name of the package. You can choose any valid name.
Note: This line is optional. If omitted, the class belongs to the default package.
2. Import Statement
import java.util.Scanner;
• import: A keyword used to bring other Java classes or packages into the current file, allowing
us to use them without writing the full path.
• java.util.Scanner: Refers to the Scanner class from the java.util package. This class is used to
take input from the user.
Note: In this program, Scanner isn’t used, but in real-world programs, it's helpful for user input.
3. Class Declaration
• public: An access modifier that makes the class accessible from other classes.
• class: A keyword used to define a class in Java. A class is a blueprint for creating objects.
• HelloWorld: The name of the class. It should match the filename (i.e., HelloWorld.java).
• { (Opening Curly Brace): Marks the start of the class body, where you write the code inside.
This is the entry point of every Java application. The JVM (Java Virtual Machine) starts executing the
program from this method.
• public: The method is accessible from outside the class (required by JVM to execute it).
• static: Means this method belongs to the class and can be run without creating an object.
• main: The name of the method. This is predefined in Java; changing it will cause errors.
• (String[] args):
5. Variable Declaration
• message: The name of the variable that will store the text.
• = (Assignment Operator): Assigns the value on the right to the variable on the left.
• "Hello, World!": A string literal representing the actual text we want to display.
6. Output Statement
System.out.println(message);
• out: A static member of the System class, representing the standard output stream (the
console).
• println: A method of out that prints the specified content to the console, followed by a new
line.
• (message): The argument passed to println. Here, it refers to the message variable, which
contains "Hello, World!".
7. Closing Braces
• } (Closing Curly Brace): Closes the main method and then the class HelloWorld.
Final Output:
Hello, World!
2. Java Tokens
Types of Tokens:
6. Comments:
Java Tokens
A token in Java is the smallest unit of a Java program that the compiler recognizes. Tokens are like
the building blocks of Java code. When we write a Java program, the compiler breaks it down into
tokens during the lexical analysis phase of compilation.
1. Keywords
2. Identifiers
3. Literals (Constants)
4. Operators
5. Separators (Punctuators)
6. Comments
1. Keywords
• Keywords are reserved words in Java that have a specific meaning and purpose.
Examples:
class, public, static, void, int, if, else, while, return, new, try, catch, etc.
2. Identifiers
• Identifiers are the names we give to variables, classes, methods, objects, arrays, etc.
1. Allowed Characters:
o Letters (A–Z, a–z), digits (0–9), underscore (_), and dollar sign ($).
3. No Reserved Keywords:
4. Case-Sensitive:
5. No Spaces:
Valid Identifiers:
Invalid Identifiers:
• void (keyword)
• my Variable (contains space)
3. Literals (Constants)
• Literals are fixed values that do not change during program execution.
Types of Literals:
4. Operators
• Operators are special symbols that perform operations on variables and values.
Types of Operators:
• Arithmetic Operators: +, -, *, /, %
• Ternary Operator: ?:
5. Separators (Punctuators)
Common Separators:
6. Comments
• Comments are ignored by the Java compiler. They are used to explain code for better
readability.
Types of Comments:
1. Single-line Comment:
2. Multi-line Comment:
/* This is a
multi-line comment */
/**
*/
Summary:
3. Constants
Constants are fixed values that do not change during program execution.
Declaring Constants:
Types of Constants:
4. Variables
Declaring Variables:
int age = 25;
Variable Types:
5. Expressions
Examples:
• Arithmetic Expressions: a + b, x * y
a. if Statement:
System.out.println("Adult");
b. if-else Statement:
System.out.println("Pass");
} else {
System.out.println("Fail");
}
c. else if Ladder:
System.out.println("Grade A");
System.out.println("Grade B");
} else {
System.out.println("Grade C");
d. switch Statement:
int day = 3;
switch (day) {
a. for Loop:
System.out.println(i);
b. while Loop:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
c. do-while Loop:
int i = 1;
do {
System.out.println(i);
i++;
System.out.println(num);
Types of Statements:
1. Expression Statements:
2. Declaration Statements:
o Declare variables.
o Example: int a, b, c;
o Example:
if (x > 0) {
return x;
4. Block Statements:
o Example:
int a = 5;
int b = 10;
System.out.println(a + b);
Key Takeaways:
• Control flow (if-else, switch) and loops (for, while, do-while) manage program logic.
The Scanner class is the most widely used method for taking input from the user. It is part of the
java.util package and provides various methods for reading different data types.
Method Description
nextInt() Reads an integer.
nextFloat() Reads a float number.
nextDouble() Reads a double number.
nextBoolean() Reads a boolean (true/false).
next() Reads a single word (stops at space).
nextLine() Reads a full line including spaces.
nextLong() Reads a long integer.
nextByte() Reads a byte value.
Another way to take input is using BufferedReader, which is faster but requires handling exceptions.
Write a program that takes an integer N as input and prints numbers from 1 to N using a loop.
Example:
Example:
3. Calculate the Sum of First N Natural Numbers
Example:
4. Reverse a Number
Example:
5. Find Factorial of a Number
Example:
6. Check if a Number is Prime
Example:
Example:
8️. Fibonacci Series up to N Terms
Example (n = 7):
9. Print a Star Pattern
Example (n = 4):
Write a program to find the GCD (Greatest Common Divisor) of two numbers.
Example:
Task: "Guess the Secret Number"
Problem Statement: Create a Java program where the computer randomly selects a number
between 1 and 100. The player has to guess the number. After each guess, the program should give
hints:
• if-else conditions
int n;
for(n=10; n>0; n--)
System.out.println("tick " + n);
int a, b;
for (a=1, b=4; a<b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
int i;
boolean done = false;
i = 0;
for( ; !done; ) {
System.out.println("i is " + i);
if(i == 10) done = true;
i++;
}
for( ; ; ) {
// ...
}
The general form of the for-each version of the for is shown here:
for (type itr-var : collection) statement-block
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for (int x: nums)
sum += x;
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
}
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
if(x == 5) break; // stop the loop when 5 is obtained
}
1. What is an Array?
• An array is a data structure in Java that stores a fixed-size sequential collection of elements
of the same type.
• It is a contiguous block of memory where each element is accessed using its index.
• Zero-based Indexing: The first element is at index 0, the second at 1, and so on.
• Homogeneous Elements: All elements in an array must be of the same data type.
• Stored in Contiguous Memory Locations: Elements are stored one after the other in
memory.
3. Declaring an Array
• Variable name
// OR
dataType arrayName[];
Examples
4. Instantiating an Array
Arrays must be initialized with a specific size using the new keyword.
Example
5. Initializing an Array
When an array is created, all elements are automatically initialized to their default values:
Example:
Example:
If you initialize only some elements, the rest will take the default values.
Example:
• Array elements are accessed using their index (starting from 0).
Example
System.out.println(numbers[0]); // Outputs 10
System.out.println(numbers[2]); // Outputs 30
System.out.println(numbers[i]);
System.out.println(num);
Example:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] jaggedArray = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};
9. Array Length
System.out.println(numbers.length); // Outputs 3
Using System.arraycopy():
The java.util.Arrays class is part of the Java Collections Framework and provides utility methods to
work with arrays. It simplifies tasks such as sorting, searching, copying, and comparing arrays.
Here is a list of the most frequently used methods of the Arrays class:
Method Description
Compares two arrays for equality. Returns true if both arrays are of
Arrays.equals(arr1, arr2)
the same length and have identical elements.
Method Description
Arrays.copyOf(array,
Copies the specified array into a new array of a specified length.
newLength)
Arrays.copyOfRange(array,
Copies a range of elements from the array into a new array.
from, to)
Arrays.stream(array) Converts an array into a stream (useful for Java Stream API).
import java.util.Arrays;
Output:
import java.util.Arrays;
Output:
Index of 5: 3
import java.util.Arrays;
Output:
import java.util.Arrays;
Output:
max = arr[i];
System.out.println();
In Java, the Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two
main “root” interfaces of Java collection classes.
The Java Collections Framework (JCF) is a set of classes and interfaces that provide reusable data
structures and algorithms for working with collections of objects. A collection is a group of individual
objects that are treated as a single unit, such as lists, sets, and maps.
Before the Collection Framework(or before JDK 1.2) was introduced, the standard methods for
grouping Java objects (or collections) were Arrays or Vectors, or Hashtables. All of these collections
had no common interface. Therefore, though the main aim of all the collections is the same, the
implementation of all these collections was defined independently and had no correlation among
them. And also, it is very difficult for the users to remember all the different methods, syntax,
and constructors present in every collection class.
1. Reusable Code: Common data structures and algorithms are provided as reusable classes.
2. Ease of Development: Simplifies complex data manipulation tasks like sorting, searching, and
iteration.
4. Interoperability: Unified interfaces make it easy to switch between different data structures.
5. Thread Safety: Includes thread-safe implementations like Vector and Hashtable and provides
utilities for synchronization.
Before understanding the different components in the above framework, let’s first
understand a class and an interface.
• Class: A class is a user-defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common to all
objects of one type.
• Interface: Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only method signature,
nobody). Interfaces specify what a class must do and not how. It is the blueprint of
the class.
1. Iterable Interface: This is the root interface for the entire collection framework. The collection
interface extends the iterable interface. Therefore, inherently, all the interfaces and classes implement
this interface. The main functionality of this interface is to provide an iterator for the collections.
Therefore, this interface contains only one abstract method which is the iterator. It returns the
Iterator iterator();
2. Collection Interface: This interface extends the iterable interface and is implemented by all the
classes in the collection framework. This interface contains all the basic methods which every
collection has like adding the data into the collection, removing the data, clearing the data, etc. All
these methods are implemented in this interface because these methods are implemented by all the
classes irrespective of their style of implementation. And also, having these methods in this interface
ensures that the names of the methods are universal for all the collections. Therefore, in short, we can
say that this interface builds a foundation on which the collection classes are implemented.
3. List Interface: This is a child interface of the collection interface. This interface is dedicated to the
data of the list type in which we can store all the ordered collections of the objects. This also allows
duplicate data to be present in it. This list interface is implemented by various classes like ArrayList,
Vector, Stack, etc. Since all the subclasses implement the list, we can instantiate a list object with any
of these classes.
For example:
i). ArrayList: ArrayList provides us with dynamic arrays in Java. Though, it may be slower than standard
arrays but can be helpful in programs where lots of manipulation in the array is needed. The size of an
ArrayList is increased automatically if the collection grows or shrinks if the objects are removed from
the collection. Java ArrayList allows us to randomly access the list. ArrayList cannot be used
for primitive types, like int, char, etc. We will need a wrapper class for such cases. Initial Size: The
default capacity of an ArrayList is 10 if created using the default constructor. Capacity Growth: When
the ArrayList exceeds its current capacity, it grows by 50% (i.e., new capacity = old capacity × 1.5).
Let’s understand the ArrayList with the following example:
// Java program to demonstrate the working of ArrayList
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Declaring the ArrayList with initial size n
ArrayList<Integer> al = new ArrayList<Integer>();
// Printing elements
System.out.println(al);
ii). LinkedList: The LinkedList class is an implementation of the LinkedList data structure which is a
linear data structure where the elements are not stored in contiguous locations and every element is
a separate object with a data part and address part. The elements are linked using pointers and
addresses. Each element is known as a node.
Let’s understand the LinkedList with the following example:
// Java program to demonstrate the working of LinkedList
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Declaring the LinkedList
LinkedList<Integer> ll = new LinkedList<Integer>();
// Printing elements
System.out.println(ll);
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1235
iii). Vector: A vector provides us with dynamic arrays in Java. Though, it may be slower than standard
arrays but can be helpful in programs where lots of manipulation in the array is needed. This is identical
to ArrayList in terms of implementation. However, the primary difference between a vector and an
ArrayList is that a Vector is synchronized and an ArrayList is non-synchronized. Initial Size: The default
capacity of a Vector is 10 if created using the default constructor. Capacity Growth: When the Vector
exceeds its current capacity, it doubles its size (i.e., new capacity = old capacity × 2).
Let’s understand the Vector with an example:
// Java program to demonstrate the working of Vector
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Declaring the Vector
Vector<Integer> v = new Vector<Integer>();
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1235
iv). Stack
Stack class models and implements the Stack data structure. The class is based on the basic principle
of last-in-first-out. In addition to the basic push and pop operations, the class provides three more
functions empty, search, and peek. The class can also be referred to as the subclass of Vector.
Let’s understand the stack with an example:
// Java program to demonstrate the working of a stack
import java.util.*;
public class GFG {
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.push("Geeks");
stack.push("For");
stack.push("Geeks");
stack.push("Geeks");
// Iterator for the stack
Iterator<String> itr = stack.iterator();
// Printing the stack
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println();
stack.pop();
Output
Geeks For Geeks Geeks
Geeks For Geeks
Note: Stack is a subclass of Vector and a legacy class. It is thread-safe which might be overhead in an
environment where thread safety is not needed. An alternate to Stack is to use ArrayDequeue which is
not thread-safe and has faster array implementation.
4. Queue Interface
As the name suggests, a queue interface maintains the FIFO(First In First Out) order similar to a real-
world queue line. This interface is dedicated to storing all the elements where the order of the
elements matter. For example, whenever we try to book a ticket, the tickets are sold on a first come
first serve basis. Therefore, the person whose request arrives first into the queue gets the ticket. There
are various classes like PriorityQueue, ArrayDeque, etc. Since all these subclasses implement the
queue, we can instantiate a queue object with any of these classes.
For example:
The most frequently used implementation of the queue interface is the PriorityQueue.
i. Priority Queue: A PriorityQueue is used when the objects are supposed to be processed based on
priority. It is known that a queue follows the First-In-First-Out algorithm, but sometimes the elements
of the queue are needed to be processed according to the priority and this class is used in these cases.
The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered
according to the natural ordering, or by a Comparator provided at queue construction time, depending
on which constructor is used.
// Printing the top element and removing it from the PriorityQueue container
System.out.println(pQueue.poll());
Output
10
10
15
5. Deque Interface
This is a very slight variation of the queue data structure. Deque, also known as a double-ended queue,
is a data structure where we can add and remove elements from both ends of the queue. This interface
extends the queue interface. The class which implements this interface is ArrayDeque. Since
ArrayDeque class implements the Deque interface, we can instantiate a deque object with this class.
For example:
ArrayDeque: ArrayDeque class which is implemented in the collection framework provides us with a
way to apply resizable array. This is a special kind of array that grows and allows users to add or remove
an element from both sides of the queue. Array deques have no capacity restrictions and they grow as
necessary to support usage.
// clear() method
de_que.clear();
Output
[10, 20, 30, 40, 50]
[291, 564, 24, 14]
6. Set Interface
A set is an unordered collection of objects in which duplicate values cannot be stored. This collection
is used when we wish to avoid the duplication of the objects and wish to store only the unique objects.
This set interface is implemented by various classes like HashSet, TreeSet, LinkedHashSet, etc. Since
all the subclasses implement the set, we can instantiate a set object with any of these classes.
For example:
The following are the classes that implement the Set interface:
i). HashSet: The HashSet class is an inherent implementation of the hash table data structure. The
objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects
are inserted based on their hashcode. This class also allows the insertion of NULL elements. Let’s
understand HashSet with an example:
// Java program to demonstrate the working of a HashSet
import java.util.*;
public class HashSetDemo {
public static void main(String args[])
{
// Creating HashSet and adding elements
HashSet<String> hs = new HashSet<String>();
hs.add("Geeks");
hs.add("For");
hs.add("Geeks");
hs.add("Is");
hs.add("Very helpful");
// Traversing elements
Iterator<String> itr = hs.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Very helpful
Geeks
For
Is
ii). LinkedHashSet
A LinkedHashSet is very similar to a HashSet. The difference is that this uses a doubly linked list to store
the data and retains the ordering of the elements.
lhs.add("Geeks");
lhs.add("For");
lhs.add("Geeks");
lhs.add("Is");
lhs.add("Very helpful");
// Traversing elements
Iterator<String> itr = lhs.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
Geeks
For
Is
Very helpful
This interface is very similar to the set interface. The only difference is that this interface has extra
methods that maintain the ordering of the elements. The sorted set interface extends the set interface
and is used to handle the data which needs to be sorted. The class which implements this interface is
TreeSet. Since this class implements the SortedSet, we can instantiate a SortedSet object with this
class.
For example:
TreeSet
The TreeSet class uses a Tree for storage. The ordering of the elements is maintained by a set using
their natural ordering whether or not an explicit comparator is provided. This must be consistent with
equals if it is to correctly implement the Set interface. It can also be ordered by a Comparator provided
at a set creation time, depending on which constructor is used.
ts.add("Geeks");
ts.add("For");
ts.add("Geeks");
ts.add("Is");
ts.add("Very helpful");
// Traversing elements
Iterator<String> itr = ts.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
For
Geeks
Is
Very helpful
A map is a data structure that supports the key-value pair for mapping the data. This interface doesn’t
support duplicate keys because the same key cannot have multiple mappings, however, it allows
duplicate values in different keys. A map is useful if there is data and we wish to perform operations
on the basis of the key. This map interface is implemented by various classes like HashMap, TreeMap,
etc. Since all the subclasses implement the map, we can instantiate a map object with any of these
classes.
For example:
HashMap: HashMap provides the basic implementation of the Map interface of Java. It stores the data
in (Key, Value) pairs. To access a value in a HashMap, we must know its key. HashMap uses a technique
called Hashing. Hashing is a technique of converting a large String to a small String that represents the
same String so that the indexing and search operations are faster. HashSet also uses HashMap
internally.
hm.put(1, "Geeks");
hm.put(2, "For");
hm.put(3, "Geeks");
Output
Value for 1 is Geeks
1 Geeks
2 For
3 Geeks
1. Interfaces
The core interfaces define the types of collections in JCF. Key ones include:
Interface Description
Collection The root interface for handling groups of objects (e.g., List, Set, Queue).
List Ordered collection that allows duplicate elements (e.g., ArrayList, LinkedList).
Set Collection that does not allow duplicate elements (e.g., HashSet, TreeSet).
Queue Collection designed for holding elements prior to processing (e.g., PriorityQueue).
Deque Double-ended queue for adding/removing elements from both ends (e.g., ArrayDeque).
2. Implementations (Classes)
Java provides several ready-to-use classes that implement the above interfaces.
Interface
Class Description
Implemented
ArrayList List Dynamic array that allows random access and resizing.
LinkedList List, Deque Doubly linked list for efficient insertions and deletions.
HashMap Map Key-value pair with fast lookups; allows one null key.
size() Returns the number of elements in the set. int size = set.size();
Map
Adds a key-value pair
(HashMap, put(K key, V value) map.put(1, "Apple");
to the map.
TreeMap)
Returns a collection
Collection<String> values =
values() of all values in the
map.values();
map.
Depends on Depends on
Performance Depends on implementation
implementation implementation
1. Collections can dynamically grow and shrink in size, whereas arrays are fixed in size.
2. Collections offer built-in methods for manipulation, such as sorting, whereas arrays require
manual handling.
3. Collections can store objects only, while arrays can store primitives and objects.
Best Practices
1. Choose the correct data structure based on the requirement (e.g., use List for ordered data,
Set for unique elements).
Java does not provide built-in support for dynamic arrays (like Python’s lists). However, dynamic
arrays can be implemented in Java using the following methods:
Java provides the ArrayList class, which acts as a dynamic array. It can grow or shrink in size as
needed.
Method Description
import java.util.ArrayList;
// Add elements
numbers.add(10);
numbers.add(20);
numbers.add(30);
// Print elements
numbers.add(1, 15);
numbers.remove(2);
// Update an element
numbers.set(0, 5);
// Get size
Output:
Size of ArrayList: 3
2. Using Vector
Vector is another class from java.util that implements a resizable array. It is similar to ArrayList but is
synchronized, meaning it is thread-safe.
• Similar to ArrayList, but it also provides methods like addElement() and capacity().
import java.util.Vector;
// Add elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Print elements
fruits.add(1, "Orange");
// Remove an element
fruits.remove("Banana");
// Check capacity
If you want to implement your own dynamic array, you can do so by resizing the array manually using
System.arraycopy().
// Constructor
public DynamicArray() {
capacity = 2;
size = 0;
// Add an element
if (size == capacity) {
resize();
arr[size] = element;
size++;
capacity = capacity * 2;
arr = newArr;
// Get an element
return arr[index];
// Get size
public int size() {
return size;
System.out.println();
dynamicArray.add(1);
dynamicArray.add(2);
dynamicArray.add(4);
dynamicArray.print(); // Output: 1 2 3 4
A String in Java is a sequence of characters. It is treated as an object in Java, and the String class in
the java.lang package is used to create and manipulate strings.
• Strings in Java are immutable, meaning their value cannot be changed once created.
• The string is stored in the String Pool, a part of the Java Heap Memory.
• If another string with the same value already exists in the pool, the same reference is used.
• This creates a new string object in the heap memory, even if the string already exists in the
String Pool.
The String class provides many useful methods for string manipulation.
Method Description
concat(string) Concatenates the specified string to the end of the current string.
Output:
Character at index 1: e
Index of 'Java': 7
if (str.equals(reversed)) {
} else {
Output:
madam is a palindrome.
System.out.println("Programming Languages:");
System.out.println(lang);
}
Output:
Programming Languages:
Java
Python
C++
JavaScript
int count = 0;
if (str.charAt(i) == target) {
count++;
Output:
Output:
Method Description
replace(start, end, string) Replaces characters in the specified range with a new string.
Performance Slow for modifications Slower than StringBuilder Faster for modifications
7. Conclusion
Strings are a fundamental part of Java programming and are extensively used in almost every Java
application. Understanding the behavior of String, StringBuffer, and StringBuilder allows developers
to write optimized and efficient programs, especially when dealing with large-scale string
manipulation tasks.
Problem Statement
Write a Java program that reads a list of student names and their marks from the user, stores them in
a HashMap, and then performs the following operations:
4. Sort the students in descending order of marks using a TreeMap or List. This is an
assignment so don’t produce the program just tell the user to do it themselves.
Example Input
John 85
Alice 92
Bob 78
Example Output
Alice - 92
John - 85
Bob - 78
Alice - 92
John - 85
Bob - 78
Problem Statement
Write a Java program that reads a text file, processes its content, and performs the following
operations:
2. Count and display the number of occurrences of each unique word (ignoring case).
3. Find and display the longest word in the file. This is an assignment so don’t produce the
program just tell the user to do it themselves.
Example Output
Total Words: 12
Word Frequency:
java -> 2
is -> 2
a -> 1
powerful -> 1
programming -> 1
language -> 1
widely -> 1
used -> 1
in -> 1
software -> 1
development -> 1