1. String Class Basics in Java
In Java, a String is used to store text data such as names, messages, emails, and sentences.
The String class is available inside the java.lang package, so it can be used directly without importing anything.
2. Why String is Used
Strings are used whenever a program needs to work with characters or text.
- a. Store user names
- b. Store passwords
- c. Display messages
- d. Handle file names
- e. Process input data
3. Where Strings are Used
Strings are used in almost every Java application.
- a. Web applications
- b. Android applications
- c. Banking systems
- d. Login systems
- e. Chat applications
- f. File processing systems
4. Creating Strings in Java
There are two common ways to create a String in Java.
4.1 Using String Literal
A string literal means directly writing text inside double quotes.
public class Main {
public static void main(String[] args) {
String city = "Bhubaneswar";
System.out.println(city);
}
}
Here, the value "Bhubaneswar" is stored inside the variable named city.
4.2 Using new Keyword
A String object can also be created using the new keyword.
public class Main {
public static void main(String[] args) {
String language = new String("Java");
System.out.println(language);
}
}
Here, a new String object is created manually using the constructor.
Creating Strings with the new keyword uses extra memory because a new object is created every time.
5. Understanding String Objects
A String in Java is actually an object, not a primitive data type.
Even though it looks simple, String internally contains methods and functionality for text processing.
5.1 String is a Class
public class Main {
public static void main(String[] args) {
String text = "Java";
System.out.println(text.length());
}
}
The length() method belongs to the String class. It returns the total number of characters.
5.2 Character Indexing
Each character inside a String has an index position.
| Character | J | a | v | a |
| Index | 0 | 1 | 2 | 3 |
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.charAt(0));
System.out.println(word.charAt(2));
}
}
charAt() returns the character present at a specific index.
String indexing always starts from 0 in Java.
6. Common Basic String Operations
6.1 Finding String Length
public class Main {
public static void main(String[] args) {
String message = "Hello";
int size = message.length();
System.out.println(size);
}
}
The length() method counts all characters including spaces.
6.2 Converting to Uppercase
public class Main {
public static void main(String[] args) {
String course = "java";
System.out.println(course.toUpperCase());
}
}
toUpperCase() converts all characters into capital letters.
6.3 Converting to Lowercase
public class Main {
public static void main(String[] args) {
String country = "INDIA";
System.out.println(country.toLowerCase());
}
}
toLowerCase() converts all characters into lowercase letters.
6.4 Concatenating Strings
Concatenation means joining multiple Strings together.
public class Main {
public static void main(String[] args) {
String firstName = "Rahul";
String lastName = "Sharma";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
}
}
The + operator joins Strings into a single String.
7. Internal Working of String in Java
Java handles Strings differently from normal objects to improve performance and memory usage.
7.1 String Objects are Stored in Memory
When a String is created, Java stores it inside memory.
Java uses a special memory area called the String Pool for string literals.
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
}
}
Both variables point to the same String object from the String Pool, so the result is true.
Java reuses String literals to save memory.
7.2 Strings Created with new Keyword
Strings created using the new keyword always create separate objects.
public class Main {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
Here, two different objects are created, so the result is false.
7.3 Understanding String Immutability
In Java, Strings are immutable.
Immutable means the original String cannot be changed after creation.
public class Main {
public static void main(String[] args) {
String text = "Java";
text.concat(" Programming");
System.out.println(text);
}
}
The original String remains unchanged because concat() creates a new String instead of modifying the old one.
public class Main {
public static void main(String[] args) {
String text = "Java";
text = text.concat(" Programming");
System.out.println(text);
}
}
Now the new String is stored back into the variable, so the updated value is displayed.
Many beginners think String methods change the original object directly. Most String methods actually return a new String.
8. Common Beginner Mistakes
8.1 Using == Instead of equals()
The == operator compares memory references, not actual text values.
public class Main {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
The first comparison checks object references. The second comparison checks actual content.
Use equals() when comparing String values.
8.2 Accessing Invalid Index
Trying to access an invalid character index causes an error.
public class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.charAt(10));
}
}
This code throws StringIndexOutOfBoundsException because index 10 does not exist.
8.3 Forgetting Case Sensitivity
Java treats uppercase and lowercase characters differently.
public class Main {
public static void main(String[] args) {
String s1 = "java";
String s2 = "Java";
System.out.println(s1.equals(s2));
}
}
The output is false because both Strings contain different character cases.
9. Important Points to Remember
- a. String is a class in Java
- b. Strings are immutable
- c. String indexing starts from 0
- d. String literals use the String Pool
- e. equals() compares actual content
- f. String methods usually return new objects
The Java String class is one of the most important classes in Java programming because text handling is required in almost every application. Understanding how Strings are created, stored, compared, and modified helps beginners write efficient and error-free Java programs.
