Java String Class
String is Java's class for text values, such as a person's name or a message. A String object keeps the same text after it is created. Methods can return another text value, but they do not edit the original String.
String name = "John";
String message = "Welcome to Java";Here, "John" and "Welcome to Java" are strings.
Java provides the String class in the java.lang package to work with strings.
Because java.lang is automatically imported, we can use String without explicitly importing it.
String name = "John";String is a Class in Java
Unlike primitive data types such as int, char, and boolean, String is a class.
String name = "John";The variable name refers to a String object.
We can also explicitly create a String object:
String name = new String("John");Both represent the text "John", but they differ in how the String object is created and how the String pool is involved.
Why is String Important in Java?
Strings are used almost everywhere in Java applications.
For example:
- User names
- Passwords and other text values
- Messages
- File paths
- URLs
- JSON data
- Database values
- Logs
- Search keywords
Example
String username = "john123";
String email = "john@example.com";
String city = "Bangalore";Creating a String
The most common way is using a string literal:
String name = "John";We can also create a String object using new:
String name = new String("John");The difference between these approaches is explained in detail in the Ways to Create String in Java and String Pool in Java topics.
| Creation form | Object behavior | Typical use |
|---|---|---|
String name = "Java"; | Uses the string literal pool; identical literals can share a pooled object | Preferred for ordinary string values |
String name = new String("Java"); | Explicitly creates a new String object | Rarely needed in application code |
Strings are Immutable
One of the most important characteristics of Java's String class is that String objects are immutable.
Immutable means:
Once a String object is created, its contents cannot be changed.Consider:
String name = "Java";
name = name + " Programming";The original "Java" String object is not modified.
Instead, a new String object representing "Java Programming" is created, and name refers to that new object.
Example of String Immutability
String text = "Hello";
text.concat(" Java");
System.out.println(text);Output
HelloThe concat() operation creates a new String, but the returned String was not assigned back to text.
To use the new value:
String text = "Hello";
text = text.concat(" Java");
System.out.println(text);Output
Hello JavaString Class is final
The String class is declared as final.
Conceptually:
public final class String {
// ...
}Because it is final, another class cannot extend String.
class MyString extends String {
// Error
}This is one of the characteristics that contributes to the controlled behavior of String objects.
String and char
A char represents a single UTF-16 code unit:
char letter = 'A';A String can contain multiple characters:
String word = "Java";For example:
'A' → char
"Java" → StringCommon String Operations
The String class provides many methods for working with text.
For example:
String text = "Hello Java";Length:
text.length();Convert to uppercase:
text.toUpperCase();Check whether text is present:
text.contains("Java");Extract part of a String:
text.substring(0, 5);Compare strings:
text.equals("Hello Java");These methods are covered in detail in the String Methods topic.
Comparing Strings
Do not use == when your intention is to compare String contents.
Use:
String a = "Java";
String b = "Java";
System.out.println(a.equals(b));Output
trueThe equals() method compares the contents of the strings.
== compares references rather than String contents.
String Example
public class HelloWorld {
public static void main(String[] args) {
String name = "John";
String message = "Hello " + name;
System.out.println(message);
System.out.println(message.length());
System.out.println(message.toUpperCase());
}
}Output
Hello John
10
HELLO JOHNKey Characteristics of String
The Java String class has several important characteristics:
| Characteristic | Description |
|---|---|
| Type | A class in the java.lang package |
| Value | Represents a sequence of characters |
| Mutability | Immutable after creation |
| Inheritance | String is final and cannot be subclassed |
| Pooling | String literals can use the string pool |
| Methods | Provides operations for comparing, searching, and transforming text |
Key Takeaway
The String class in Java is used to represent and work with text. String objects are immutable, meaning their contents cannot be changed after creation. Java provides many built-in methods to compare, search, extract, and transform strings.
See you in next page....🙂