The Flavors of Integer

   

The next set of primitive types in Java contains what are known as the integer types:

  • byte

  • int

  • long

  • short

The types of integer differ only in the range of values they can hold. For instance, a byte cannot hold any number that is greater than 127, but a long can easily hold the world's population count (many times over actually).

Why are there so many types of integer? Couldn't you just declare every integer variable you need as a long and not worry about size restrictions? Although this would work in many cases, it is a confusing and inefficient use of a limited resource, namely the memory available to execute your programs. When you specify a meaningful integer type for a variable, you are providing information about the expected use of that variable to other programmers. For instance, storing the number of months of financing selected in a car loan program as a long would be confusing, or at least appear careless to others.

Besides clarity, you should also be concerned about the memory footprint required by your programs. A careful assignment of variable types prevents a program from requiring more memory to load and run than it actually needs to do its work. Programmers of embedded systems should be especially cautious.

Limits on Integer Values

Table 3.3 summarizes the supported ranges for each of the integer types. It also defines the default value assigned to an integer attribute of a class if it isn't explicitly initialized as part of its declaration.

Table 3.3. Integer Types and Their Limits
Integer Type Minimum Value Default Value Maximum Value
byte “128 127
short “32,768 32,767
int “2,147,483,648 2,147,483,647
long “9,223,372,036,854,775,808 9,223,372,036,854,775,807

Notice there is no concept of an unsigned int in Java. The integer types in Java are always treated as signed values.

Note

If an operation or assignment produces a value outside the allowed limits for an integer type, no overflow or exception occurs. Instead, the twos -complement value is the result. This produces a wrapping effect. For example, assigning 127+1 to a byte results in a stored value of “128. Assigning 127+2 would result in “127 and so on. You must be careful when assigning types to make sure that variables are sufficiently large for their use because you will not be notified at either compile time or runtime if this wrapping occurs.


Declaring Integer Variables

You declare variables of any of the integer types in basically the same way. The following lines show how to create a variable of each type:

 byte myFirstByte = 10; short myFirstShort = 1000; int myFirstInt = 1000000; long myFirstLong = 1000000000; 

Notice that the form of a declaration for an integer variable is nearly identical to what you already looked at for a boolean. The declaration for a Java primitive always consists of the type, an identifier, and an optional initial value. The initial value for an integer variable can be any whole number within the supported range of the specific type.

Operations on Integers

You can perform a wide variety of operations on integer variables. Table 3.4 shows a complete list. C and C++ programmers should find these very familiar.

Table 3.4. Operations on Integer Expressions
Operation Description
=, +=, “=, *=, /= Assignment operators
==, != Equality and inequality operators
<, <=, >, >= Relational operators
+, “ Unary sign operators
+, “, *, /, % Addition, subtraction, multiplication, division, and remainder operators
+=, “=, *=, /= Addition, subtraction, multiplication, division, and assign operators
++, “ “ Increment and decrement operators
<<, >>, >>> Bitwise shift operators
<<=, >>=, >>>= Bitwise shift and assign operators
~ Bitwise logical negation operator
&, , ^ Bitwise AND, OR, and exclusive or (XOR) operators
&=, =, ^= Bitwise AND, OR, exclusive or (XOR), and assign operators

Later in Chapter 6, you learn about the equality, inequality, and relational operators that produce Boolean results. For now, let's concentrate on the arithmetic operators.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net