21xrx.com
2024-09-19 10:14:49 Thursday
登录
文章检索 我的文章 写文章
深入了解Java基本数据类型
2023-06-15 15:30:48 深夜i     --     --
Java 基本数据类型 大小

我来介绍一下Java基本数据类型。Java基本数据类型共有8种,分别是: byte、short、int、long、float、double、char、boolean。 下面是每种类型的值范围和大小:

- byte(占用1个字节):-128 ~ 127

- short(占用2个字节):-32768 ~ 32767

- int(占用4个字节):-2147483648 ~ 2147483647

- long(占用8个字节):-9223372036854775808 ~ 9223372036854775807

- float(占用4个字节):1.4E-45 ~ 3.4028235E38

- double(占用8个字节):4.9E-324 ~ 1.7976931348623157E308

- char(占用2个字节):0 ~ 65535

- boolean(占用1个字节):true 或 false

下面是一个简单的Java程序,用于演示基本数据类型的使用:


public class DataTypesExample {

  public static void main(String[] args) {

    byte myByte = 127;

    short myShort = 32767;

    int myInt = 2147483647;

    long myLong = 9223372036854775807L;

    float myFloat = 3.4028235E38f;

    double myDouble = 1.7976931348623157E308;

    char myChar = 'A';

    boolean myBool = true;

    

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

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

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

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

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

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

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

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

  }

}

其中的变量用来存储各种类型的数据,并将其打印到控制台。通过运行这个程序,我们可以清楚地了解每个基本数据类型的范围和大小。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复