15.04.2018 Views

programming-for-dummies

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

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

544<br />

Declaring Variables<br />

Declaring integer data types<br />

Whole numbers represent integers, such as –9, 62, or 10. A whole number<br />

can be positive or negative. The most common integer data type is int and<br />

is used as follows:<br />

int variablename;<br />

Besides integer values, Java also offers a variety of other integer data types<br />

that can hold a different range of values, as shown in Table 2-1.<br />

Table 2-1 Typical Storage and Range Limitations of Java Integer Data Types<br />

Data Type Number of Bytes Range<br />

byte 1 –128 to 127<br />

short 2 –32,768 to 32,767<br />

int 4 –2,147,483,648 to 2,147,483,647<br />

long 8 –9,223,372,036,854,775,808 to<br />

9,223,372,036,854,775,807<br />

C# also offers a variety of different integer data types, which can be signed<br />

(positive and negative values) or unsigned (only positive values), as shown<br />

in Table 2-2. To declare a signed or unsigned variable, use the signed (or<br />

unsigned) keyword, such as<br />

signed int profit;<br />

unsigned short pets_owned;<br />

Table 2-2 Typical Storage and Range Limitations of C# Integer Data Types<br />

Data Type Number of Bytes Range<br />

byte 1 Signed: –128 to 127<br />

Unsigned: 0 to 255<br />

short 2 Signed: –32,768 to 32,767<br />

Unsigned: 0 to 65,535<br />

int 4 Signed: –2,147,483,648 to<br />

2,147,483,647<br />

Unsigned: 0 to 4,294,967,295<br />

long 8 Signed: –9,223,372,036,854,775,808 to<br />

9,223,372,036,854,775,807<br />

Unsigned: 0 to 1.8x1019

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

Saved successfully!

Ooh no, something went wrong!