Recall or review from Lesson_02 that Strings are not just primitive data types. They are in fact objects that have special consideration, allowing them to act like primitive data in some ways. However, there are many situations in which Strings will behave differently than other datatypes.
When you create a String in Java...
String name = "James Brown"
You are not really setting a String called name equal to "James Brown". You are creating a new object of the String class that contains the "James Brown" String....
String name = new String ("James Brown");
The difference is that the String "James Brown" that we put into name can never be changed. It gets it's own permanent memory address for as long as the program is running. You are able to set name equal to a new String...
name = "Superman"
But "James Brown" is here forever (as long as the program is running), and if we set other variables equal to "James Brown", they will all be referring to the same String.
String supaBad = "James Brown"; //=========+
String soulMan = "James Brown"; //=========+==>all refer to the
String godFatherSoul = "James Brown"; //===+ same String
Because of this property, String objects are said to be "immutable", or unable to be changed.
Grades in Review
Open up your Lab 10 and review Exercise_01. Here we defined the grade values without taking user inputs. Why did we do this? Would the program work if we took user inputs?In the Grades class, the calcPoints() method, uses the "==" operator to compare the two grade String objects.
In the runner, our grade[X] String objects are set manually
This allows the immutability of Strings to work in our favor. For example, if we set history to "A", then the "A" in the calcPoints() method would be referring to the same "A" String object, and the condition letterGrade == "A" would evaluate to true.
But the "==" operator doesn't quite work how we would expect. It does not compare the value of the two objects, but simply evaluates if two objects refer to the same data.
Therefore, when we take a user input for history.....
we are not setting it to "A" directly. We are setting it to the return value of next() from the Scanner object keyboard, that may or may not get an input of "A". So if we were to evaluate history == "A", we would get a return of false, even if we input "A" when prompted.
We are then passing history through the constructor into the variable letterGrade, and the condition letterGrade == "A" returns false.
In fact, no matter what combination of letter grades you input, all the conditions in the calcPoints() method will all evaluate to false, giving us a GPA of 0..... ALWAYS!!!
String Methods
As objects of the class java.lang.String, Strings in Java have methods that we can use to manipulate and change how the String data is presented.equals()
In order to take user inputs in a program like Grades, we need a way to compare the value of objects. We can use the equals() method of the String class, which checks whether the value of two objects are equal to one another. The format of an equals() method call is as follows...object1.equals(object2)
In the Grades class, we can simply change the "==" operator to a .equals(), and put the "A" into the parameters...
Now the condition evaluates whether letterGrade has a value of "A", and will therefore return true, setting the gradePointValue for hist to 4.0.
compareTo()
The compareTo() String method compares Strings lexicographically, or in dictionary order. It will return 0 if the Strings are the same string, a negative number if the left value comes sooner in alphabetical order, and a positive number if the left value comes later.
Output |
The trim() method
The trim() method returns all the leading ( before the chars ) and trailing ( after the chars ) whitespace from a character String.toUpperCase and toLowerCase
The toUpperCase() method returns a String with the same letters as the original, but with all uppercase letters.
The toLowerCase() method returns a String with the same letters as the original, but replaces any uppercase letters with lowercase.
replaceAll()
The replaceAll() method takes two parameters. It returns a new String from the original, replacing all characters that match the first parameter with the second parameter.
------------------------------------------------------------------------------------------------------------------------
Please download the lab files here.
APLab_11
Please download the lab files here.