首页 > Java > java教程 > 正文

Java 中的原始数据类型

WBOY
发布: 2024-08-30 15:15:33
原创
398 人浏览过

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中的原始数据类型可以细分为以下四组:

1.整数数据类型

java中的整数数据类型存储正数和负数。 byte、short、int 和 long 等数据类型都属于此类数据类型。

  • Byte: Java中的Byte数据类型可以存储-128到127范围内的数字。每当我们想要节省内存时,都可以使用byte数据类型,因为它消耗的内存更少转换为 int 数据类型。
  • Int:java中的Int数据类型可以存储从-2147483648到2147483647的数字,它是我们存储数字时常用的数据类型。
  • Short: Short 数据类型可以存储范围从 -32768 到 32767 的数据。
  • Long:长数据类型可用于存储范围从-9223372036854775808到9223372036854775807的数字。当要存储的数据超出整数数据类型的范围时,通常首选长数据类型。如果是长数据类型,实际数据后面必须跟“L”。

2.浮点数

这种类型的数据类型是为了存储十进制数字而设计的。浮点型和双精度型都属于此类数据类型。

  • Float:Float 数据类型可以存储小数点后 6 到 7 位的小数值。使用浮点数据类型时,实际数据后面应跟“f”。
  • Double:Double 数据类型旨在存储具有 14 到 15 位小数的数字。使用双精度数据类型时,实际值后面应带有“d”。
  • Boolean:布尔数据类型是使用 boolean 作为关键字声明的,它只允许两个 true 或 false 值。
  • 字符:java中的字符数据类型是使用char关键字声明的,占用2个字节的空间。它只能用于存储单个字符。

这是一个显示不同数据类型以及大小的表格:

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.
原始数据类型

尺寸

详细信息

字节 1 字节 存储范围从 -128 到 127 的正数和负数。 整数 4 字节 存储范围从 -2,147,483,648 到 2,147,483,647 的正数和负数。 短 2 字节 存储范围从 -32,768 到 32,767 的正数和负数。 长 8 字节 存储从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的正数和负数。 浮动 4 字节 存储十进制数字。可用于存储6到7位小数的​​数字 双 8 字节 存储十进制数字。它可用于存储具有 15 位十进制数字的数字。 布尔值 1 位 只能存储 true 或 false。 字符 2 字节 它可用于仅存储单个字符、字母或 ASCII 值。 表> 在原始数据中实现的示例 在此示例中,我们将展示如何使用 Java 编程中可用的不同基本类型:
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原始数据类型,并举例说明了每种数据类型的意义。

以上是Java 中的原始数据类型的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!