java八种基本数据类型是什么

原创
ithorizon 8个月前 (09-17) 阅读数 151 #Java

Java八种基本数据类型

Java八种基本数据类型

Java是一种强类型编程语言,它定义了八种基本数据类型(Primitive Data Types),分别为:

1. 整数类型

整数类型用于存储整数数值,共有四种:

  • byte:占用1个字节(8位),取值范围为-128至127
  • short:占用2个字节(16位),取值范围为-32,768至32,767
  • int:占用4个字节(32位),取值范围为-2,147,483,648至2,147,483,647
  • long:占用8个字节(64位),取值范围为-9,223,372,036,854,775,808至9,223,372,036,854,775,807

2. 浮点类型

浮点类型用于存储带有小数的数值,共有两种:

  • float:占用4个字节(32位),取值范围为大约-3.4E38至3.4E38(有效位大约6-7位)
  • double:占用8个字节(64位),取值范围为大约-1.8E308至1.8E308(有效位大约15位)

3. 字符类型

字符类型用于存储单个字符,占用2个字节(16位):

  • char:取值范围为0至65,535(无符号的Unicode码点值)

4. 布尔类型

布尔类型用于存储真或假的状态,仅有一个值:

  • boolean:取值为true或false

示例代码

public class PrimitiveDataTypes {

public static void main(String[] args) {

byte byteValue = 100;

short shortValue = 32000;

int intValue = 100000;

long longValue = 10000000000L;

float floatValue = 234.5f;

double doubleValue = 123.456789;

char charValue = 'A';

boolean booleanValue = true;

System.out.println("byte: " + byteValue);

System.out.println("short: " + shortValue);

System.out.println("int: " + intValue);

System.out.println("long: " + longValue);

System.out.println("float: " + floatValue);

System.out.println("double: " + doubleValue);

System.out.println("char: " + charValue);

System.out.println("boolean: " + booleanValue);

}

}


本文由IT视界版权所有,禁止未经同意的情况下转发

文章标签: Java


热门