Unit 2 - Using Objects

2.6 String Objects: Concatenation, Literals, and More

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.
2.6 String Objects: Concatenation, Literals, and More

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:

 
java
 
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.

 
java
 
 
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.

 
java
 
 
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.

 
java
 
 
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.
 
java
 
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.
java
 
String str = "Hello, World!";
String substr = str.substring(7, 12); // "World"

 

  • toLowerCase() and toUpperCase(): Return a new string converted to lowercase or uppercase.
 
java
 
 
String lower = "Java".toLowerCase(); // "java"
 
String upper = "Java".toUpperCase(); // "JAVA"

 

  • trim(): Returns a new string with whitespace removed from the beginning and end.
 
java
 
 
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 with newChar.
 
java
 
 
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.

java
 
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.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *