Java Strings & Special Characters

What Is a String?

In Java, a String is a built-in object that stores a sequence of characters.

Loading...
Output:

String Immutability

Strings in Java are immutable. Any operation on a string returns a new object.

Loading...
Output:

String Concatenation

You can join strings using the + operator:

Loading...
Output:

Common String Methods

  • .length() – returns number of characters
  • .toUpperCase(), .toLowerCase()
  • .indexOf() – returns index of first match
  • .equals() – compares content
  • .contains() – checks if substring is present
Loading...
Output:

Escape Characters

Use escape sequences to include special characters inside strings:

  • \\n – new line
  • \\t – tab
  • \\\" – double quote
  • \\\\ – backslash
Loading...
Output:

Comparing Strings

Use equals() instead of == to compare string values:

Loading...
Output:

Practice Question

What will this print?

A. true false
B. false true
C. true true
Loading...
Output:

Hint: Java interned strings with literals.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic