Sunday, December 28, 2008

What are Integer Data Types in Java

Welcome back to Java Code Online. Integer Data types are the most commonly used data types in Java. They provide the developers with an extraordinary power to deal with numbers. The Integer data types are the ones which deal with whole numbers.

The integer data types in Java is of four types. They are :-
1. byte (8 bits) (range: -128 to 127)
2. short (16 bits) (range: -32768 to 32767)
3. int (32 bits) (range: -2147483648 to 2147483647)
4. long (64 bits) (range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

The order is from smallest to largest. All the Integer Data types in Java are signed numbers, and they could be positive or negative. The above order depicts all the key features of the four Integer data types. I will cover each of them in detail below.

byte
This is the smallest of all the Integer Data Types consisting of 8 bits. These are signed 8 bit data types used for handling very small amount of data. As you can see from the above rankings, they can store up to a maximum of 8 bits, or the highest decimal possible is 127, i.e 2^7 - 1. Here 7 is used for power because one bit is reserved for the positive or negative sign., so it leaves it with 7 bits.

As the value which could be hold is very small, they are not much used in applications, apart from handing some binary operations where the data range is very small.

short
"short" data type comes after the "byte" data type in the hierarchy, they are 16 bit signed data types. These data types are the least used data types among all the various data types in Java.

int
After "short" data type comes the most used and most common data type in Java, and that is "int". The point that makes it the most versatile data type in Java, is the fact that its range covers almost all the possible numbers which could be used by a common person. The rankings above show that in case of an "int", the range is large enough to hold any kind of number.

long
"long" is the last data type in the rankings and comes after "int" data type. The range that a "long" can accommodate is huge, and is infact only used in those rare occasions when the int is not sufficient to hold the number. This data type is quite useful when dealing with very large numbers.

I hope the article helped you understand about the Integer Data types in Java. If you liked this article, then do leave a comment. For more information on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.