Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 14

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.

💡 String is a class, so it comes with methods like length(), substring() and toUpperCase().

Creating strings

String a = "Hello";               // literal (preferred)
String b = new String("World");   // with new (rarely needed)
System.out.println(a + " " + b);
Hello World

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
12
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
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)
true
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);
CodingEasily

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. Use equals() for content comparison.
  • Expecting a method to change the string. Strings are immutable — reassign the result.
  • Using length without brackets. Strings use length() (a method), arrays use length.
  • 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 String is rarely needed.
  • Strings are immutable — methods return a new String.
  • Compare content with equals(), not ==.
  • Join with + or concat(); 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?
A String is an object that holds a sequence of characters, like "Hello". It is a reference type (a class), written with a capital S, and comes with many built-in methods such as length(), substring() and toUpperCase().
Why are strings immutable in Java?
Immutable means a String cannot be changed after it is created. Methods like toUpperCase() return a new String instead of changing the original. This makes strings safe to share and helps performance through a string pool.
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?
Use the 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?
Use the + operator or the concat() method: "a" + "b" or "a".concat("b"). For joining many strings in a loop, a StringBuilder is more efficient.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMethods in Java Your progress: 14 / 61 (23%) · Lesson 15 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free