Difference Between byte, short, int and long Datatype in Java

19 Jan, 2021
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.
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.
byte
char
boolean
intsignedunsigned
signed
unsigned
short
float
long
double
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.
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.
Conclusion:
The keyword used is  ‘byte’.
The keyword used is ‘short’.
The keyword used is ‘int’.
The keyword used is ‘long’.
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 range
 long >  int > short > byte
Order in terms of size 
long> int > short> byte
Java-Data Types
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.