The Java String Class defines the constructs of a String object and consists of a collection of constructors and methods. String objects are immutable, which means that once they are created, they cannot be changed. Every time you modify the value stored in a String variable, a whole new String object is instantiated. Explore the following example.
Constructing String objects:
Output:
Commentary:
Notice that line 5, line 6, and line 7 of the source code declares three different variables str, str1, and str2. Further notice that they store the same literal String “Hello”.
Questions:
[1] Why does the statement str == str1 return a value of true, yet str1 == str2 returns false?
[2] Why does the statement str1.equals(str2) return true?
Answers:
[1] First note that str == str1 is not comparing the string stored by the variable, but rather the memory location for the variable. Since strings are immutable objects they can be shared. The JVM already was aware of a String object “Hello” stored in memory, so when the string variable str1 was initialized with the same literal string, the JVM simply pointed it to the existing object already in memory. However, str2 was instantiated with the keyword new, so a new memory location was allocated to store the new instance of the string “Hello”.
[2] This line of code is now comparing the actual string stored by each variable. Since str1 and str2 both store “Hello”, this returns true as a result of the .equals() method of the String class.
Commentary:
Notice that lines 13 through 18 of the source code generates the identityHashCode for the objects str, str1, and str2. Afterwards, the identityHashCode which is an integer value is converted to the hexadecimal equivalent. This is a unique identifier for each object and this is what Java returns by default when you print an instance of the Object class. The JVM handles all memory management and offers this unique identifier as an alternative to the memory location to maintain a safe and secure programming environment.
Notice that str and str1 have the same identityHashCode(). This is Java’s way of telling us that they are one in the same. Therefore, we can say that str and str1 are aliases.
Students taking the AP Computer Science A Exam should be familiar with the Exam Appendix - Java Quick Reference sheet. It provides the following method details:
class java.lang.String
• String(String str)
• int length()
• String substring(int from, int to)
• String substring(int from)
• int indexOf(String str)
• boolean equals(String other)
• int compareTo(String other)
The following link is to the Oracle Help Center for the Java Math Class and provides the full list of available methods including details for implementation.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Sample excerpt from Oracle documentation:
New programmers should take the time to explore as many of the methods in the String class as possible and contemplate scenarios for which you might implement each method. Continue learning to read the Oracle documentation and using it as a reference in order to keep expanding your ability to efficiently code complex problems.
String name = “jamie”;
System.out.print(name.toUpperCase()); //prints JAMIE
String alphabet = “abcdefghijklmnopqrstuvwxyz”;
System.out.print(alphabet.indexOf(‘m’)); //returns 12
//notice that we use single quotes with char types
String sentence = “I am so happy that you can make it to my party.”;
System.out.print(sentence.indexOf(“that”)); //returns 14
//notice that we use double quotes with String types