The Java Experience
The Java Experience
By Simba
Introduction
This guide is designed to help students understand Java programming in a simple and
practical way. It covers Object-Oriented Programming (OOP), the Bubble Sort algorithm,
and Data Structures. Java can be fun when approached correctly, and this document
aims to eliminate fear and build confidence in writing loops and solving problems.
For additional Java materials, check out this link: Java Papers
What is OOP?
1. Encapsulation
Encapsulation is the process of bundling data (variables) and methods that operate on
that data into a single unit (class). It protects data from unintended modification.
https://stackedit.io/app# 1/17
29/01/2025, 15:49 StackEdit
Example:
class Person {
private String name;
2. Inheritance
Inheritance allows a class (child) to acquire properties and behaviors of another class
(parent), promoting code reusability.
Example:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
3. Polymorphism
https://stackedit.io/app# 2/17
29/01/2025, 15:49 StackEdit
class MathOperations {
int add(int a, int b) {
return a + b;
}
class Parent {
void showMessage() {
System.out.println("Message from Parent");
}
}
Imagine a restaurant:
Method Overloading: A chef can cook a meal, but the same method (cook) can be
used with different ingredients (parameters) to make different dishes.
Method Overriding: A new chef (child class) may change the recipe (method
behavior) while still calling it “cook” like the original chef (parent class).
4. Abstraction
https://stackedit.io/app# 3/17
29/01/2025, 15:49 StackEdit
Example:
Questions
Bubble Sort is a simple sorting algorithm that repeatedly swaps adjacent elements if
they are in the wrong order.
https://stackedit.io/app# 4/17
29/01/2025, 15:49 StackEdit
Implementation in Java
Loops are just ways to repeat actions. Think of them like dance instructions:
Questions
https://stackedit.io/app# 5/17
29/01/2025, 15:49 StackEdit
I’ll keep it simple and explain what each part of the code does:
System.out.println("Array elements:");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]); // Access array elements
}
Node(int data) {
this.data = data;
this.next = null;
}
https://stackedit.io/app# 6/17
29/01/2025, 15:49 StackEdit
Output:
Array Example:
Array elements:
10
20
30
40
50
https://stackedit.io/app# 7/17
29/01/2025, 15:49 StackEdit
50
Explanation:
1. Array:
Arrays are fixed in size. We initialize it with a fixed size of 5 elements, and
the size cannot be changed later.
Elements are accessed using indices.
2. Linked List:
A linked list consists of nodes. Each node contains data and a reference to
the next node.
Insertion of elements can be done at any time, and it dynamically grows or
shrinks in size as nodes are added or removed.
We created a simple LinkedList class with add and print methods for
demonstration.
Key Differences:
Size: Arrays have a fixed size, while linked lists can grow or shrink.
Accessing elements: Arrays allow direct access via indices, while linked lists
require traversal from the head node to access an element.
A stack is a data structure where elements are added and removed from the same end
(top).
import java.util.Stack;
class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
https://stackedit.io/app# 8/17
29/01/2025, 15:49 StackEdit
stack.pop(); // Removes 20
}
}
import java.util.LinkedList;
import java.util.Queue;
class QueueExample {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.add(10);
queue.add(20);
queue.poll(); // Removes 10
}
}
LISTS:
In Java, a List is an ordered collection (also known as a sequence) that allows duplicate
elements. It is part of the Java Collections Framework and is an interface that extends
the Collection interface.
1. Ordered: The elements in a list have a specific order, which means you can access
them based on their index (like arrays).
2. Duplicates Allowed: Lists can contain duplicate elements, unlike some other
collections like Set .
https://stackedit.io/app# 9/17
29/01/2025, 15:49 StackEdit
3. Indexed: Each element in a list has an index (position), starting from 0. This allows
random access to elements.
4. Growable: Lists can grow dynamically, unlike arrays, which have a fixed size once
created.
1. ArrayList: A resizable array that allows fast random access but slower insertions
and deletions, particularly for large lists.
2. LinkedList: A doubly-linked list that allows fast insertions and deletions but slower
random access.
3. Vector: Similar to ArrayList but is synchronized and thread-safe, though it is
rarely used in modern applications.
import java.util.*;
// Modifying elements
fruits.set(1, "Mango");
System.out.println("Modified List: " + fruits);
// Removing elements
fruits.remove("Cherry");
https://stackedit.io/app# 10/17
29/01/2025, 15:49 StackEdit
Output:
List Methods:
https://stackedit.io/app# 11/17
29/01/2025, 15:49 StackEdit
1. Indexing: List elements are indexed starting from 0. So, the first element is at
index 0 , the second at index 1 , and so on.
2. Duplicates: Lists allow duplicate elements, so you can have multiple identical
elements in the list.
3. Order: The order of elements in a list is preserved. If you add elements in a specific
order, they will stay in that order when you retrieve them.
4. Null Values: Lists can contain null values (unless otherwise specified by the
implementation).
import java.util.*;
// Adding elements
colors.add("Red");
colors.add("Green");
colors.add("Blue");
// Removing an element
colors.remove("Green");
System.out.println("After removal: " + colors);
}
}
Output:
Colors in LinkedList:
Red
Green
https://stackedit.io/app# 12/17
29/01/2025, 15:49 StackEdit
Blue
After removal: [Red, Blue]
Use a List when you need an ordered collection of elements that can be
accessed by index.
Use an ArrayList when you need fast random access to elements and are mostly
performing get() or set() operations.
Use a LinkedList when you need to perform frequent insertions and deletions
from the middle of the list or at both ends.
Lists are essential when you need an ordered, indexed collection in Java. They are
versatile and offer various useful methods for manipulating data. Whether you use an
ArrayList for fast access or a LinkedList for efficient insertions, Lists are a
fundamental part of Java programming.
Questions
1. What are the main differences between an array and a linked list?
2. Implement a stack using an array in Java.
3. Implement a queue using LinkedList in Java.
4. How would you reverse a string using a stack?
Conclusion
By mastering OOP, sorting algorithms, and data structures, you will have a solid
foundation for Java programming. Practice is key! Keep coding, challenge yourself with
problems, and explore real-world applications.
https://stackedit.io/app# 13/17
29/01/2025, 15:49 StackEdit
------------------------------------------------------------------------------------
BONUS CONTENT:
The enhanced for-each loop (introduced in Java 5) simplifies the process of iterating
over arrays or collections. It’s often used when you don’t need the index of the
elements, and you just want to iterate through each element in a collection or array.
Syntax:
dataType: The type of the element (like int , String , or custom objects).
element: The current element in the loop.
collectionOrArray: The array or collection you are iterating through.
Key Points:
1. No index required: You don’t have to use an index variable, which makes the code
cleaner.
2. Readability: It’s shorter and more readable when you don’t need the index.
3. Used for collections and arrays: Works for arrays and any class that implements
the Iterable interface (like List , Set , etc.).
https://stackedit.io/app# 14/17
29/01/2025, 15:49 StackEdit
}
}
Output:
10
20
30
40
50
In this example, the loop automatically picks each element from the numbers array and
prints it. There’s no need to manually reference indices ( numbers[i] ), making it simpler.
import java.util.*;
Output:
Apple
Banana
https://stackedit.io/app# 15/17
29/01/2025, 15:49 StackEdit
Cherry
Here, the enhanced for-each loop is used to iterate over a List of String elements. It
automatically handles the iteration, so you don’t need to manage the index.
Benefits:
1. Simplified Code: Reduces the amount of code needed to iterate over arrays or
collections.
2. Prevents Errors: With regular loops, you might accidentally go out of bounds or
modify the index incorrectly, but the enhanced loop avoids such mistakes.
3. Cleaner and More Concise: It’s great for situations where you just need to access
the elements, without caring about their positions in the collection.
Limitations:
1. No Access to Index: You can’t directly access the index of elements. If you need
the index during iteration, the regular for loop would be better.
2. Read-Only: It’s not suitable for modifying the collection or array while iterating, as
it doesn’t allow access to the index directly for updating elements.
The enhanced for-each loop is perfect for situations where you simply need to read the
elements of an array or collection and don’t need to modify the collection or track the
index.
#stuffyaeasy
SOURCES:
I believe all are working links
https://stackedit.io/app# 16/17
29/01/2025, 15:49 StackEdit
https://stackedit.io/app# 17/17