Java defines four integer types:byte,short,int, and long, which are shown here:
The most commonly used integer type is int. Variables of type int are often employed to control loops, to index arrays, and to perform general-purpose integer math. When you need an integer that has a range greater than int, use long. For example, here is a program that computes the number of cubic inches contained in a cube that is one mile by one mile, by one mile:
/*
Compute the number of cubic inches in 1 cubic mile.
*/
class Inches
{
public static void main(String args[])
{
long ci;
long im;
im = 5280 * 12;
ci = im * im * im;
System.out.println("There are " + ci + " cubic inches in cubic mile.");
}
}
Here is the output from the program:
There are 254358061056000 cubic inches in cubic mile.
Clearly, the result could not have been held in an int variable. The smallest integer type is byte. Variables of type byte are especially useful when working with raw binary data that may not be directly compatible with Java’s other built-in types. The short type creates a short integer that has its high-order byte first (called big- end i an format).
0 comments:
Post a Comment