23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

zeros<br />

>>> shift bits right, fill<strong>in</strong>g <strong>in</strong> with<br />

Operational Assignment Operators<br />

Besides the st<strong>and</strong>ard assignment operator (=), <strong>Java</strong> also provides a number of<br />

other assignment operators that have operational side effects. These other k<strong>in</strong>ds of<br />

operators are of the follow<strong>in</strong>g form:<br />

variable op = expression<br />

where op is any b<strong>in</strong>ary operator. The above expression is equivalent to<br />

variable = variable op expression<br />

except that if variable conta<strong>in</strong>s an expression (for example, an array <strong>in</strong>dex),<br />

the expression is evaluated only once. Thus, the code fragment<br />

a [5] = 10;<br />

i = 5;<br />

a[i++] += 2;<br />

leaves a [5] with value 12 <strong>and</strong> i with value 6.<br />

Str<strong>in</strong>g Concatenation<br />

Str<strong>in</strong>gs can be composed us<strong>in</strong>g the concatenation operator (+), so that the code<br />

Str<strong>in</strong>g rug = "carpet";<br />

Str<strong>in</strong>g dog = "spot";<br />

Str<strong>in</strong>g mess = rug + dog;<br />

Str<strong>in</strong>g answer = mess + " will cost me " + 5 + "<br />

dollars!";<br />

would have the effect of mak<strong>in</strong>g answer refer to the str<strong>in</strong>g<br />

"carpetspot will cost me 5 dollars!"<br />

This example also shows how <strong>Java</strong> converts nonstr<strong>in</strong>g constants <strong>in</strong>to str<strong>in</strong>gs,<br />

when they are <strong>in</strong>volved <strong>in</strong> a str<strong>in</strong>g concatenation operation.<br />

45

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

Saved successfully!

Ooh no, something went wrong!