Strings in Java are objects that represent sequences of characters. The String
class in Java is used to create and manipulate string data. Here are some key concepts and operations related to strings in Java, including concatenation, literals, and more.
String Literals
In Java, string literals are defined by enclosing a sequence of characters in double quotes. String literals are instances of the String
class. For example:
String greeting = "Hello, World!";
Concatenation
Concatenation is the operation of joining two strings end-to-end. In Java, you can concatenate strings using the +
operator.
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "John Doe"
Java also provides a concat
method for string concatenation, but the +
operator is more commonly used.
String hello = "Hello, ";
String world = "World!";
String greeting = hello.concat(world); // "Hello, World!"
Immutability of Strings
Strings in Java are immutable, meaning once a String
object is created, its value cannot be changed. When you perform operations on strings, such as concatenation, a new String
object is created.
String str = "Hello";
str = str + ", World!"; // A new String object is created
String Methods
The String
class provides many methods for string manipulation, including methods for searching, substring extraction, case conversion, trimming, replacing characters, and more. Here are a few examples:
- length(): Returns the number of characters in the string.
String str = "Java";
int len = str.length(); // 4
- substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string.
String str = "Hello, World!";
String substr = str.substring(7, 12); // "World"
- toLowerCase() and toUpperCase(): Return a new string converted to lowercase or uppercase.
String lower = "Java".toLowerCase(); // "java"
String upper = "Java".toUpperCase(); // "JAVA"
- trim(): Returns a new string with whitespace removed from the beginning and end.
String str = " Java ";
String trimmed = str.trim(); // "Java"
- replace(char oldChar, char newChar): Returns a new string resulting from replacing all occurrences of
oldChar
in the string withnewChar
.
String str = "Java";
String replaced = str.replace('a', 'A'); // "JAvA"
String Equality
To check if two strings have the same value, use the equals
method rather than ==
. The ==
operator checks for reference equality (whether two references point to the same object), while equals
checks for value equality.
String str1 = new String("Java");
String str2 = new String("Java");
// Incorrect way to compare strings
System.out.println(str1 == str2); // false
// Correct way to compare strings
System.out.println(str1.equals(str2)); // true
Conclusion
Understanding strings and their manipulation is fundamental in Java programming, as strings are one of the most used data types. The String
class provides a wide range of methods to perform various operations, enabling powerful and efficient string manipulation within Java applications.