Java Strings
Java Strings
1 Introduction to Strings
• In Java, strings are stored in a special memory area called the String
Constant Pool (SCP).
String s1 = "Hello";
String s2 = "Hello"; // s1 and s2 both refer to the same object in SCP
- Strings are widely used in file handling, networking, and user input.
5. Important String Methods with Examples
Method: length()
Output: 5
Method: charAt()
Output: World
Method: contains()
Output: true
Method: equals()
String a = "Java";
String b = "Java";
System.out.println(a.equals(b));
Output: true
Method: equalsIgnoreCase()
String a = "Java";
String b = "java";
Output: true
System.out.println(a.equalsIgnoreCase(b));
Method: toLowerCase()
Output: hello
Method: toUpperCase()
Output: HELLO
Method: trim()
Output: Hello
Method: replace()
Output: bonono
Method: split()
Output: b
Method: indexOf()
Output: 10
Method: startsWith()
Output: true
Method: endsWith()
Output: true