Difference Between byte, short, int and long Datatype in Java
There are two types of data types namely primitive datatype/fundamental and non-primitive/derived datatype. The primitive data type is defined as local sets or default set of datatype already present while deriving a datatype or creating a set of datatype using them are known as derived data types such as an array, stack, queue, tries, etc. Let’s clear understanding of primitive data types before diving into differentiating the same.
|Fundamental Data Types||Derived Data Types|
|A fundamental data type is also called a primitive data type. These are the basic data types.||The derived data type is the aggregation of the fundamental data type.|
|character, integer, float, and void are fundamental data types.||Pointers, arrays, structures, and unions are derived data types.|
|Character is used for characters. It can be classified as|
char, signed char, Unsigned char.
|Pointers are used for storing the address of variables.|
|Integer is used for integers( not having decimal digits). It can be classified as signed and unsigned. Further, classified as int, short int, and long int.||An array is used to contain a similar type of data.|
|float is used for decimal numbers. These are classified as float, double and long double.||structure is used to group items of possibly different types into a single type.|
|void is used where there is no return value required.||It is like structure but all members in the union share the same memory location|
There are eight different primitive data types in JAVA namely byte, short, int, long, float, double, boolean, and char. In primitive data type requires different amounts of memory and has some specific operations which can be performed over it. They include a total of eight data types as follows as named. Among these, the integer data types are byte, short, long, and int. The integer data types are used to store numeric values. In this article, we will discuss the difference between these four Integer data-types. JAVA does not support an unsigned version of these integer data types. The main basis of difference is size and range.
Now, in primitive datatype let’s differentiate byte, short, int, and long to get a better understanding of which is to be used as per the requirements as they possess different traits that play a vital role in the implementation part in any programming language.
- byte datatype has a range from -128 to 127 and it requires very little memory (only 1 byte). It can be used in place of int where we are sure that the range will be very small. The compiler automatically promotes the byte variables to type int, if they are used in an expression and the value exceeds their range.
- short datatype is the variable range is more than byte but less than int and it also requires more memory than byte but less memory in comparison to int. The compiler automatically promotes the short variables to type int, if they are used in an expression and the value exceeds their range.
- int datatype is the most preferred type for numeric values.
- long datatype is less frequently used. It should only be used when the range of the numeric value is too high. It requires the most memory(8 bytes) in comparison to the other three data-types.
|Size / width||It is of 8 bits||It is of 16 bits||It is of 32 bits||It is of 64 bits|
|Range||-128 to 127||-32,768 to 32,767||-2,147,483,648 to 2,147,483,647||–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.|
The keyword used is ‘byte’.
The keyword used is ‘short’.
The keyword used is ‘int’.
The keyword used is ‘long’.
|Syntax of declaration||byte datatype_name;||short datatype_name;||int datatype_name;||long datatype_name;|
|Memory required||It requires 1 byte.||It requires 2 bytes.||It requires 4 bytes.||It requires 8 bytes.|
From above table it is evidently seen that-
- If the range of the numeric value is less, and we want to save the memory we can use byte or short depending on the range of values.
- While if there is a need for medium-range value then we can use the type int but when the range for the numeric values will be larger, then the long type variable must be used to hold the values.
- Order in terms of rangelong > int > short > byte
- Order in terms of sizelong> int > short> byte