- A String is a sequence of characters (like text).
- In Java, String is a class in
java.langpackage. - Strings are immutable → once created, they cannot be changed (new object is created if you modify it).
Example:
String s = "Hello";Internally memory me ye aise store hota hai:
H e l l o
Yani:
char[] arr = {'H','e','l','l','o'};But difference ye hai ki String ek object hai, normal array nahi.
Immutable matlab — ek baar string ban gayi, uske characters change nahi kar sakte.
- Jab tum likhte ho
String s = "Hello";, to Java usse String Pool me store karta hai. - Agar koi aur likhta hai
String s2 = "Hello";, to wo same memory address use karega. (Java nayi memory allocate nahi karta)
Agar string mutable hoti, to ek change dusre variable me bhi reflect ho jata — jo galat hota.
Example:
String s1 = "Java";
String s2 = "Java"; // same memory pool location
s1 = s1 + "Lang"; // new object banta haiYani s1 ab ek new object point karta hai,
"JavaLang" ke liye ek nayi memory allocate hoti hai.
Java memory ke andar ek String Constant Pool (SCP) hota hai. Ye heap ke andar ka ek special area hai jaha unique strings store hoti hain.
Example:
String a = "Hello";
String b = "Hello";Dono ka reference same hoga, kyunki "Hello" already pool me tha.
Java firse naya object nahi banata.
Lekin:
String c = new String("Hello");Ye heap me alag object banata hai, pool me nahi.
Ye reference address check karta hai (yaani dono same memory location point karte hain ya nahi).
String a = "Hello";
String b = "Hello";
System.out.println(a == b); // true (same pool object)String x = new String("Hello");
String y = new String("Hello");
System.out.println(x == y); // false (different objects)Ye content (value) compare karta hai, address nahi.
System.out.println(x.equals(y)); // trueWorking internally:
public boolean equals(Object obj) {
if (this == obj) return true; // same object
if (obj instanceof String) {
String another = (String) obj;
int n = value.length;
if (n == another.value.length) {
for (int i = 0; i < n; i++) {
if (value[i] != another.value[i]) return false;
}
return true;
}
}
return false;
}Matlab Java internally character by character comparison karta hai.
Lexicographical (dictionary order) me compare karta hai.
Internally working:
- Har character ka Unicode (ASCII) value check hota hai.
- Jaha difference milta hai, wahin comparison stop hota hai.
Example:
"hello".compareTo("chello");Step by step:
- 'h' (104) – 'c' (99) = +5
positive →
"hello"bada hai"chello"se.
Returns number of characters. Internally:
public int length() {
return value.length; // value[] = char array
}Particular position ka character deta hai.
Internally:
public char charAt(int index) {
return value[index];
}Original string ka ek part (new object) banata hai.
Example:
"HelloWorld".substring(0,5); // "Hello"Internally:
- Naya character array banata hai
[0..5)tak ka part leke - Original string ko modify nahi karta
Dono strings ko jodta hai, aur ek new String object banata hai.
"Hello".concat("World"); // new object "HelloWorld"Har character ya substring ko replace karta hai. Internally ek loop chalake new string banata hai.
Remove karta hai leading aur trailing spaces (beech ke spaces nahi).
Example:
" Java ".trim(); // "Java"String ko array of strings me todta hai (regex ke base pe).
String s = "A,B,C";
String[] arr = s.split(",");Internally ye regex pattern se matching karke ek array return karta hai.
Character ke Unicode values convert karta hai:
- 'a' (97) → 'A' (65)
- 'b' (98) → 'B' (66)
String me substring present hai ya nahi check karta hai.
Internally indexOf(seq) != -1 check karta hai.
Regex ke pattern ke against pura string match karta hai.
Ye internally Pattern aur Matcher class use karta hai.
String immutable hone ki wajah se har modification nayi memory banata hai.
Example:
String s = "Hello";
s = s + "World"; // new object banta haiUse StringBuilder ya StringBuffer.
Ye dono internally ek char[] array rakhte hain, jisme content change hota hai bina naya object banaye.
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Hello WorldInternally:
- Same char[] array me “ World” characters add ho jate hain.
- Memory efficient & fast.
Simplified internal structure:
public final class String implements java.io.Serializable, Comparable<String> {
private final char value[]; // actual character array
private final int hash; // cache for hashcode
}value[]→ store karta hai charactershash→ ek baar calculate hone ke baad cache hota hai (performance ke liye)
| Concept | Description | Working |
|---|---|---|
| Immutable | Once created, can’t be changed | New object created on change |
| == | Compare memory address | Same reference or not |
| equals() | Compare content | Character by character |
| compareTo() | Dictionary order | Unicode subtraction |
| concat() | Join strings | Creates new object |
| substring() | Part of string | Creates new object |
| length() | Returns size | value[].length |
| trim() | Remove spaces | Iterates from both ends |
| split() | Divide by regex | Returns string array |
| toUpperCase() | Convert to upper case | Unicode conversion |
| StringBuilder | Mutable version | Changes same object |
| String Pool | Memory optimization | Reuses literals |