Six Strings in Java — Detailed Explanation (Concept + Internal Working + Methods)
1. What exactly is a String?
Till now, you have seen:
char c = 'A';
This stores only one character.
Now suppose you want to store:
"Aman"
You cannot use char for this.
So Java provides:
String name = "Aman";
👉 A String is a sequence of characters
2. Very Important Concept (Do not skip)
String is NOT a primitive type
It is a:
Non-primitive (object / class)
That means:
It does not store value directly
It stores reference (address)
3. How String is Stored in Memory
When you write:
String s = "Hello";
Internally:
s → "Hello"
But actually:
"Hello" is stored in a special area called String Pool
s stores its reference
4. String Immutability (Very Important Concept)
Strings in Java are immutable
👉 Meaning: cannot be changed
Example:
String s = "Hello";
s = s + " World";
You may think it modifies the same string.
But actually:
"Hello" → remains unchanged
New String → "Hello World"
👉 A new object is created
5. Ways to Create String
(1) Using literal (recommended)
String s = "Hello";
(2) Using new keyword
String s = new String("Hello");
Difference:
Literal → stored in String Pool
new → stored in Heap (separate memory)
6. String Comparison (Most Important for Exams)
❌ Using ==
String a = "Hello";
String b = "Hello";
[Link](a == b); // true
👉 Why true?
Both point to same object in String Pool
But:
String a = new String("Hello");
String b = new String("Hello");
[Link](a == b); // false
👉 Different memory locations
Correct Way → .equals()
[Link]([Link](b)); // true
👉 Compares content, not reference
7. Important String Methods
(1) length()
String s = "Hello";
[Link]([Link]()); // 5
(2) charAt()
String s = "Hello";
[Link]([Link](1)); // e
👉 Index starts from 0
(3) substring()
String s = "Hello";
[Link]([Link](1, 4)); // ell
👉 from index 1 to 3
(4) concat()
String s1 = "Hello";
String s2 = "World";
[Link]([Link](s2)); // HelloWorld
(5) toUpperCase() / toLowerCase()
String s = "hello";
[Link]([Link]()); // HELLO
(6) equals()
String a = "Java";
String b = "Java";
[Link]([Link](b)); // true
(7) compareTo() (Important)
String a = "Apple";
String b = "Banana";
[Link]([Link](b));
👉 Returns:
0 → equal
negative → a < b
positive → a > b
8. Taking String Input (Important)
Scanner sc = new Scanner([Link]);
String name = [Link]();
Difference:
next() → single word
nextLine() → full line
9. Common Mistakes
❌ Using == for comparison
if (a == b) // WRONG
❌ Index out of bounds
[Link](10); // ERROR
❌ Forgetting immutability
[Link](); // does not change original
Correct:
s = [Link]();
10. Real Understanding
Whenever you see String, think:
It is an object
Stored in String Pool
Immutable
Use .equals() for comparison
11. Mini Practice
1. Take a string and print its length
2. Print first and last character
3. Reverse a string (basic logic)
4. Compare two strings
Next
👉 Arrays (very important — base for DSA and exams)
Tell me to continue.
continue
Arrays in Java — Detailed Explanation (Concept + Memory + Usage)
1. Why Arrays are Needed
Suppose you want to store marks of 5 students:
int m1 = 90;
int m2 = 85;
int m3 = 78;
int m4 = 92;
int m5 = 88;
This becomes:
Hard to manage
Not scalable
Repetitive
So Java provides Arrays
2. What is an Array?
An array is a collection of similar data types stored in contiguous memory locations.
👉 Key idea:
Same type
Indexed access
Continuous memory
3. How Array Works (Important Visualization)
int[] arr = {10, 20, 30, 40};
Memory:
Index: 0 1 2 3
-------------------
arr → 10 20 30 40
👉 Index always starts from 0
4. Declaration of Array
int[] arr;
👉 Only reference created, no memory yet
5. Memory Allocation
arr = new int[5];
👉 Now memory is created for 5 elements
6. Combined Form
int[] arr = new int[5];
7. Initialization
arr[0] = 10;
arr[1] = 20;
Shortcut Initialization
int[] arr = {10, 20, 30, 40};
8. Accessing Elements
[Link](arr[2]); // 30
9. Looping Through Array
Using for loop
for (int i = 0; i < [Link]; i++) {
[Link](arr[i]);
}
Using for-each loop
for (int x : arr) {
[Link](x);
}
👉 Cleaner and simpler
10. Important Property
[Link]
👉 Gives size of array
11. Default Values (Very Important)
When array is created:
int[] arr = new int[3];
Values are:
0 0 0
Defaults:
int → 0
double → 0.0
boolean → false
String → null
12. Important Concept — Array is Object
int[] arr = new int[5];
👉 Array is stored in heap memory
Variable arr stores:
👉 reference (address)
13. Common Errors
❌ Index Out of Bounds
arr[5] = 10; // ERROR
👉 Max index = size - 1
❌ Uninitialized array
int[] arr;
arr[0] = 10; // ERROR
14. Types of Arrays
(1) Single Dimensional
int[] arr = {1, 2, 3};
(2) Multi-Dimensional (2D Array)
int[][] arr = new int[2][3];
2D Array Visualization
[ [1, 2, 3],
[4, 5, 6] ]
Example
int[][] arr = {
{1, 2, 3},
{4, 5, 6}
};
Traversing 2D Array
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
15. Real Understanding
Whenever you see array:
Fixed size
Same data type
Indexed access
Stored in heap