Header Ads

Breaking News
recent

Strings, the Assignment Operator, and the Operator

A person’s name contains more than one character. Such values are called strings. More formally, a string is a sequence of zero or more characters. Strings in Java are enclosed in double quotation marks (not in single quotation marks, as are the char data types). Most often, we process strings as a single unit. To process strings effectively, Java provides the class String. The class String contains various operations to manipulate a string. You will see this class used throughout the book. Chapter 3 discusses various operations provided by the class String.Moreover, technically speaking, the class String is not a primitive type. A string that contains no characters is called a null or empty string. The following are examples of strings. Note that "" is the empty string.
"William Jacob"
"Mickey"
""
A string, such as "hello", is sometimes called a character string or string literal or string constant. However, if no confusion arises, we refer to characters between double quotation marks simply as strings. Every character in a string has a specific position in the string. The position of the first character is 0, the position of the second character is 1, and so on. The length of a string is the number of characters in it.


Suppose that str is a String variable and we want to assign the string "Sunny" to str.
This can be accomplished by using the statement:
str = "Sunny"; //Line 1
or the statement:
str = new String("Sunny"); //Line 2
After the execution of the statement in Line 1 or Line 2, str will point to the String object with the value "Sunny". Recall from Chapter 3 that the statement in Line 2 explicitly uses the operator new. Also recall that there is a slight difference in the way these statements execute. When the statement in Line 1 executes, the computer checks whether there already is a String object with the value "Sunny"; if so, then the address of that object is stored in str. On the other hand, when the statement in Line 2 executes, the computer will create a new String object with the value "Sunny" regardless of whether such a String object already exits. Let us further explain this concept.
Consider the following statements:
String str1 = "Hello";
String str2 = "Hello";
When the first statement executes, a String object with the value "Hello" is created and its address is assigned to str1. When the second statement executes, because there already exists a String object with the value "Hello", the address of this String object is stored in str2
Therefore, if you evaluate the expression (str1 == str2) after these statements, this expression evaluates to true. Moreover, here the expression str1.equals(str2) also evaluates to true. If you later assigned a different string, say, "Cloudy", to str2, then if no String object exists with the value "Cloudy", a String object with this value is created and its address is stored in str2. However, str1 would still point to the string "Hello". In other words, changing the value of the string str2 does not change the value of the string str1.
Next, consider the following statements:
String str3 = new String("Hello");
String str4 = new String("Hello");
When the first statement executes, a String object with the value "Hello" is created and its address is assigned to str3. When the second statement executes, another String object with the value "Hello" is created and its address is assigned to str4
It follows that the expression (str3 == str4) evaluates to false. However, the
expression str3.equals(str4) evaluates to true.

No comments:

Powered by Blogger.