Strings in Java
💫 Strings in Java
In Java, strings are among the most widely used data types. Every program — whether simple or complex — deals with text, and Strings make that possible.
Let’s explore everything about Strings in Java, step by step! 🚀
🔹 What is a String?
A String in Java is a sequence of characters enclosed in double quotes (" "
).
Example:
Here, "Alice"
is a String literal.
🔹 Strings are Objects
In Java, Strings are objects of the String
class, which is part of the package:
Even though you can create Strings just like primitive data types, under the hood, they are objects with many useful methods.
🔹 Ways to Create Strings
1️⃣ Using String Literals
Strings created this way are stored in a special memory area called the String Constant Pool.
2️⃣ Using the new
Keyword
This creates a new String object in heap memory, even if the same value exists in the String pool.
🔹 Example: Creating Strings
Output:
🔹 String Immutability
Strings in Java are immutable, meaning once created, their value cannot be changed.
Example:
Output:
Here, the original String s
remains unchanged.
If we assign it back:
Output:
A new String object is created with the modified value.
🔹 Common String Methods
The String
class provides many useful methods. Let’s explore the most important ones 👇
Method | Description | Example | Output |
---|---|---|---|
length() | Returns number of characters | "Hello".length() | 5 |
charAt(int index) | Returns character at index | "Java".charAt(2) | v |
toUpperCase() | Converts to uppercase | "java".toUpperCase() | JAVA |
toLowerCase() | Converts to lowercase | "HELLO".toLowerCase() | hello |
concat(String s) | Joins two strings | "Hello".concat(" World") | Hello World |
equals(Object s) | Compares two strings (case-sensitive) | "Java".equals("java") | false |
equalsIgnoreCase(String s) | Case-insensitive comparison | "Java".equalsIgnoreCase("java") | true |
contains(String s) | Checks if substring exists | "Machine Learning".contains("Learn") | true |
substring(int start, int end) | Returns substring | "Programming".substring(0, 7) | Program |
replace(char old, char new) | Replaces characters | "banana".replace('a', 'o') | bonono |
trim() | Removes leading/trailing spaces | " Hello ".trim() | Hello |
🔹 Example 1: Using String Methods
Output:
🔹 String Comparison
1️⃣ Using ==
Compares references (memory locations), not content.
2️⃣ Using .equals()
Compares actual content of Strings.
✅ Always use .equals()
for comparing text values.
🔹 Example 2: Comparing Strings
🔹 String Concatenation
Strings can be joined using either:
-
The
+
operator -
The
concat()
method
Example:
Output:
🔹 String and Character Array Conversion
String → Char Array
Output:
Char Array → String
Output:
🔹 Example 3: Palindrome Check
A palindrome reads the same forwards and backwards.
Output:
🔹 StringBuffer and StringBuilder
Since String
is immutable, Java provides mutable alternatives for string modification:
Class | Mutability | Thread-Safe | Faster |
---|---|---|---|
String | ❌ Immutable | ✅ Yes | ❌ Slower |
StringBuffer | ✅ Mutable | ✅ Yes | ⚙️ Moderate |
StringBuilder | ✅ Mutable | ❌ No | ⚡ Fastest |
Example:
Output:
🔹 Key Points to Remember
✅ Strings are objects, not primitive types.
✅ They are immutable — any change creates a new object.
✅ Use .equals()
to compare content.
✅ Use StringBuilder
or StringBuffer
for frequent string modifications.
✅ Many built-in methods make text processing easy and efficient.
💡 Summary
Strings form the backbone of almost every Java program.
Example Programs
1️⃣ Program to Count Vowels in a String
Output:
2️⃣ Program to Check Whether a String is a Palindrome
Output:
3️⃣ Program to Reverse Each Word in a Sentence
Output:
4️⃣ Program to Compare Two Strings Ignoring Case
Output:
5️⃣ Program to Convert a String to Uppercase Without Using toUpperCase()
Output:
Comments
Post a Comment