Introduction
In Java, strings are one of the most commonly used data types. Almost every Java program uses strings to display text, take input, or process information. A string is simply a sequence of characters enclosed within double quotes (" ").
For example:
String name = "Shivam";
What is a String in Java?
A String in Java is an object of the String class, which belongs to the java.lang package.
Unlike other programming languages where strings are just character arrays, in Java, strings are immutable, meaning once created, their values cannot be changed.
Example:
String s1 = "Hello";
s1.concat(" World");
System.out.println(s1); // Output: Hello
Even after using concat(), the original string s1 remains unchanged because strings are immutable.
Creating Strings in Java
There are two main ways to create a string:
1. Using String Literal
String str1 = "Java";
In this case, the JVM stores the string in a String Constant Pool to save memory.
2. Using the new Keyword
String str2 = new String("Java");
This creates a new string object in the heap memory, even if the same string already exists in the pool.
Common String Methods
The String class provides many useful methods for manipulating text. Below are some of the most commonly used ones:
| Method | Description | Example | Output |
|---|---|---|---|
length() |
Returns length of the string | "Java".length() |
4 |
charAt(int index) |
Returns character at specified index | "Java".charAt(2) |
'v' |
toUpperCase() |
Converts to uppercase | "java".toUpperCase() |
"JAVA" |
toLowerCase() |
Converts to lowercase | "JAVA".toLowerCase() |
"java" |
concat(String str) |
Joins two strings | "Hello".concat(" World") |
"Hello World" |
equals(Object another) |
Compares two strings | "Java".equals("Java") |
true |
equalsIgnoreCase(String another) |
Compares ignoring case | "JAVA".equalsIgnoreCase("java") |
true |
contains(CharSequence s) |
Checks if substring exists | "Java".contains("av") |
true |
substring(int start, int end) |
Extracts substring | "Programming".substring(0, 7) |
"Program" |
trim() |
Removes spaces from both ends | " Hello ".trim() |
"Hello" |
Example Program
Here’s a simple Java program demonstrating some string operations:
public class StringExample {
public static void main(String[] args) {
String str1 = "Java Programming";
System.out.println("Length: " + str1.length());
System.out.println("Uppercase: " + str1.toUpperCase());
System.out.println("Lowercase: " + str1.toLowerCase());
System.out.println("Substring: " + str1.substring(5, 16));
System.out.println("Contains 'Pro': " + str1.contains("Pro"));
}
}
Output:
Length: 16
Uppercase: JAVA PROGRAMMING
Lowercase: java programming
Substring: Programming
Contains 'Pro': true
String Immutability Explained
When you modify a string, a new object is created instead of changing the original one.
For example:
String s = "Java";
s = s.concat(" Language");
System.out.println(s);
Now s points to the new string "Java Language", while the old "Java" string still exists in memory (until garbage collected).
StringBuffer and StringBuilder
If you need mutable strings (that can be changed), Java provides:
-
StringBuffer – Thread-safe (used in multithreading)
-
StringBuilder – Faster but not thread-safe (used in single-threaded applications)
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World
Conclusion
Strings in Java are powerful and essential for any program that deals with text. While the String class provides immutability and safety, classes like StringBuilder and StringBuffer offer flexibility when frequent modifications are needed.
Mastering strings is a fundamental step toward becoming a proficient Java programmer.
Comments
Post a Comment