Ever wondered why a piece of text in your program behaves like a magic rope you can stretch, cut, or join?
In simple words, a String in Java is just a line of characters – like a sentence – that the computer stores for you. You can read it, compare it, or combine it with other strings, but the original line stays exactly the same.
What is a String in Java?
A String is a sequence of characters, such as letters, numbers, or symbols. Think of it as a train of LEGO blocks where each block is a character. Once the train is built, you can’t pull out a block without building a new train – that’s what we call immutable (unchangeable). The computer keeps the original train safe while you create new ones.
How to create Strings
You can make a String in three common ways:
- Using double quotes:
String name = "Aven"; - Using the
newkeyword:String greeting = new String("Hello"); - From a character array:
char[] letters = {'J','a','v','a'}; String word = new String(letters);
All three give you a ready‑to‑use string object.
Common String methods you must know
Methods are actions you can ask a String to perform. Here are the ones that pop up in ICSE exams:
| Method | What it does |
|---|---|
length() | Returns the number of characters (like counting beads on a necklace). |
charAt(int index) | Gives the character at a specific position; index starts at 0 (like picking the third bead). |
substring(int start, int end) | Creates a new string from a part of the original; think of cutting a slice from a loaf of bread. |
toUpperCase() | Changes all letters to capital ones. |
equals(Object obj) | Checks if two strings have exactly the same characters. |
compareTo(String another) | Compares two strings alphabetically; returns a number indicating order. |
trim() | Removes leading and trailing spaces, like trimming the edges of a photograph. |
String concatenation – joining strings together
Concatenation means sticking strings side by side. The easiest way is the + operator:
String first = "Hello";
String second = "World";
String combined = first + " " + second; // "Hello World"
Behind the scenes, Java actually creates a new String because the original ones stay unchanged (remember the LEGO train analogy).
Why use StringBuilder for large text?
If you need to add or delete characters many times, creating a new String each time becomes slow. StringBuilder is a mutable (changeable) class – like modeling clay that you can reshape without starting over.
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.append(" ");
sb.append("Rocks");
String result = sb.toString(); // "Java Rocks"
When the work is done, call toString() to get a regular String.
Typical exam style question and solution
Write a program that reads a sentence and prints the number of vowels it contains.
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String line = sc.nextLine();
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i Notice the use of length(), charAt(), and indexOf() – all classic String tools.
Quick recap – bullet summary
- String = immutable sequence of characters.
- Create with quotes,
new, or char array. - Key methods:
length(),charAt(),substring(),equals(),compareTo(),trim(). - Use
+for simple concatenation. - Choose
StringBuilderwhen you modify text many times.
📝 Likely Exam Questions
- Explain why strings are immutable in Java.
Answer: Once a String object is created its characters cannot be changed. Any operation that seems to modify it actually creates a new String, keeping the original safe and allowing string sharing. - Write the output of the following code:
Answer:String s = "Hello"; s += " World"; System.out.println(s);Hello World(the+=creates a new String that combines the two parts). - What method would you use to compare two strings ignoring case?
Answer:equalsIgnoreCase(String anotherString)checks equality without caring about upper‑ or lower‑case letters. - Given
String txt = " Java ";, show how to obtain"Java"without spaces.
Answer:String clean = txt.trim(); - When should you prefer StringBuilder over String?
Answer: When you need to modify the text many times (e.g., in loops) because StringBuilder avoids creating many temporary String objects, making the program faster.