Skip to content

常量池技术 超出[-128,127] ,会重新创建新的对象实例,那会把这个实例缓存起来吗? #461

@jol5

Description

@jol5

Java 基本类型的包装类的大部分都实现了常量池技术,即 Byte,Short,Integer,Long,Character,Boolean;这 5 种包装类默认创建了数值[-128,127] 的相应类型的缓存数据,但是超出此范围仍然会去创建新的对象。

如:
Integer one=333;
Integer two=333;

在创建one,是不是不会把对应的结果缓存起来啊?这才导致 one != two;
问:表面上时不会缓存的,那为什么不缓存呢?如果不缓存,为什么又要预先创建[-128,127] 呢?

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions