|
| 1 | +package com.amir.wrapper; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +/* |
| 9 | + * Integer constant pool depends on literals assignment (autoboxing,valueOf(int) method) |
| 10 | + * if you see this method you will find that there is cache machenism for few numbers |
| 11 | + * IntegerCache class does cached Integer object from [-128 to 127] using static block,and high-value[127] can be configurable |
| 12 | + * means you can configure any high value instead of 127 using -Djava.lang.Integer.IntegerCache.high=<size> |
| 13 | + * java.lang.Integer.IntegerCache.high default value is null; |
| 14 | + * for Character cached working for range of (0,127) |
| 15 | + * |
| 16 | + * there 8 Wrapper classes(4 number+2decimal+1boolean+1char) --Except decimal(float & double) ..caching are working for all 6 wrapper type. |
| 17 | + * and only for Integer we can change max value: |
| 18 | + * |
| 19 | + */ |
| 20 | +public class WrapperDemoTest { |
| 21 | + |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testNumber127Cp() { |
| 25 | + |
| 26 | + Byte i0=Byte.valueOf((byte) 127); |
| 27 | + Byte i01=127; |
| 28 | + assertTrue(i0==i01); |
| 29 | + |
| 30 | + Short i02=Short.valueOf((short) 127); |
| 31 | + Short i03=127; |
| 32 | + assertTrue(i02==i03); |
| 33 | + |
| 34 | + |
| 35 | + Integer i1=Integer.valueOf(127); |
| 36 | + Integer i2=127; |
| 37 | + assertTrue(i1==i2); |
| 38 | + |
| 39 | + |
| 40 | + Long i3=Long.valueOf(127L); |
| 41 | + Long i4=127L; |
| 42 | + assertTrue(i4==i3); |
| 43 | + |
| 44 | + Double d1=10.4; |
| 45 | + Double d2=Double.valueOf(10.4); |
| 46 | + |
| 47 | + assertFalse(d1==d2); |
| 48 | + |
| 49 | + Float f1=Float.valueOf(10.4f); |
| 50 | + Float f2=10.4f; |
| 51 | + |
| 52 | + assertFalse(f1==f2); |
| 53 | + |
| 54 | + Character c1 = Character.valueOf('z'); |
| 55 | + Character c2 ='z'; |
| 56 | + System.out.println((int)c2); |
| 57 | + assertTrue(c1==c2); |
| 58 | + |
| 59 | + Boolean b1 = Boolean.valueOf(false); |
| 60 | + Boolean b2 = false; |
| 61 | + assertTrue(b1==b2); |
| 62 | + } |
| 63 | + @Test |
| 64 | + public void testNumber128Cp() { |
| 65 | + Long i1=128l; |
| 66 | + Long i2=Long.valueOf(128); |
| 67 | + assertFalse(i1==i2); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +} |
0 commit comments