Java 数据类型

Java 数据类型 用于定义不同变量类型、常量、方法参数、返回类型等等。变量只不过是用于存储某些值的保留内存位置。这意味着当您创建变量时,您会在内存中保留一些空间来存储一些数据。数据类型告诉编译器要存储的数据类型以及所需的内存。

操作系统根据变量的数据类型分配内存并决定在保留内存中可以存储什么内容。因此,通过为变量分配不同的数据类型,您可以在这些变量中存储整数、小数或字符。

Java 中的数据类型

Java 中有两种数据类型,它们分别是:

  • 原始数据类型
  • 引用/对象数据类型

1. Java 原始数据类型

原始数据类型由语言预定义并由关键字命名。 Java 支持八种基本数据类型。下面是基本数据类型的列表:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
简单类型二进制位数取值范围默认值包装类高频区间数据缓存范围
byte8-2^8 - 2^8-10Byte-128 - 127
short16-2^16 - 2^16-10Short-128 - 127
int32-2^32 - 2^32-10Integer-128 - 127
long64-2^64 - 2^64-10Long-128 - 127
float32-2^32 - 2^32-10.0Float无缓存区
double64-2^64 - 2^64-10.0Double无缓存区
boolean1true/falsefalseBoolean使用静态final定义,就会返回静态值
char16-2^16 - 2^16-1\u0000Character0 - 127

byte数据类型

  • 字节数据类型是 8 位有符号二进制补码整数

  • 最小值为 -128 (-2^7)

  • 最大值为 127(含)(2^7 -1)

  • 默认值为 0

  • 字节数据类型用于节省大型数组中的空间,主要是代替整数,因为字节比整数小四倍。

  • 示例 - byte a = 100, byte b = -50

short数据类型

  • short数据类型是 16 位带符号的二进制补码整数

  • 最小值为 -32,768 (-2^15)

  • 最大值为 32,767(含)( 2^15 -1)

  • 短数据类型也可以用作字节数据类型来节省内存。 Short 比整数小 2 倍

  • 默认值为 0。

  • 示例 − Short s = 10000,short r = -20000

int 数据类型

  • int 数据类型是 32 位带符号的二进制补码整数。

  • 最小值为 - 2,147,483,648 (-2^31)

  • 最大值为 2,147,483,647(含) (2^31 -1)

  • 除非考虑内存问题,否则整数通常用作整数值的默认数据类型。

  • 默认值为 0

  • 示例 − int a = 100000, int b = -200000

long数据类型

  • 长整型数据是一个64位有符号二进制补码整数
  • 最小值为-9,223,372,036,854,775,808(-2^63)
  • 最大值为 9,223,372,036,854,775,807(含)(2^63 -1)
  • 当需要比 int 更宽的范围时使用此类型
  • 默认值为0L
  • 示例 − long a = 100000L,long b = -200000L

float数据类型

  • Float 数据类型是单精度 32 位 IEEE 754 浮点

  • Float 主要用于在大型浮点数数组中节省内存

  • 默认值为 0.0f

  • Float 数据类型从不用于货币等精确值

  • 示例 - float f1 = 234.5f

double 数据类型

  • double数据类型是双精度64位IEEE 754浮点

  • 此数据类型一般用作十进制值的默认数据类型,一般为默认选择

  • 双精度数据类型切勿用于货币等精确值

  • 默认值为 0.0d

  • 示例 - double d1 = 123.4

boolen类型

  • 布尔数据类型表示一位信息

  • 只有两个可能的值:true 和 false

  • 此数据类型是用于跟踪真/假条件的简单标志

  • 默认值为 false

  • 示例 - boolean one = true

char 数据类型

  • char 数据类型是单个 16 位 Unicode 字符

  • 最小值为"\u0000"(或 0)

  • 最大值为"\uffff"(或 65,535(含))

  • Char 数据类型用于存储任何字符

  • 示例 - char letterA = 'A'

示例:演示不同的原始数据类型

以下示例显示了我们上面讨论的各种原始数据类型的用法。我们对数字数据类型使用了添加操作,而布尔和字符变量则按此方式打印。

public class JavaTester {
   public static void main(String args[]) {

      byte byteValue1 = 2;
      byte byteValue2 = 4;
      byte byteResult = (byte)(byteValue1 + byteValue2);

      System.out.println("Byte: " + byteResult);

      short shortValue1 = 2;
      short shortValue2 = 4;
      short shortResult = (short)(shortValue1 + shortValue2);

      System.out.println("Short: " + shortResult);

      int intValue1 = 2;
      int intValue2 = 4;
      int intResult = intValue1 + intValue2;

      System.out.println("Int: " + intResult);

      long longValue1 = 2L;
      long longValue2 = 4L;
      long longResult = longValue1 + longValue2;

      System.out.println("Long: " + longResult);

      float floatValue1 = 2.0f;
      float floatValue2 = 4.0f;
      float floatResult = floatValue1 + floatValue2;

      System.out.println("Float: " + floatResult);

      double doubleValue1 = 2.0;
      double doubleValue2 = 4.0;
      double doubleResult = doubleValue1 + doubleValue2;

      System.out.println("Double: " + doubleResult);

      boolean booleanValue = true;

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

      char charValue = 'A';

      System.out.println("Char: " + charValue);     
   }
} 

输出

Byte: 6
Short: 6
Int: 6
Long: 6
Float: 6.0
Double: 6.0
Boolean: true
Char: A 

2. Java 引用/对象数据类型

引用数据类型是使用类的对象。这些变量被声明为无法更改的特定类型。例如,Employee、Puppy 等。

类对象和各种类型的数组变量都属于引用数据类型。任何引用变量的默认值为 null。引用变量可用于引用声明类型或任何兼容类型的任何对象。

示例

以下示例演示引用(或对象)数据类型。

// 创建 'Animal' 类的对象
Animal animal = new Animal("giraffe");

// 创建"String"类的对象
String myString = new String("Hello, World!");