有两种类型的数据类型,即原始数据类型/基本数据类型和非原始数据类型/派生数据类型。原始数据类型被定义为在派生数据类型或使用它们创建一组数据类型时已经存在的本地集或默认数据类型集被称为派生数据类型,例如数组、堆栈、队列、尝试等。让我们清楚地了解在深入区分相同的原始数据类型之前先对原始数据类型进行分类。
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 |
Java有八种不同的原始数据类型,分别是 byte、short、int、long、float、double、boolean 和 char。在原始数据类型中,需要不同数量的内存,并且有一些可以对其执行的特定操作。它们总共包括八种数据类型,如下所示。其中,整数数据类型有byte、short、long、int。整数数据类型用于存储数值。在本文中,我们将讨论这四种整数数据类型之间的区别。 Java不支持这些整数数据类型的无符号版本。差异的主要依据是大小和范围。
- 字节
- 字符
- 布尔值
- 整数
- 签
- 未签名
- 短的
- 漂浮
- 长
- 双倍的
现在,在原始数据类型中,让我们区分 byte、short、int 和 long,以便更好地了解要根据要求使用哪个,因为它们具有不同的特征,这些特征在任何编程语言的实现部分中都起着至关重要的作用。
- byte 数据类型的范围从 -128 到 127,它需要很少的内存(只有 1 个字节)。在我们确定范围非常小的情况下,它可以代替 int 使用。如果在表达式中使用字节变量并且值超出其范围,编译器会自动将字节变量提升为 int 类型。
- short 数据类型是变量范围大于字节但小于 int 并且它比字节需要更多的内存但与 int 相比需要更少的内存。如果在表达式中使用短变量并且值超出其范围,编译器会自动将短变量提升为 int 类型。
- int 数据类型是最喜欢的数值类型。
- long 数据类型不太常用。它 仅应在数值范围太大时使用。与其他三种数据类型相比,它需要最多的内存(8 字节)。
结论:
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
- Order in terms of size
long> int > short> byte