Java

Strings

Strings are everywhere! Learn about the String class, immutability, and StringBuilder.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Strings are everywhere! Learn about the String class, immutability, and StringBuilder. This hands-on tutorial focuses on practical implementation of strings concepts.

Strings

In Java, a String is an object that represents a sequence of characters.

String greeting = "Hello";

String Immutability

Strings in Java are immutable. This means once a String object is created, its data or state cannot be changed.

String s = "Sachin";
s.concat(" Tendulkar"); // concat() appends the string at the end
System.out.println(s); // Will print "Sachin" because strings are immutable

To modify it, you must assign it back:

s = s.concat(" Tendulkar");
System.out.println(s); // "Sachin Tendulkar"

String Methods

Java provides many methods to perform operations on strings.

  • length(): Returns the length of the string.
  • charAt(int index): Returns the character at the specified index.
  • substring(int start, int end): Extracts a part of the string.
  • equals(String another): Compares two strings.

StringBuilder & StringBuffer

If you need to modify strings frequently (like in a loop), using String is inefficient because it creates a new object every time.

Use StringBuilder (faster, not thread-safe) or StringBuffer (thread-safe, slower).

StringBuilder sb = new StringBuilder("Hello");
sb.append(" Java");
System.out.println(sb); // "Hello Java"

Interactive Code

Explore string manipulation!

JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Java String class, immutability and StringBuilder"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

Are Strings mutable in Java?

Yes
No

Next Steps

Strings are great, but what if we have a list of strings? Let's check out Arrays.