Chapter 7Java Tutorial~1 min read
Strings in Java
Strings — Java चा String Class
Java मध्ये String एक class आहे — primitive नाही. Strings immutable आहेत — एकदा बनवले की बदलता येत नाही, नवी String object बनते. String साठी double quotes, char साठी single quotes.
Marathi Analogy
Java String म्हणजे cement मध्ये कोरलेले नाव — एकदा कोरले की बदलता येत नाही (immutable). String बदलायचे असेल तर नवा cement block (नवी String object) बनवतात. StringBuilder म्हणजे whiteboard — बदलता येतो.
String बनवणे आणि Concatenation
String creation
java
// String literal (String pool मध्ये)
String name = "Rahul";
String city = "Pune";
// new keyword (heap मध्ये — avoid करा)
String s = new String("Hello");
// Concatenation
String full = name + " from " + city;
System.out.println(full); // Rahul from Pune
// concat() method
String greeting = "नमस्कार ".concat(name);
System.out.println(greeting); // नमस्कार Rahul
// String + number — auto convert होतो
int age = 20;
System.out.println(name + " is " + age + " years old.");String Methods
Common String methods
java
String s = " Hello World ";
// Length
System.out.println(s.length()); // 15
// Case
System.out.println(s.toUpperCase()); // " HELLO WORLD "
System.out.println(s.toLowerCase()); // " hello world "
// Trim
System.out.println(s.trim()); // "Hello World"
// Substring
String text = "Hello World";
System.out.println(text.substring(6)); // "World"
System.out.println(text.substring(0, 5)); // "Hello"
// indexOf, contains, replace
System.out.println(text.indexOf("World")); // 6
System.out.println(text.contains("Hello")); // true
System.out.println(text.replace("World", "Java")); // Hello Java
// Split
String csv = "Pune,Mumbai,Nagpur";
String[] cities = csv.split(",");
for (String c : cities) System.out.println(c);
// charAt, compareTo
System.out.println(text.charAt(0)); // H
System.out.println("abc".compareTo("abc")); // 0 — equalString Comparison
== vs .equals() — important!
java
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
// == compares references (memory address)
System.out.println(s1 == s2); // true (String pool)
System.out.println(s1 == s3); // false! (different objects)
// .equals() compares content — HE VAAPARAA!
System.out.println(s1.equals(s3)); // true ✅
System.out.println(s1.equalsIgnoreCase("hello")); // trueStringBuilder — Mutable String
StringBuilder — efficient string building
java
// Loop मध्ये String concatenate करायला StringBuilder वापरा
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) {
sb.append("Item ").append(i).append("\n");
}
sb.insert(0, "--- Shopping List ---\n");
sb.delete(sb.length() - 1, sb.length()); // last \n काढा
System.out.println(sb.toString());
System.out.println("Length: " + sb.length());✅ Key Points — लक्षात ठेवा
- ▸String immutable — बदलले की नवी object बनते
- ▸== reference compare, .equals() content compare
- ▸.length(), .substring(), .replace(), .split() — common methods
- ▸.toUpperCase(), .toLowerCase(), .trim() — formatting
- ▸StringBuilder — mutable, loop मध्ये efficient
0/11 chapters पूर्ण