首页 后台JAVA正文

java八种基本数据类型及其包装类

yuange JAVA 2018-12-27 1139 0 数据类型

八种基本数据类型以及包装类

1. 基本数据类型

byte

占用1个字节(8位),范围:-2^7~2^7-1


short

占用2个字节(16位),范围:-2^15~2^15-1


int

占用4个字节(32位),范围:-2^31~2^31-1


long

占用8个字节(64位),范围:-2^63~2^63-1


float

占用4个字节(32位,1位符号位,8位指数位),范围:2^-149~2^128-1


double

占用8个字节(64位,1位符号位,11位指数位),范围2^-1074~2^1024-1


表格表示:


基本类型字节数位数最大值最小值

byte1byte8bit2^7 - 1-2^7

short2byte16bit2^15 - 1-2^15

int4byte32bit2^31 - 1-2^31

long8byte64bit2^63 - 1-2^63

float4byte32bit3.4028235E381.4E - 45

double8byte64bit1.7976931348623157E3084.9E - 324

char2byte16bit2^16 - 10

2. 包装类:

short → Short


int → Integer


long → Long


char → Character


byte → Byte


float → Float


boolean → Boolean


double → Double


包装类中提供了更多的方法来对数据进行操作。


基本数据的值存放在栈栈中,包装类是在堆中分配空间给对象,栈中存放的是对对象的引用的地址。因此,包装类的效率会比基本数据类型的效率要低。


3. 包装类的装箱与拆箱

- 装箱


以Integer为例。


将基本数据类型变为包装类,例如


Integer i = 2;


这里自从jdk15后可以这样写,jdk15之前必须显示的new Integer(2),这里实际上是自动调用了Integer.valueOf(int i)方法,Integer.valueOf(int i)源码如下


     public static Integer valueOf(int i) {

        if (i >= IntegerCache.low && i <= IntegerCache.high)

            return IntegerCache.cache[i + (-IntegerCache.low)];

        return new Integer(i);

    }

1

2

3

4

5

发现里边有个IntegerCache,是什么东西呢。再看源码


    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() {}

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

可以看出这个类中已经默认创建了值为-128~127的Integer对象,所以当我们赋值的时候如果变量值在这个范围内,则会直接引用这个存在的对象,不必再new新对象,这种缓存机制,可以减少大量的new对象,提高效率,并且其中的high值是可以配置的。配置方法 

-XX:AutoBoxCacheMax=< size >


float、double类似。


在idea中可以这添加启动参数来配置:




对此,有以下代码:


    public static void main(String[] args) {

        Integer a = 1;

        Integer b = 1;

        System.out.println(a==b);


        Integer c = 200;

        Integer d = 200;

        System.out.println(c==d);

    }

1

2

3

4

5

6

7

8

9

输出为:


true


false

1

2

3

在配置了high的值(例如1000)后输出都为true.


因为a和b在[-128,127]之间,引用的是同一个对象,所以相等。


而c和d虽然值都为200,但是不在[-128~127]之间,是两个完全不同的对象,所以不相等。


所以,判断两个Integer的值是否相等,应该用equlas方法。


- 拆箱


将包装类变为基本数据类型,例如


Integer i = 2;


int j = i;

1

2

3

这里默认调用了intValue()方法实现自动拆箱


intValue()的源码很简单:


     public int intValue() {

        return value;

    }


评论

在线客服-可直接交谈

您好!有什么需要可以为您服务吗?