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

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

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

n.longValue( )<br />

float<br />

Float<br />

n = new Float(3.934F);<br />

n.floatValue( )<br />

double<br />

Double<br />

n = new Double(3.934);<br />

n.doubleValue( )<br />

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

A str<strong>in</strong>g is a sequence of characters that comes from some alphabet (the set of all<br />

possible characters). Each character c that makes up a str<strong>in</strong>g s can be referenced<br />

by its <strong>in</strong>dex <strong>in</strong> the str<strong>in</strong>g, which is equal to the number of characters that come<br />

before c <strong>in</strong> s (so the first character is at <strong>in</strong>dex 0). In <strong>Java</strong>, the alphabet used to<br />

def<strong>in</strong>e str<strong>in</strong>gs is the Unicode <strong>in</strong>ternational character set, a 16-bit character<br />

encod<strong>in</strong>g that covers most used written languages. Other programm<strong>in</strong>g languages<br />

tend to use the smaller ASCII character set (which is a proper subset of the<br />

Unicode alphabet based on a 7-bit encod<strong>in</strong>g). In addition, <strong>Java</strong> def<strong>in</strong>es a special<br />

built-<strong>in</strong> class of objects called Str<strong>in</strong>g objects.<br />

For example, a str<strong>in</strong>g P could be<br />

"hogs <strong>and</strong> dogs",<br />

which has length 13 <strong>and</strong> could have come from someone's Web page. In this case,<br />

the character at <strong>in</strong>dex 2 is 'g' <strong>and</strong> the character at <strong>in</strong>dex 5 is 'a'. Alternately, P<br />

could be the str<strong>in</strong>g "CGTAATAGTTAATCCG", which has length 16 <strong>and</strong> could<br />

have come from a scientific application for DNA sequenc<strong>in</strong>g, where the alphabet<br />

is {G, C, A, T}.<br />

Concatenation<br />

Str<strong>in</strong>g process<strong>in</strong>g <strong>in</strong>volves deal<strong>in</strong>g with str<strong>in</strong>gs. The primary operation for<br />

comb<strong>in</strong><strong>in</strong>g str<strong>in</strong>gs is called concatenation, which takes a str<strong>in</strong>g P <strong>and</strong> a str<strong>in</strong>g Q<br />

comb<strong>in</strong>es them <strong>in</strong>to a new str<strong>in</strong>g, denoted P + Q, which consists of all the<br />

characters of P followed by all the characters of Q. In <strong>Java</strong>, the "+" operation<br />

27

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

Saved successfully!

Ooh no, something went wrong!