Exam 7: Characters, Strings, and the Stringbuilder
Strings that cannot be changed are said to be immutable. What does this mean and how does it relate to values held in memory addresses?
When you reference a String, you actually are accessing the address of the characters that make up the String. If you subsequently assign a new value to the String, the address held by the original String is altered . The String now holds a new address where the characters are stored. The original String is still in memory, but the String no longer holds its address. Eventually, a part of the Java system called the garbage collector discards the String characters. Strings, therefore, are never actually changed; instead, new Strings are created and String references hold the new addresses. Strings that can't be changed are called immutable.
StringBuilder greeting = new StringBuilder("Day 1");
Using the above StringBuilder, create a setCharAt() method that will change the "1" to a "2" in the String "Day 1". Explain how the setCharAt() method operates.
greeting.setCharAt(4, '2');
To alter just one character in a StringBuilder, you can use the setCharAt() method, which allows you to change a character at a specified position within a StringBuilder object. This method requires two arguments: an integer position and a character. In the phrase "Day 1", the greeting.setCharAt(4, '2'); changes the value 1 to 2.
Match each term with the correct statement below.
-Returns the lowercase equivalent of the argument
B
System.out.println("Your name is " + yourName); The above statement is an example of ____, which is used to join Strings.
Match each term with the correct statement below.
-Add characters to the end of a StringBuilder object
String aName = "Michael"
Using the above statement, write the length() method that will return the length of the aName String. What value will the length() method return when executed?
In Java, to ____________________ a String means to break down its separate characters into a numeric format.
Strings and other objects that can't be changed are known as ____.
Which of the following correctly declares and initializes a String object?
To convert a String to an integer, you use the valueOf() method of the ____ class.
When you compare Strings with the == operator, you are comparing their values, not their memory addresses.
The StringBuilder class is more efficient than the StringBuffer class because it can execute multiple threads during program execution.
You can tell that the equals() method takes a ____ argument because parentheses are used in the method call.
myCounty = " Clark Jackson Scioto"
myCounty.charAt(6)
Using the above code, what will be the value of the charAt() method once the code executes? Explain how the charAt() method operates.
String greeting = "Welcome back";
Using the above statement, write the length() method that will return the length of the greeting String. Store the length in an integer named greetingLength.
Describe how a programmer would use the two types of Character class methods (those that begin with "is" and those that begin with "to") for testing the values of characters.
The ____________________ class contains standard methods for testing the values of characters.
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)