19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

68 Chapter 2 Elementary <strong>Programming</strong><br />

2.31 Show the output of the following program:<br />

public class Test {<br />

public static void main(String[] args) {<br />

char x = 'a';<br />

char y = 'c';<br />

}<br />

}<br />

System.out.println(++x);<br />

System.out.println(y++);<br />

System.out.println(x - y);<br />

Key<br />

Point<br />

2.18 The String Type<br />

A string is a sequence of characters.<br />

The char type represents only one character. To represent a string of characters, use the data<br />

type called String. For example, the following code declares the message <strong>to</strong> be a string with<br />

the value "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>".<br />

String message = "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>";<br />

concatenate strings and<br />

numbers<br />

String is a predefined class in the <strong>Java</strong> library, just like the classes System, JOptionPane,<br />

and Scanner. The String type is not a primitive type. It is known as a reference type. Any <strong>Java</strong><br />

class can be used as a reference type for a variable. Reference data types will be thoroughly discussed<br />

in Chapter 8, Objects and Classes. For the time being, you need <strong>to</strong> know only how <strong>to</strong><br />

declare a String variable, how <strong>to</strong> assign a string <strong>to</strong> the variable, and how <strong>to</strong> concatenate strings.<br />

As first shown in Listing 2.1, two strings can be concatenated. The plus sign (+) is the concatenation<br />

opera<strong>to</strong>r if one of the operands is a string. If one of the operands is a nonstring<br />

(e.g., a number), the nonstring value is converted in<strong>to</strong> a string and concatenated with the other<br />

string. Here are some examples:<br />

// Three strings are concatenated<br />

String message = "Wel<strong>com</strong>e " + "<strong>to</strong> " + "<strong>Java</strong>";<br />

// String Chapter is concatenated with number 2<br />

String s = "Chapter" + 2; // s be<strong>com</strong>es Chapter2<br />

// String Supplement is concatenated with character B<br />

String s1 = "Supplement" + 'B'; // s1 be<strong>com</strong>es SupplementB<br />

If neither of the operands is a string, the plus sign (+) is the addition opera<strong>to</strong>r that adds<br />

two numbers.<br />

The augmented += opera<strong>to</strong>r can also be used for string concatenation. For example, the following<br />

code appends the string "and <strong>Java</strong> is fun" with the string "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong>" in<br />

message.<br />

message += " and <strong>Java</strong> is fun";<br />

So the new message is "Wel<strong>com</strong>e <strong>to</strong> <strong>Java</strong> and <strong>Java</strong> is fun".<br />

If i = 1 and j = 2, what is the output of the following statement?<br />

System.out.println("i + j is " + i + j);<br />

The output is "i + j is 12" because "i + j is " is concatenated with the value of<br />

i first. To force i + j <strong>to</strong> be executed first, enclose i + j in the parentheses, as follows:<br />

System.out.println("i + j is " + ( i + j ));

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!