首页 > 编程语言 >Java基本数据类型

Java基本数据类型

时间:2022-11-25 22:24:50浏览次数:69  
标签:基本 code Java 数据类型 value final static public

Java基本数据类型


1. 整型

  • byte
    1字节空间,取值范围-2^7~(2^7)-1,二进制首位为符号位,二进制表示0_000_0000~1_111_11111_000_0000记为-128
    /**
     * A constant holding the minimum value a {@code byte} can
     * have, -2^7.
     */
    public static final byte   MIN_VALUE = -128;
    /**
     * A constant holding the maximum value a {@code byte} can
     * have, (2^7)-1.
     */
    public static final byte   MAX_VALUE = 127;

image

  • short
    2字节空间,取值范围-2^15~(2^15)-1,二进制首位为符号位,二进制表示0_000_0000_0000_0000~1_111_1111_1111_1111
    /**
     * A constant holding the minimum value a {@code short} can
     * have, -2^15.
     */
    public static final short   MIN_VALUE = -32768;
    /**
     * A constant holding the maximum value a {@code short} can
     * have, (2^15)-1.
     */
    public static final short   MAX_VALUE = 32767;

image

  • int(整数无后缀时默认)
    4字节空间,取值范围-2^31~2^31-1,二进制首位为符号位。
    /**
     * A constant holding the minimum value an {@code int} can
     * have, -2^31.
     */
    @Native public static final int   MIN_VALUE = 0x80000000;
    /**
     * A constant holding the maximum value an {@code int} can
     * have, (2^31)-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff;
  • long
    8字节空间,取值范围-2^63~(2^63)-1,二进制首位为符号位。
    /**
     * A constant holding the minimum value a {@code long} can
     * have, -2^63.
     */
    @Native public static final long MIN_VALUE = 0x8000000000000000L;
    /**
     * A constant holding the maximum value a {@code long} can
     * have, (2^63)-1.
     */
    @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

2. 浮点型 (Java中浮点数采用的是IEEE 754标准)

  • 具体参考Java中float/double取值范围与精度
  • float
    4字节空间,取值范围+/-3.4E+38F(6~7 个有效位)
  • double(浮点数无后缀时默认)
    8字节空间,取值范围+/-1.8E+308 (15 个有效位)

3. char (Java采用的是16位的Unicode字符集)

2字节空间,表示范围\u0000~\uffff

    /**
     * The constant value of this field is the smallest value of type
     * {@code char}, {@code '\u005Cu0000'}.
     *
     * @since   1.0.2
     */
    public static final char MIN_VALUE = '\u0000';
    /**
     * The constant value of this field is the largest value of type
     * {@code char}, {@code '\u005CuFFFF'}.
     *
     * @since   1.0.2
     */
    public static final char MAX_VALUE = '\uFFFF';

4. boolean

2.3.4. The Type boolean
Although the Java Virtual Machine defines a type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on values. Instead, expressions in the Java programming language that operate on values are compiled to use values of the Java Virtual Machine data type. booleanbooleanbooleanint
The Java Virtual Machine does directly support arrays. Its newarray instruction (§newarray) enables creation of arrays. Arrays of type are accessed and modified using the array instructions baload and bastore (§baload, §bastore). booleanbooleanbooleanbyte
In Oracle’s Java Virtual Machine implementation, arrays in the Java programming language are encoded as Java Virtual Machine arrays, using 8 bits per element. booleanbyteboolean
The Java Virtual Machine encodes array components using to represent and to represent . Where Java programming language values are mapped by compilers to values of Java Virtual Machine type , the compilers must use the same encoding. boolean1true0falsebooleanint

以上是Oracle发布的《Java虚拟机规范》中关于boolean的解释原文
意思大概是:

  • JVM没有提供boolean类型专用的字节指令,而是使用int相关指令来代替。
  • 对boolean数组的访问与修改,会共用byte数组的baload和bastore指令。
  • 简单来说就是:单boolean值占4字节空间,boolean数组则是每个元素占1字节空间。(boolean实际只需要1位空间)

待续。。。

标签:基本,code,Java,数据类型,value,final,static,public
From: https://www.cnblogs.com/fire-cat/p/16926551.html

相关文章

  • 记一次java四舍五入错误结果的问题
    1.背景在进行除法计算时,结果不符合预期2.测试过程double精度问题,导致四舍五入结果误差BigDecimalb1=newBigDecimal(0.245);BigDecimalb2=new......
  • JAVA数据类型拓展
    数据类型拓展,基础面试题讲解整数//整数拓展进制二进制0b十进制八进制0十六进制0xinti=10;inti2=0b10;inti3=010;......
  • maven的基本使用
    本文摘要:理解并实现分模块开发能够使用聚合工程快速构建项目能够使用继承简化项目配置能够根据需求配置生成、开发、测试环境,并在各个环境间切换运行了解Maven的私服......
  • jenkins pipline 基本语法详解(第四周)
    pipline简介pipline运行在jenkins2.X版本的核心插件,Pipline就是一套运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成......
  • ACM 模式下的Java
    一、引入包相关importjava.util.*;二、基本输入相关涉及到输入需要提前创建一个键盘接收器Scannercin=newScaner(System.in);1、输入一个基本数据结构按照by......
  • Java SPI 机制浅析
    本文通过探析JDK提供的,在开源项目中比较常用的JavaSPI机制,希望给大家在实际开发实践、学习开源项目提供参考。一、SPI是什么SPI全称ServiceProviderInterface,......
  • Java 数组拷贝的几种方式
    目前在Java中数据拷贝提供了如下方式:1、clone2、System.arraycopy3、Arrays.copyOf4、Arrays.copyOfRange。一、clone方法clone方法是从Object类继承过来的,基本数据......
  • Java 序列化工具
    一、Java自带的序列化Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据、有关对象的类型的信息和存储在对象中数据......
  • Java学习六
    一.小结1.使用语法elemenrType[] arrayRefVar(元素类型[]数组引用变量)或elementType  arrayRefVar[](元素类型数组引用变量[])声明一个数组类型的变量。尽管e......
  • JavaScript中String的match方法详解
      String.prototype.match()**String.prototype.match()方法返回通过一个正则表达式匹配到的字符串结果。**varparagraph='Thequickbrownfoxjumpsove......