Java的八种基本类型和String

概述

Java的数据类型分为基本数据类型引用数据类型

使用关键字new来创建对象,创建的对象都存放在内存的中。而对于使用new来创建占用空间小的或者简单的变量来说,会显得比较啰嗦,所以就出现了Primitive type。他们的值存放在内存的堆栈中。Java为每种基本数据类型定义了存储空间大小,这种大小是固定的,不会随机器架构的不同而改变。

Primitive typeSizeScopeWrapper type
boolean--Boolean
byte8 bits-128 ~ 127Byte
short16 bits-215 ~ 215 -1Short
char16 bits-215 ~ 215 -1Character
int32 bits-231 ~ 231 -1Integer
long64 bits-263 ~ 263 -1Long
float32 bits-231 ~ 231 -1Float
double64 bits-263 ~ 263 -1Double

拆箱与装箱

由上可知:

  • Primitive types --> stack
  • Reference types(new places objects) --> heap

装箱

基本数据类型 —> 对象类型

自动装箱的实现步骤为:

	Integer num = 128; 
==> Integer num = Integer.valueOf(128);
==> Integer num = new Integer(128);

JDK中Integer.valueOf()源码如下:

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

拆箱

对象 —> 基本数据类型

自动拆箱的实现步骤为:

源码:

Integer num = 10;
int a = num;

编译后的代码:

Integer num = Integer.valueOf(10);
int a = num.intValue();

在拆箱的过程中,其内部调用的是 Integer中的intValue() 方法。

int与Integer比较

public static void test2() {
    boolean result;

    //
    int num1 = 128;
    int num2 = 128;
    result = num1 == num2;

    // 输出结果:true
    System.out.println("int == int: " + result);
    //

    //
    Integer num3 = 127;
    Integer num4 = 127;
    result = num3 == num4;

    // 输出结果:true
    System.out.println("Integer == Integer: " + result);
    //

    //
    Integer num5 = 128;
    Integer num6 = 128;
    result = num5 == num6;

    // 输出结果:false
    System.out.println("Integer == Integer: " + result);
    //
}
  • 基本数据类型在stack中,如果存在了值,则直接获取该值;如果不存在,则在stack中添加该值,所以num1 == num2。
  • 要理解num3 == num4结果为true,而num5 == num6结果为false,则要看Integer.valueOf()源码。
public static Integer valueOf(int i) {
    if (i >= Integer.IntegerCache.low && i <= Integer.IntegerCache.high)
        return Integer.IntegerCache.cache[i + (-Integer.IntegerCache.low)];
    return new Integer(i);
}

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];		// 创建一个Integer对象数组

    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];   // 设置数组大小为256
        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 Integer.IntegerCache.high >= 127;
    }

    private IntegerCache() {}
}

由上代码可知,在自动装箱的时候,如果int值在[-128, 127]之间,那么装箱的时候,直接赋值一个Integer对象,所以num3 == num4结果为true,而num5 == num6结果为false。再来看以下例子:

public static void test1() {
    boolean result;     // 判断结果

    //
    Integer num1 = new Integer(128);
    int num2 = 128;
    result = num1 == num2;

    // 输出结果:true
    System.out.println("new Integer == int: " + result);
    //

    //
    int num3 = 150;
    Integer num4 = new Integer(150);
    result = num3 == num4;

    // 输出结果:true
    System.out.println("int == new Integer: " + result);
    //


    //
    int num = 127;
    Integer num5 = new Integer(num);
    Integer num6 = new Integer(num);
    result = num5 == num6;

    // 输出结果:false
    System.out.println("new Integer == new Integer: " + result);
    //

}

int与Integer在使用==比较时,会先把Integer自动拆箱,然后比较两个值是否相等。

Integer num1 = new Integer(128);
int num2 = 128;
result = (num1.intValue() == num2);

范围越界

是否存在 x>x+1?为什么?

public static void main(String[] args) {
    byte a = 127;
    a += 5;
    System.out.println(a);    // -124
}

当超过最大临界值后,会变为最小值。

String类

String概述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值