Introduction
In Java, a String is used to store text data.
Example:
String name = "Jay";
Here "Jay" is a String.
Strings are one of the most commonly used data types in Java because almost every application works with text like usernames, emails, messages, passwords, etc.
What is String in Java?
A String is a sequence of characters.
Example:
String word = "Hello";
Characters:
H e l l o
Why String is Important?
Strings are used everywhere:
- Login systems
- Search functionality
- Form inputs
- APIs
- Database values
- Chat applications
Without Strings, handling text is impossible.
How to Create a String?
1. Using String Literal
String s1 = "Hello";
This is the most commonly used way.
Java stores it inside the String Pool.
2. Using new Keyword
String s2 = new String("Hello");
This creates a new object in heap memory.
Difference Between Literal and new Keyword
| String Literal | new Keyword |
|---|---|
| Stored in String Pool | Stored in Heap |
| Memory efficient | Creates separate object |
| Faster | Slightly slower |
Example:
String a = "Java";
String b = "Java";
System.out.println(a == b);
Output:
true
- Because both refer to the same object.
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
Output:
false
- Because two different objects are created.
String is Immutable
Immutable means:
Once a String is created, it cannot be changed.
Example:
String s = "Hello";
s.concat(" World");
System.out.println(s);
Output:
Hello
- Because concat() creates a new object.
Correct way:
s = s.concat(" World");
System.out.println(s);
Output:
Hello World
Important String Methods
length()
Returns total characters.
String s = "Java";
System.out.println(s.length());
Output:
4
charAt()
Gets character by index.
String s = "Java";
System.out.println(s.charAt(1));
Output:
a
toUpperCase()
String s = "java";
System.out.println(s.toUpperCase());
Output:
JAVA
toLowerCase()
String s = "JAVA";
System.out.println(s.toLowerCase());
Output:
java
equals()
Compares values.
String a = "Java";
String b = "Java";
System.out.println(a.equals(b));
Output:
true
equalsIgnoreCase()
Ignores uppercase/lowercase.
String a = "java";
String b = "JAVA";
System.out.println(a.equalsIgnoreCase(b));
Output:
true
contains()
Checks whether text exists.
String s = "Java Programming";
System.out.println(s.contains("Java"));
Output:
true
startsWith()
String s = "Java";
System.out.println(s.startsWith("Ja"));
Output:
true
endsWith()
String s = "Java";
System.out.println(s.endsWith("va"));
Output:
true
replace()
String s = "Java";
System.out.println(s.replace('a', 'o'));
Output:
Jovo
substring()
- Extracts part of String.
String s = "Programming";
System.out.println(s.substring(0, 6));
Output:
Progra
split()
- Splits String into parts.
String s = "Java Python React";
String arr[] = s.split(" ");
for(String word : arr)
{
System.out.println(word);
}
Output:
Java
Python
React
== vs equals()
==
- Checks memory reference.
equals()
- Checks actual content.
Example:
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
System.out.println(a.equals(b));
Output:
false
true
String Pool
Java stores String literals in a special memory area called:
String Constant Pool
Example:
String a = "Java";
String b = "Java";
- Both point to the same object.
- This saves memory.
StringBuilder
Since String is immutable, repeated modifications create many objects.
To solve this, Java provides:
StringBuilder
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
Output:
Hello World
StringBuffer
Similar to StringBuilder but thread-safe.
| StringBuilder | StringBuffer |
|---|---|
| Faster | Slower |
| Not synchronized | Synchronized |
Common Interview Programs
Reverse String
String s = "Java";
for(int i = s.length()-1; i >= 0; i--)
{
System.out.print(s.charAt(i));
}
Output:
avaJ
Palindrome String
String s = "madam";
String rev = "";
for(int i = s.length()-1; i >= 0; i--)
{
rev += s.charAt(i);
}
if(s.equals(rev))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
Output
Palindrome
And so on
Conclusion
String is one of the most important concepts in Java.
To become strong in Java:
- Understand immutability
- Learn String methods
- Practice String programs
- Know String Pool concept
- Learn difference between == and equals()
Mastering Strings will help a lot in:
- Interviews
- Backend Development
- Spring Boot
- Problem Solving
- Real-world Applications
Top comments (0)