Strings in Java
In this lesson: Most-used Java String methods — length, charAt, substring, equals, split — and why strings are immutable. In Hindi & English.
What is a String
A String holds text — a sequence of characters like "Hello". In Java, String is a class (written with a capital S), so it is a reference type with many useful built-in methods. Strings are one of the most-used types in any program.
Creating strings
String a = "Hello"; // literal (preferred)
String b = new String("World"); // with new (rarely needed)
System.out.println(a + " " + b);
The usual way is a string literal in double quotes. Using new String(...) also works but is rarely needed and makes an extra object.
Common string methods
String s = "CodingEasily";
System.out.println(s.length()); // 12
System.out.println(s.toUpperCase()); // CODINGEASILY
System.out.println(s.substring(0, 6)); // Coding
System.out.println(s.charAt(0)); // C
System.out.println(s.indexOf("Easy")); // 6
CODINGEASILY
Coding
C
6
length() gives the count, substring() extracts part, charAt() gets one character, and indexOf() finds a position. There are many more.
Strings are immutable
String s = "hello"; s.toUpperCase(); // does NOT change s System.out.println(s); // still hello s = s.toUpperCase(); // reassign to keep the result System.out.println(s); // HELLO
HELLO
A String cannot be changed after creation. Methods like toUpperCase() return a new String, leaving the original untouched. You must reassign to keep the result.
Comparing strings
String a = "hi";
String b = "hi";
String c = new String("hi");
System.out.println(a.equals(c)); // true (same text)
System.out.println(a == c); // false (different objects)
false
Use equals() to compare text content. == compares references (whether they are the same object), which is usually not what you want for strings.
Joining strings
String first = "Coding";
String full = first + "Easily"; // with +
String also = first.concat("Easily"); // with concat
System.out.println(full);
The + operator and concat() both join strings. For building a long string inside a loop, prefer a StringBuilder for better performance.
Common mistakes
- Using
==to compare text. Useequals()for content comparison. - Expecting a method to change the string. Strings are immutable — reassign the result.
- Using
lengthwithout brackets. Strings uselength()(a method), arrays uselength. - Out-of-range substring. Indexes must be within the string, or you get an error.
Summary
- A String is a class holding text, with many built-in methods.
- Create with a literal in double quotes;
new Stringis rarely needed. - Strings are immutable — methods return a new String.
- Compare content with
equals(), not==. - Join with
+orconcat(); use StringBuilder for heavy building.
Next, learn efficient editing with String vs StringBuffer vs StringBuilder, or reverse text in reverse a string.
Frequently Asked Questions
What is a String in Java?
Why are strings immutable in Java?
What is the difference between == and equals() for strings?
== checks if two references point to the same object, while equals() checks if the text content is the same. To compare string values, always use equals(), not ==.How do I find the length of a String?
length() method: str.length(). Note it has brackets, unlike arrays which use length as a field with no brackets.How do I join two strings?
+ operator or the concat() method: "a" + "b" or "a".concat("b"). For joining many strings in a loop, a StringBuilder is more efficient.