Java String Compiler

Strings are immutable in Java. Test your understanding with these snippets — palindrome checks, StringBuilder loops, splits, and more — all runnable in the browser.

Run Java String Compiler examples in the Java compiler

Examples included

Practical tips

  • Use s.equals(t) for content comparison. == compares references and gives false positives via string interning.
  • StringBuilder for any concatenation in a loop. String "+" in a loop is O(N²).
  • String.chars() gives an IntStream of code points — useful for functional transforms.

Common questions

How do I reverse a string in Java?

Use new StringBuilder(s).reverse().toString(). The example above is editable and runnable.

Why is == wrong for strings?

It checks reference equality. Two strings with the same content can be different objects. Always use .equals().

Practice related Java exercises · Browse Java programs with output