HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Strings

Strings

length, toUpperCase, substring, split, and contains methods.

Simple enough for a 5-year-old to understand

Length and uppercase

Example code
1
public class Main {
2
public static void main(String[] args) {
3
System.out.println("hello".length());
7 (method has parentheses)
Output / Input7 (method has parentheses)
4
System.out.println("hello".toUpperCase());
HELLO
Output / InputHELLO
5
}
6
}
Output / Input
💡 Hint: String.length() is a method with parentheses; in arrays, length is a field without parentheses.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the word "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

In Java, a String is an object and is IMMUTABLE: every operation you think modifies a text actually creates a new String. toUpperCase, replace, trim — they all return a new object, and the original text remains untouched.

This immutability is a deliberate choice for security and performance, but it has a side effect: concatenating texts with a plus sign inside a loop creates a new object in every iteration, causing a serious slowdown over thousands of iterations. In this case, StringBuilder is used.

Using equals instead of double equals for String comparison is Java's most fundamental rule. To ignore case sensitivity, there is equalsIgnoreCase.

What you will learn

  • using length, charAt, and substring
  • comparing with equals and equalsIgnoreCase
  • using toUpperCase, trim, and replace methods
  • searching with contains, startsWith, and indexOf
  • splitting text with split
  • performing efficient concatenation with StringBuilder

When to use?

Text operations are used in input validation, parsing file and network data, and generating UI text. When comparing user input, using trim and equalsIgnoreCase together is almost mandatory.

If you are accumulating text in a loop, use StringBuilder; for a few one-off concatenations, the plus sign is completely sufficient and the compiler already optimizes it.

Common mistakes

Wrong

String name = "  Ada  ";
name.trim();
System.out.println(name.length());

trim does not modify the original text; it returns a new trimmed String. Because you didn't assign the result, name still has spaces, and the length turns out larger than you expected.

Correct

String name = "  Ada  ";
name = name.trim();
System.out.println(name.length());

Wrong

String result = "";
for (int i = 0; i < 10000; i++) {
    result += i;
}

Every iteration creates a new String object; ten thousand iterations create ten thousand objects, and the operation slows down noticeably. StringBuilder should be used for concatenation inside loops.

Correct

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++) {
    sb.append(i);
}
String result = sb.toString();

Wrong

String text = "Java";
System.out.println(text.charAt(4));

Indices start at zero; the valid indices of a 4-letter text are from 0 to 3. You will get a "StringIndexOutOfBoundsException" error. For the last character, you need to subtract one from the length.

Correct

String text = "Java";
System.out.println(text.charAt(text.length() - 1));

Frequently asked questions

Why is String immutable?

For security and performance. Because it is immutable, the same text can be safely shared in multiple places, and its hash code can be computed and cached once. Also, when you pass a text to a method, you can be sure that the method won't corrupt it.

When is StringBuilder necessary?

When accumulating text inside a loop. It is not needed for a few one-off concatenations; the compiler already converts the plus sign into a StringBuilder. The difference appears when the number of concatenations exceeds the hundreds.

Why is the split method not working as expected?

Because split expects a regular expression, not plain text. Characters like dots, asterisks, and pipes carry special meaning in regular expressions; to split by them, you need to escape them or use Pattern.quote. This is the most common issue faced by those wanting to split by a dot.

Summary

In Java, Strings are immutable: every method returns a new object, and you need to assign the result. Comparisons use equals, and StringBuilder is preferred for concatenation inside loops.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code