Java Strings
Work with text, one operation at a time.
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.
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.
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 / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
String | The String class — note the capital S |
name | Variable name |
"Hello, Java!" | The text value — always wrapped in double quotes |
Common String Methods
length()— number of characterstoUpperCase()/toLowerCase()— change casetrim()— remove leading/trailing whitespacecharAt(i)— character at indexiindexOf(s)— position of first match, or -1contains(s)— returnstrueif match foundsubstring(i)— new string from indexito endreplace(a, b)— swap all occurrences ofawithb
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.
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")); // trueA 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.
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.
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.
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.
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.