Java中的基本数据类型和包装类型的知识,你都知道吗?

2020年6月28日15:29:38java评论63阅读模式

Java中的基本数据类型和包装类型

Java 中的基本数据按类型可以分为四大类:布尔型、整数型、浮点型、字符型;
这四大类包含 8 种基本数据类型。文章源自随机的未知-https://sjdwz.com/11146.html

  • 布尔型:boolean
  • 整数型:byte、short、int、long
  • 浮点型:float、double
  • 字符型:char

这8 种基本类型取值如下:文章源自随机的未知-https://sjdwz.com/11146.html

数据类型代表含义默认值取值包装类
boolean布尔型false0(false) 到 1(true)Boolean
byte字节型(byte)0﹣128 到 127Byte
char字符型'\u0000'(空)'\u0000' 到 '\uFFFF'Character
short短整数型(short)0- 到
﹣1Short
int整数型0﹣ 到 ﹣1Integer
long长整数型0L﹣ 到 ﹣1Long
float单浮点型0.0f1.4e-45 到 3.4e+38Float
double双浮点型0.0d4.9e-324 到 1.798e+308Double

我们可以看到除 char 的包装类 Character 和 int 的包装类 Integer之外,
其他基本数据类型的包装类只需要首字母大写即可。包装类的作用和特点,本文下半部分详细讲解。
这些都是我们很熟悉的知识了,那下面的知识你有了解吗?文章源自随机的未知-https://sjdwz.com/11146.html

你可能不知道的知识点

首先我们来看一道题目?
下面这段代码输出什么呢?文章源自随机的未知-https://sjdwz.com/11146.html

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

        Integer number1 = 127;
        Integer number2 = 127;

        System.out.println("number1==number2判断的值为" + (number1 == number2));

        Integer number3 = 128;
        Integer number4 = 128;

        System.out.println("number3==number4判断的值为" + (number3 == number4));
    }
}

让我们来看一下答案:文章源自随机的未知-https://sjdwz.com/11146.html

Java中的基本数据类型和包装类型的知识,你都知道吗?
答案1

number1number2均赋值为了127,number3number4均赋值为了128,
那为什么number1==number2truenumber3==number4false呢?文章源自随机的未知-https://sjdwz.com/11146.html

我们来看一下Integer中的valueOf的源码:文章源自随机的未知-https://sjdwz.com/11146.html

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

我们看到如果传入的参数在**[IntegerCache.low,IntegerCache.high]之间就返回IntegerCache.cache[i + (-IntegerCache.low)]**,如果值没在这里面,就创建一个新对象返回;
实际上这是一个 高频区间的数据缓存,我们再来看看IntegerCache类的实现:文章源自随机的未知-https://sjdwz.com/11146.html

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

我们看到IntegerCache.low为-128,IntegerCache.high为127;
所以在通过valueOf方法创建Integer对象的时候,如果数值在**[-128,127]**之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。
与Integer类似,有高频区间数据缓存的包装类还有:文章源自随机的未知-https://sjdwz.com/11146.html

  • Byte:缓存区 -128~127
  • Short:缓存区 -128~127
  • Character:缓存区 0~127
  • Long:缓存区 -128~127
  • Integer:缓存区 -128~127

我们再来看一下以下代码:文章源自随机的未知-https://sjdwz.com/11146.html

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

            Boolean bool1 = false;
            Boolean bool2 = false;
            Boolean bool3 = true;
            Boolean bool4 = true;

            System.out.println("bool1==bool2判断的值为"+(bool1==bool1));
            System.out.println("bool3==bool4判断的值为"+(bool3==bool4));
        }
}

让我们来看一下答案:文章源自随机的未知-https://sjdwz.com/11146.html

Java中的基本数据类型和包装类型的知识,你都知道吗?
答案2

我们来看一下Boolean的valueOf代码:文章源自随机的未知-https://sjdwz.com/11146.html

public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

再来看一下TRUE和FALSE的定义:文章源自随机的未知-https://sjdwz.com/11146.html

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);

可以看到它们使用静态 final 定义,就会返回静态值,所以答案2中返回都是true。文章源自随机的未知-https://sjdwz.com/11146.html

Double、Float的valueOf方法的实现是类似的,但是它们的valueOf与Integer、Short、Byte、Character、Long的不同。文章源自随机的未知-https://sjdwz.com/11146.html

我们再看一下下面的代码:文章源自随机的未知-https://sjdwz.com/11146.html

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

        Double d1 = 20.0;
        Double d2 = 20.0;

        System.out.println("d1==d2判断的值为" + (d1 == d2));

        Double d3 = 30.0;
        Double d4 = 30.0;

        System.out.println("d3==d4判断的值为" + (d3 == d4));
    }
}

让我们来看一下答案:文章源自随机的未知-https://sjdwz.com/11146.html

Java中的基本数据类型和包装类型的知识,你都知道吗?
答案3

看一下Double类型的valueOf的实现文章源自随机的未知-https://sjdwz.com/11146.html

public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
}

它会返回一个新的Double对象。
看一下Float类型的valueOf的实现文章源自随机的未知-https://sjdwz.com/11146.html

public static Float valueOf(String s) throws NumberFormatException {
        return new Float(parseFloat(s));
}

Float类型的valueOf的实现与Double类型类似。文章源自随机的未知-https://sjdwz.com/11146.html

欢迎关注

扫下方二维码即可关注,微信公众号:随机的未知文章源自随机的未知-https://sjdwz.com/11146.html

Java中的基本数据类型和包装类型的知识,你都知道吗?
微信公众号:随机的未知
文章源自随机的未知-https://sjdwz.com/11146.html
欢迎关注本站微信公众号:随机的未知 如果喜欢本文,欢迎点赞,收藏,转发,打赏。
java最后更新:2022-2-24
  • 本文由 发表于 2020年6月28日15:29:38
  • 转载请注明:来源:随机的未知 本文链接https://sjdwz.com/11146.html
java

详解java接口interface

引言 接口这个词在生活中我们并不陌生。 在中国大陆,我们可以将自己的家用电器的插头插到符合它插口的插座上; 我们在戴尔,惠普,联想,苹果等品牌电脑之间传输数据时,可以使用U盘进行传输。 插座的普适性是...
java

Java中的抽象类和抽象方法

引言 实例图片 如上图,二维图形类有三个子类,分别是正方形类,三角形类,圆形类; 我们都知道要求正方形的面积,直接使用面积公式边长的平方即可,同理三角形的是底乘高除以2,圆的面积是**乘以半径的平方。...
java

Java多线程中的虚假唤醒和如何避免

先来看一个例子 一个卖面的面馆,有一个做面的厨师和一个吃面的食客,需要保证,厨师做一碗面,食客吃一碗面,不能一次性多做几碗面,更不能没有面的时候吃面;按照上述操作,进行十轮做面吃面的操作。 用代码说话...
java

谈谈Java中的反射机制

前言 在使用框架进行开发时,我们的开发速度大大提升。我们感叹于它的神奇之处,我们使用它的时候,也要知道其“灵魂”。正所谓,无反射,不框架,框架的灵魂就是反射。 另外,我们在eclipse或者IDEA中...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定