Weird Integer boxing in Java

前端 未结 12 1953
广开言路
广开言路 2020-11-22 00:15

I just saw code similar to this:

public class Scratch
{
    public static void main(String[] args)
    {
        Integer a = 1000, b = 1000;
        System.o         


        
12条回答
  •  灰色年华
    2020-11-22 00:36

    In Java 5, a new feature was introduced to save the memory and improve performance for Integer type objects handlings. Integer objects are cached internally and reused via the same referenced objects.

    1. This is applicable for Integer values in range between –127 to +127 (Max Integer value).

    2. This Integer caching works only on autoboxing. Integer objects will not be cached when they are built using the constructor.

    For more detail pls go through below Link:

    Integer Cache in Detail

提交回复
热议问题