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.

}<br />

<strong>in</strong>t i = 512;<br />

double e = 2.71828;<br />

1.3 Expressions<br />

Variables <strong>and</strong> constants are used <strong>in</strong> expressions to def<strong>in</strong>e new values <strong>and</strong> to modify<br />

variables. In this section, we discuss how expressions work <strong>in</strong> <strong>Java</strong> <strong>in</strong> more detail.<br />

Expressions <strong>in</strong>volve the use of literals, variables, <strong>and</strong> operators. S<strong>in</strong>ce we have<br />

already discussed variables, let us briefly focus on literals <strong>and</strong> then discuss operators<br />

<strong>in</strong> some detail.<br />

1.3.1 Literals<br />

A literal is any "constant" value that can be used <strong>in</strong> an assignment or other<br />

expression. <strong>Java</strong> allows the follow<strong>in</strong>g k<strong>in</strong>ds of literals:<br />

• The null object reference (this is the only object literal, <strong>and</strong> it is def<strong>in</strong>ed<br />

to be from the general Object class).<br />

• Boolean: true <strong>and</strong> false.<br />

• Integer: The default for an <strong>in</strong>teger like 176, or -52 is that it is of type <strong>in</strong>t,<br />

which is a 32-bit <strong>in</strong>teger. A long <strong>in</strong>teger literal must end with an "L" or "l," for<br />

example, 176L or −52l, <strong>and</strong> def<strong>in</strong>es a 64-bit <strong>in</strong>teger.<br />

• Float<strong>in</strong>g Po<strong>in</strong>t: The default for float<strong>in</strong>g- numbers, such as 3.1415 <strong>and</strong><br />

10035.23, is that they are double. To specify that a literal is a float, it must<br />

end with an "F" or "f." Float<strong>in</strong>g-po<strong>in</strong>t literals <strong>in</strong> exponential notation are also<br />

allowed, such as 3.14E2 or .19e10; the base is assumed to be 10.<br />

• Character: In <strong>Java</strong>, character constants are assumed to be taken from the<br />

Unicode alphabet. Typically, a character is def<strong>in</strong>ed as an <strong>in</strong>dividual symbol<br />

enclosed <strong>in</strong> s<strong>in</strong>gle quotes. For example, 'a' <strong>and</strong> '?' are character constants. In<br />

addition, <strong>Java</strong> def<strong>in</strong>es the follow<strong>in</strong>g special character constants:<br />

'\n' (newl<strong>in</strong>e)<br />

'\t' (tab)<br />

'\b' (backspace)<br />

'\r' (return)<br />

41

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

Saved successfully!

Ooh no, something went wrong!