ICSE Class 10 – Java String Notes
1. What is a String?
A String is a sequence of characters. In Java, String is a class present in the [Link]
package. Strings are objects, not primitive data types.
Example:
String name = "ICSE";
2. Creating Strings
(a) Using String literal
String s1 = "Computer";
• Stored in String Constant Pool.
• If the same value already exists, memory is reused.
(b) Using new keyword
String s2 = new String("Computer");
• Creates a new object in heap memory.
3. Characteristics of String
• Strings are immutable (cannot be changed).
• Any modification creates a new String object.
Example:
String s = "Java";
s = s + " ICSE";
4. Commonly Used String Methods
• length() – returns number of characters
• charAt(index) – returns character at given index (index starts from 0)
• toUpperCase() – converts string to uppercase
• toLowerCase() – converts string to lowercase
• equals() – compares content of two strings
• compareTo() – compares strings lexicographically
• substring() – extracts part of a string
• indexOf() – returns index of a character or substring
5. equals() vs ==
equals() compares the content of strings.
== compares the reference (memory address).
Example:
String a = "ICSE";
String b = new String("ICSE");
[Link](b) // true
a == b // false
6. Important Points for ICSE Exams
• Index of string starts from 0.
• String is immutable.
• Use equals() to compare strings.
• String class is in [Link] package (no import required).