String Introduction
Strings are sequences of characters. In Java, strings are objects of the
String class. They are immutable – once created, their value cannot be changed. This immutability is important for security and performance.Creating Strings
String s1 = "Hello"; // string literal (pooled)
String s2 = new String("Hello"); // new object (heap)
String s3 = s1 + " World"; // concatenation
String Pool
String literals are stored in a special memory area called the string pool. This helps reuse strings and save memory.
Important Methods
String str = " Java DSA ";
int len = str.length(); // 11
String trimmed = str.trim(); // "Java DSA"
String upper = str.toUpperCase(); // " JAVA DSA "
char ch = str.charAt(2); // 'J'
int idx = str.indexOf("DSA"); // 6
String sub = str.substring(2, 9); // "Java DSA"
boolean eq = s1.equals(s2); // compare content
boolean eqIgnore = s1.equalsIgnoreCase("hello"); // true
Two Minute Drill
- Strings are immutable sequences of characters.
- String literals are stored in string pool for reuse.
- Useful methods: length(), charAt(), substring(), indexOf(), equals().
- Use equals() for content comparison, not ==.
Need more clarification?
Drop us an email at career@quipoinfotech.com
