Strings

A String in Java is a sequence of characters, backed by the java.lang.String class. Even though String is a reference type (an object), Java lets you create one using a simple literal — just like a primitive — which makes strings easy to work with from day one.

Creating a String

public class StringDemo {
  public static void main(String[] args) {
    String name = "Sonu";              // literal - most common way
    String name2 = new String("Sonu");  // object form - rarely needed

    System.out.println(name);
  }
}

In practice, you'll almost always use the literal form (String s = "text";). Java quietly reuses identical literals internally, which we'll see matters when comparing strings below.

Strings Are Immutable

Once a String object is created, its contents can never change. Every method that looks like it "modifies" a string — toUpperCase(), substring(), replace(), etc. — actually returns a brand new String, leaving the original untouched.

String s = "hello";
s.toUpperCase();  // return value is thrown away!
System.out.println(s);  // hello - s never changed

String upper = s.toUpperCase();  // capture the new string
System.out.println(upper);  // HELLO
Gotcha: Calling s.toUpperCase(); on its own line does nothing useful — the original s is unchanged because strings are immutable. You must assign the result to a variable to keep it.

Comparing Strings: == vs .equals()

This trips up almost every Java beginner. == checks whether two variables point to the same object in memory. .equals() checks whether two strings have the same characters. For strings, you almost always want .equals().

String a = "hello";
String b = "hello";
String c = new String("hello");

System.out.println(a == b);          // true - same literal, reused
System.out.println(a == c);          // false - c is a different object
System.out.println(a.equals(c));   // true - same characters
Rule of thumb: Never use == to compare String content. Always use .equals() (or .equalsIgnoreCase()).

Common String Methods

MethodWhat it does
.length()Returns the number of characters in the string
.charAt(i)Returns the character at index i
.substring(i)Returns a new string starting at index i
.indexOf(text)Returns the index of the first match, or -1 if not found
.toUpperCase() / .toLowerCase()Returns a new string in upper/lower case
String s = "Hello World";

System.out.println(s.length());          // 11
System.out.println(s.charAt(0));         // H
System.out.println(s.substring(6));      // World
System.out.println(s.indexOf("World"));  // 6
System.out.println(s.toUpperCase());    // HELLO WORLD

String Concatenation

Use the + operator to join strings together. Java automatically converts non-string values (numbers, booleans) to text when they're combined with a string.

String first = "Cat";
String second = "alog";
String result = first + second;  // "Catalog"

int age = 21;
System.out.println("Age: " + age);  // Age: 21

StringBuilder for Efficient String Building

Because strings are immutable, concatenating inside a loop with + creates a new string object on every iteration — wasteful for large loops. StringBuilder is a mutable buffer that lets you build up text efficiently, then convert it to a String once at the end.

StringBuilder sb = new StringBuilder();

for (int i = 0; i < 5; i++) {
  sb.append(i).append(",");
}

System.out.println(sb.toString());  // 0,1,2,3,4,
Tip: Use StringBuilder whenever you're building a string piece-by-piece inside a loop. For a handful of one-off concatenations, plain + is perfectly fine and more readable.
Next up: Now that you can work with text, head to Methods to learn how to package reusable blocks of logic into your own functions.