Difference Between byte, short, int and long Datatype in Java
Last Updated :
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.
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.
- byte
- char
- boolean
- int
- 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.
Conclusion:
Criteria | Byte | Short | Int | Long |
---|
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. |
Deceleration/keyword used |
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. |
Default Value | 0 | 0 | 0 | 0L |
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
long> int > short> byte
Similar Reads
Difference Between Long and BigInteger in Java In Java, long and BigInteger are two different data types used to represent and perform operations on the large integers. In this article we will learn about BigInteger and Long in Java. long in Javalong is a primitive data type in Java used to store 64-bit signed integers. It has a range of values
3 min read
Difference between byte and sbyte in C# In C#, a single byte is used to store 8-bits value. The byte and sbyte both are used for byte type of data. byte : This Struct is used to represent 8-bit unsigned integers. The byte is an immutable value type and the range of Byte is from 0 to 255. Example : C# // C# program to demonstrate // the by
2 min read
Difference between x++ and x=x+1 in Java In x++, it increase the value of x by 1 and in x=x+1 it also increase the value of x by 1. But the question is that both are same or there is any difference between them. We should aware with the fact that whenever we are trying to apply any arithmetic operator between two variables a and b, the res
3 min read
Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
Difference between data type and data structure Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the
4 min read
Difference between an Integer and int in Java with Examples In Java, int is a primitive data type while Integer is a Wrapper class.int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it.Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipul
6 min read
Difference between Simple and Compound Assignment in Java Many programmers believe that the statement "x += i" is simply a shorthand for "x = x + i". This isnât quite true. Both of these statements are assignment expressions. The second statement uses the simple assignment operator (=), whereas the first uses a compound assignment operator. The compound as
2 min read
Difference between Type Casting and Type Conversion 1. Type Casting: In type casting, a data type is converted into another data type by the programmer using the casting operator during the program design. In type casting, the destination data type may be smaller than the source data type when converting the data type to another data type, that's why
3 min read
Short byteValue() method in Java with Example The java.lang.Short.byteValue() method of Short class is a built in method in Java which is used to return the value of the Short object as a byte. Syntax ShortObject.byteValue() Return Value: It return the value of ShortObject as byte. Below is the implementation of byteValue() method in Java: Exam
2 min read
Byte longValue() method in Java with examples The longValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as long. Syntax ByteObject.longValue() Return Value: It return the value of ByteObject as long. Below is the implementation of longValue() method in Java: Example 1: Java // Java c
2 min read