Why Strings Matter in Java Programming

Ever typed a message on your phone and wondered how the phone actually stores each letter? In Java, that “message” is a String – a sequence of characters that the computer treats as one piece of data.

💡 In Simple Words: A String is just text wrapped in double quotes, like "Hello". Java lets you create, change, and examine that text using built‑in tools, so you don’t have to write the low‑level code yourself.

How to Create a String

You can make a String in two common ways:

  • Direct assignment with double quotes: String name = "Aven";
  • Using the new keyword: String name = new String("Aven");

Both give you a piece of text you can work with. The first style is shorter and is what most programmers prefer.

Common String Operations You’ll Need for the ICSE Exam

Java ships with a toolbox called the String class. Each tool (method) does a specific job. Below is a quick cheat‑sheet.

MethodWhat it does
length()Returns the number of characters in the string.
charAt(int index)Gives the character at the given position (positions start at 0).
substring(int start, int end)Extracts a part of the string from start (inclusive) to end (exclusive).
toUpperCase()Changes all letters to capital letters.
toLowerCase()Changes all letters to small letters.
trim()Removes spaces at the beginning and end of the string.
replace(char old, char new)Swaps every occurrence of old with new.
contains(String seq)Checks whether the string includes the given sequence of characters.
equals(String other)Tests if two strings have exactly the same characters.

Step‑by‑Step Example: Cleaning Up User Input

Imagine you ask a friend to type their name, but they accidentally add a space before or after it and use mixed case. You want the name in all caps without extra spaces. Here’s how you can do it.

String raw = "  aVen  ";
String cleaned = raw.trim().toUpperCase();
System.out.println(cleaned);   // prints AVEN

First trim() throws away the surrounding blanks, then toUpperCase() flips every letter to capital.

graph TD\nA[Start] --> B[Read raw string] --> C[Trim spaces] --> D[Convert to uppercase] --> E[Print result] --> F[End]

Why Strings Are Immutable (and Why That’s Good)

In Java, once you create a String, you cannot change the original characters. Think of a String like a sealed envelope – you can’t edit the letter inside, but you can create a new envelope with the edited text. This “immutability” makes programs safer because many parts of the code can share the same String without worrying that someone else will alter it behind their back.

Practical Tips for the ICSE Exam

  • Remember that indices start at 0. charAt(0) gives the first character.
  • Use equals() to compare two strings, not the == operator, which only checks if they are the same object.
  • Chain methods when you need several changes: str.trim().toLowerCase().replace('a','@').
  • When a question asks for “the length of a string”, call myString.length() – note the parentheses.

📝 Likely Exam Questions

  1. Write a Java program that reads a line of text, removes leading/trailing spaces, converts it to lowercase, and prints the result.
    import java.util.Scanner;
    public class CleanInput {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            System.out.println(line.trim().toLowerCase());
        }
    }
    
  2. What will be the output of the following code?
    String s = "HelloWorld";
    System.out.println(s.substring(5,10));
    
    Answer: World (characters from index 5 up to, but not including, index 10).
  3. Explain the difference between == and equals() when comparing strings.
    Answer: == checks whether both references point to the exact same object in memory, while equals() checks whether the characters inside the two strings are identical.
  4. Given String s = " Java ", write a single line of code to print “JAVA” without spaces.
    Answer: System.out.println(s.trim().toUpperCase());
  5. List three String methods that can be used to modify a string and briefly describe each.
    Answer:
    • trim() – removes leading and trailing spaces.
    • toUpperCase() – converts all letters to uppercase.
    • replace(old,new) – substitutes every occurrence of old with new.
#ICSE#Class 10#Java#String#Computer Applications