How can I set the value of a string variable to be blank?
A pair of double quotation marks without any text between them is considered to be an empty string. You can set a string variable equal to this upon its creation or in other parts of your programs. The following code creates a new string variable called adaSays and sets it to nothing:
String adaSays = "";
Is there a way to make the text in one println() statement start right at the end of the text in the preceding println() statement? I don't want the second println() statement to start at the beginning of a new line, but it always does.
Java automatically ends each System.out.println() statement with its own new line, so the only way to prevent this is to use a statement that includes all of the text you want to display, or use the System.out.print() statement, which displays a string without ending it with a newline character automatically. The Credits program from the workshop has an example of a println() statement that includes several different lines of output. Take a look at it and see whether it fits what you want to do.
I can't seem to get the toUpperCase() method to change a string so that it's all capital letters. What am I doing wrong?
When you call a String object's toUpperCase() method, it doesn't actually change the String object it is called on. Instead, it creates a new string that is all uppercase letters. Consider the following statements:
String firstName = "Nessie"; String changeName = firstName.toUpperCase(); System.out.println("First Name: " + firstName);
This example will output the text First Name: Nessie because firstName contains the original string. If you switched the last statement to display the changeName variable instead, it would output First Name: NESSIE.
If the + operator is used with strings to link up two different strings, can you add the numeric value of one string to the value of another?
You can use the value of a String variable as an integer only by using a method that converts the string's value into a numeric form. This procedure is called casting, because it recasts existing information, in this case a string, as a different type of information. You'll learn about casting during Hour 11, "Describing What Your Object Is Like."
Is it necessary to use += instead of + when adding some text to a string variable?
Not at all. The += operator is strictly for the benefit of programmers who want to use it as a shortcut. If you're more comfortable using the + operator when pasting some added text to a string variable, you ought to stick with it. The time and convenience you can gain by using += will be lost pretty quickly if it causes you to make errors in your program.
Isn't there some kind of == operator that can be used to determine if two strings have the same value, as in daughter == "Flora"?
As you will discover during the next hour, "Using Conditional Tests to Make Decisions," the == operator can be used with all the variable types except for strings. The reason for the difference is that strings are objects. Java treats objects differently than other types of information, so special methods are necessary to determine whether one string is equal to another.
Do all methods in Java display true or false in the same way that the equals() method does in relation to strings?
Methods have different ways of making a response after they are used. When a method sends back a value, as the equals() method does, it's called returning a value. The equals() method is set to return a Boolean value. Other methods might return a string, an integer, another type of variable, or nothing at all.