java中的原始数据类型是那些指定数据类型和大小但不提供任何额外方法的数据类型; Java 中可用的基本数据类型的示例包括 byte、short、int、char、long、float、boolean 和 double。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
下面是显示 Java 中如何使用原始数据类型的语法:
byte byteData= 88; //declaring byte data type short shortData= 6000; //declaring short data type int intData= 20; // declaring integer data type long longData = 20000000000000L; // declaring long data type float floatdata= 1.1f; // declaring float data type double doubleData = 29.94d; // declaring double data type boolean booleanData= true; //declaring boolean data type char charData = ’b’; // declaring character data type
java中的原始数据类型可以细分为以下四组:
java中的整数数据类型存储正数和负数。 byte、short、int 和 long 等数据类型都属于此类数据类型。
这种类型的数据类型是为了存储十进制数字而设计的。浮点型和双精度型都属于此类数据类型。
这是一个显示不同数据类型以及大小的表格:
Primitive Data Type | Size | Details |
byte | 1 byte | Stores positive and negative numbers ranging from -128 to 127. |
int | 4 bytes | Stores positive and negative numbers ranging from -2,147,483,648 to 2,147,483,647. |
short | 2 bytes | Stores positive and negative numbers ranging from -32,768 to 32,767. |
long | 8 bytes | Stores positive and negative numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. |
float | 4 bytes | Stores Decimal numbers. It can be used for storing numbers having 6 to 7 decimal digits |
double | 8 bytes | Stores Decimal numbers. It can be used for storing numbers having 15 decimal digits. |
boolean | 1 bit | Can Store Only true or false. |
char | 2 bytes | It can be used for storing only a single character, letter or ASCII values. |
public class DataTypeDemo { public static void main(String[] args) { byte byteData= 88; //declaring byte data type int intData= 20; // declaring integer data type short shortData= 6000; //declaring short data type long longData = 20000000000000L; // declaring long data type float floatdata= 1.1f; // declaring float data type double doubleData = 29.94d; // declaring double data type boolean booleanData= true; //declaring boolean data type char charData = 'A'; // declaring character data type System.out.println("Value Declared using Byte Data Type is " + byteData); System.out.println("Value Declared using Integer Data Type is " + intData); System.out.println("Value Declared using Short Data Type is " + shortData); System.out.println("Value Declared using Long Data Type is " + longData); System.out.println("Value Declared using Float Data Type is " + floatdata); System.out.println("Value Declared using Double Data Type is " + doubleData); System.out.println("Value Declared using Character Data Type is " + charData); System.out.println("Value Declared using Boolean Data Type is " + booleanData); } }
代码:
输出: 结论 为了学习任何编程语言,正确理解不同的数据类型非常重要。上面的文章详细解释了java原始数据类型,并举例说明了每种数据类型的意义。
以上是Java 中的原始数据类型的详细内容。更多信息请关注PHP中文网其他相关文章!