QA OnString Handling InJava

Q In addition to System.out.println(), what are some other ways to display strings in Java programs?

A
Strings can be displayed using different means in Java programs that run on World Wide Web pages and in programs that have a graphical user interface. Web page Java programs, which are called applets, rely on a method called drawString() to display text. Hour 13, "Learning How Applets Work," covers several programming features that are specific to applet programming. Programs that have a graphical user interface display strings by putting them into a text-entry field or displaying them as a label next to some other part of the program's window.

Q How can I set the value of a string variable to be blank?

A
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 = "";

Q 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.

A
Java automatically starts each System.out.println() statement on 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. 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.

Q 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?

A
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.

Q Is it necessary to use += instead of + when adding some text to a string variable?

A
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.

Q Isn't there some kind of == operator that can be used to determine whether two strings have the same value, as in daughter == "Flora"?

A
As you will discover during the next hour, "Using Conditional Tests to Make Decisions," the
== operator can be used with all of 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.

Q Do all methods in Java display true or false in the same way that the equals() method does in relation to strings?

A
Methods have different ways of making a response after they are used. When a method sends back a value, as the
equals() method does, this is 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.

Quiz

The following questions will test your knowledge of the care and feeding of a string.

Questions

1. My friend concatenates. Should I report him to the authorities?

(a)
No. It's illegal only during the winter months.
(b) Yes, but not until I sell my story to Hard Copy first.
(c) No. All he's doing is pasting two strings together in a program.

2.
Why is the word
String capitalized while int and others are not?

(a)
String is a full word, but int ain't.
(b) Like all objects in Java,
String has a capitalized name.
(c) Poor quality control at JavaSoft.

3.
Which of the following characters will put a single quote in a string?

(a)
<QUOTE>
(b)
\'
(c) `

Answers

1. c. Concatenation is just another word for pasting, joining, melding, or otherwise connecting two strings together. It uses the + and += operators.

2.
b. The types of objects available in Java are all capitalized, which is the main reason variable names have a lowercase first letter. It makes it harder to mistake them for objects.

3.
b. The single backslash is what begins one of the special characters that can be inserted into strings.

Activities

You can review the topics of this hour more fully with the following activities:

·         Write a short Java program called Favorite that puts the code from this hour's "Comparing Two Strings" section into the main() block statement. Test it out to make sure it works as described and says that Ada's favorite instrument is not the ukelele. Then, change the initial value of the guess variable from ukelele to piano. See what happens.

·         Modify the Credits program so that the names of the director and all performers are displayed entirely in uppercase letters.

Post a Comment

Previous Post Next Post