The data types in java

By: aathishankaran Viewed: 153135 times  Printer Friendly Format    


The data types in java

 

            Java defines eight simple (or elemental) types of data: byte, short, int, long char, float, double, and Boolean. These can be put in four groups:

 

Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.

 

Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.

 

Characters: This group includes char, which represents symbols in a character set, like letters and numbers.

 

Boolean: This group includes Boolean, which is a special type for representing true/false values.

 

You can use these types as-is, or to construct arrays or your own class types. Thus, they form the basis for all other types of data that you can create.

 

            The simple types represent single values not complex objects. Although java is otherwise completely object-oriented, the simple types are not. They are analogous to the simple types found in most other non-object-oriented languages. The reason for this is efficiency. Making the simple types into objects would have degraded performance too much.

 

Integers

 

            Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values, java does not support unsigned, positive-only integers. Many other computer languages support both signed and unsigned integers. However, java’s designers felt that unsigned integers were unnecessary. Specifically, they felt that the concept of unsigned was used mostly to specify the behavior of the high-order bit, which defined the sign of an int when expressed as a number.

 

            The width of an integer type should not be thought of as the amount of storage it consumes, but rather as the behavior it defines for variable and expressions of that type

 

            The width and ranges of these integer types vary widely, as shown in this table.

 

Name               Width               Range

long                  64                    -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

int                     32                    -2,147,483,648 to 2,147,483,647

short                 16                    -32,768 to 32,767

byte                  8                      -128 to 127

 

byte

 

            The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127. Variable of type are especially useful when you’re working with a stream of data from a network or file. They are also useful when you’re working with raw binary data that may not be directly compatible with java’s other built-in types.

 

short

            short is a signed 16-bit type. It has a range from –32,768 to 32,767. It is probably the least-used java type, since it is defined as having its high byte first (called big-endian format). This type is mostly applicable to 16-bit computers, which are becoming increasingly scarce.

 

int

            The most commonly used integer type is int. it is a signed 32-bit type that has a range from –2,147,483,647. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. Any time you have an integer expression involving bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done.

 

            The int type is the most versatile and efficient type, and it should be used most of the time when you want to create a number for counting or indexing arrays or doing integer math.

 

long

 

            long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. This makes it useful when big, whole numbers are needed.

 



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial