In Java, strings are immutable, which means that they cannot be altered once created.
For example, the following code:
String message = "Jello world";
message[0] = 'H';
would produce an error.
Variable can still be changed:
Note that this does not mean that message
cannot be changed, just that the individual characters of a string literal cannot be changed.
"Jello world" is a string literal that Java stores in memory, while message
is a variable that refers to that string literal.
When we do message[0] = 'H'
, we are trying to alter the string literal "Jello world", which is not allowed.
But like any other variable, message
can be changed by reassigning it with a new string, like this:
String message = "Jello world";
message = "Hello world";
System.out.println(message);
This code would output:
Hello world
Instead of trying to alter the first string, this code now creates a new one ("Hello world"), stores it in memory and tells message
to refer to this new string.
Assignment
Follow the Coding Tutorial and let's practice with string immutability!
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore the concept of string immutability in Java. Understanding string immutability is crucial for writing efficient and bug-free code. Strings are a fundamental part of any programming language, and Java is no exception. They are used in various scenarios, such as handling user input, manipulating text data, and more.
String immutability means that once a string object is created, it cannot be changed. This is different from variables, which can be reassigned to new values. For example, if you have a string "Hello"
, you cannot change its individual characters. However, you can reassign the variable to a new string.
Consider the following example:
String greeting = "Hello";
greeting = "Hi";
System.out.println(greeting); // Outputs: Hi
In this example, the variable greeting
is reassigned to a new string "Hi"
, but the original string "Hello"
remains unchanged in memory.
Let's delve deeper into the key concepts of string immutability:
Here's an example to illustrate these concepts:
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2); // Outputs: true
str1 = "Hi";
System.out.println(str1 == str2); // Outputs: false
In this example, str1
and str2
initially point to the same string in the pool. When str1
is reassigned, it points to a new string, while str2
still points to the original string.
Let's look at some practical examples and use cases of string immutability:
String part1 = "Hello";
String part2 = "World";
String message = part1 + " " + part2;
System.out.println(message); // Outputs: Hello World
In this example, a new string is created by concatenating part1
and part2
. The original strings remain unchanged.
String original = "Hello World";
String sub = original.substring(0, 5);
System.out.println(sub); // Outputs: Hello
Here, the substring
method creates a new string containing a portion of the original string. The original string remains unchanged.
When working with strings, it's essential to be aware of common pitfalls and follow best practices:
StringBuilder
or StringBuffer
for concatenating strings in a loop to avoid creating multiple string objects.For advanced string manipulation, consider using the following techniques:
Here's an example using StringBuilder
:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb.toString()); // Outputs: Hello World
Let's implement a simple program to demonstrate string immutability:
public class StringImmutability {
public static void main(String[] args) {
// Initial string
String message = "Jello world";
// Attempt to change the first character (will cause an error)
// message[0] = 'H'; // Uncommenting this line will cause a compilation error
// Correct way: Reassign the variable to a new string
message = "Hello world";
// Print the updated message
System.out.println(message); // Outputs: Hello world
}
}
When debugging and testing string-related code, consider the following tips:
Here's an example of a simple test case:
public class StringImmutabilityTest {
public static void main(String[] args) {
String message = "Hello";
assert message.equals("Hello") : "Test failed!";
message = "Hi";
assert message.equals("Hi") : "Test failed!";
System.out.println("All tests passed.");
}
}
When solving problems related to strings, consider the following strategies:
In this lesson, we explored the concept of string immutability in Java. We discussed its significance, common use cases, and best practices. Understanding string immutability is essential for writing efficient and maintainable code. Keep practicing and exploring further applications to master this concept.
For further reading and practice, consider the following resources: