Java Strings

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 10 / 50 · English

Java Strings

Work with text, one operation at a time.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Strings.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

A String holds text. String objects are immutable: operations such as toUpperCase return another String rather than changing the original object. Use equals for content comparisons, and check the bounds before requesting a substring.

02 / Step by step

Explore the details

Java Strings

A String stores text — any sequence of characters surrounded by double quotes. Unlike primitive types, String is an object with built-in methods you can call on it.

Declaring a String

Code anatomy / கோடின் விளக்கம்

CodeMeaning
StringThe String class — note the capital S
nameVariable name
"Hello, Java!"The text value — always wrapped in double quotes

Common String Methods

  • length() — number of characters
  • toUpperCase() / toLowerCase() — change case
  • trim() — remove leading/trailing whitespace
  • charAt(i) — character at index i
  • indexOf(s) — position of first match, or -1
  • contains(s) — returns true if match found
  • substring(i) — new string from index i to end
  • replace(a, b) — swap all occurrences of a with b

String indexes start at 0, just like arrays. The first character is at index 0, the second at index 1, and so on. Use charAt(0) to get the first character.

Java reference fragmentJAVA
String s = "Hello, Java!";
System.out.println(s.length());           // 12
System.out.println(s.toUpperCase());      // HELLO, JAVA!
System.out.println(s.substring(7, 11));   // Java
System.out.println(s.contains("Java"));   // true
System.out.println(s.replace("Java", "World")); // Hello, World!
System.out.println(s.charAt(0));          // H

// Comparing — always use .equals()
String a = "hello", b = "hello";
System.out.println(a.equals(b));                  // true ✓
System.out.println(a.equalsIgnoreCase("HELLO")); // true
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        String topic = "Java";
        System.out.println(topic.toUpperCase());
        System.out.println(topic);
        System.out.println(topic.equals("Java"));
        System.out.println(topic.length());
    }
}
Predict first. Then reveal the expected output / வெளியீடு
JAVA
Java
true
4

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Store the result of toUpperCase back into topic and print it again.

Reveal the answer and reasoning / விடை

Use topic = topic.toUpperCase();. Subsequent reads of topic now refer to the uppercase String.

One mistake to avoid: == checks object-reference identity for Strings. It is not the usual way to compare their text.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.