

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How strings are handled as objects in java, including the creation and initialization of string variables, the concept of immutability, and the consequences of changing string variable references. It also includes exercises to test understanding.
Typology: Exams
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Strings in Java are objects. They are instances of the class String (in the package java.lang). As is the case with other objects, memory. The variable with contain an address, rather than the actual string. String variables are actually references to a String object in For example, the statement String friend = new String(“Fred”); creates a new string as shown in the following diagram. friend “Fred” This constructor is rarely used. Java has an equivalent, simpler form to create and initialize a variable of type String. String friend = “Fred”; The constructor can be useful, however, to create multiple string objects with the same value (but different instances of each object). String s1 = “Sample”; String s2 = new String(s1);
It is also possible for a String variable to be in a state where it does not refer to a string containing characters. Consider the following: String s1; String s2 = null; // uninitialized// null string String s3 = ""; // empty string The variable variable will result in an error. s1 is uninitialized. It can be assigned to a String, but any other attempt to use this The variable compared to other strings. s2 has been set to the null value. It can be printed (producing the output "null") or The variable characters, but is also valid to use with all string operations. s3 has been set to the empty string, which has a length of zero and contains no
s “Sample”
s “Sample”
Changing a String Value The String class contains no Because of this, we say strings are mutator immutable methods. Once we create a string, its value cannot be changed. objects. We can still change the values of the string variable, to reference another string, but the actual string object (in memory) is fixed. For example, consider the fragment String s = "Hello"; s = "Bonjour"; The first line sets the variable changes the reference to another object containing s to refer to a string object containing "Bonjour". The first object, "Hello". The second line "Hello", which now has no variables referencing it, it now lost.
Now consider the fragment String s2 = "Repeat"; s2 = "Repeat"; Once again, the first line creates s2 and sets it to refer to a string object. The second line creates a new string object , even though the strings are exactly the same. The result is the same – two string objects are in memory, but the variable references the second object, and the first has been lost.
s “Bonjour” “Hello”
s “Repeat” “Repeat”