Integer flyweight implementation

Here is a unit test that illustrates the Fly Weight implementation of Integers in Java.

import junit.framework.TestCase;

public final class TestIntegers extends TestCase {
    public void testValueOfGivesTheSameIntegerForSmallValues() {
        assertSame(Integer.valueOf(1), Integer.valueOf(1));
    }

    public void testValueOfGivesADifferentIntegerForSomeValues() {
        assertNotSame(Integer.valueOf(13333333), Integer.valueOf(13333333));
        assertEquals(Integer.valueOf(13333333), Integer.valueOf(13333333));
    }

    public void testNew() {
        assertNotSame(new Integer(1), new Integer(1));
        assertEquals(new Integer(1), new Integer(1));
    }

    public void testValueOfGivesTheSameIntegerForAutoBoxing() {
        assertSame((Integer) 1, (Integer) 1);
    }

    public void testValueOfGivesADifferentIntegerForAutoBoxing() {
        assertNotSame((Integer) 13333333, (Integer) 13333333);
        assertEquals((Integer) 13333333, (Integer) 13333333);
    }
}

One Response to “Integer flyweight implementation”

  1. mckinney Says:

    I must express thanks to this writer for bailing me out of such a predicament. Right after scouting through the search engines and getting advice which are not powerful, I believed my life was well over. Living without the solutions to the difficulties you’ve fixed by means of your good site is a crucial case, as well as ones which could have in a wrong way damaged my career if I hadn’t discovered the website. Your personal ability and kindness in taking care of almost everything was invaluable. I am not sure what I would have done if I had not discovered such a subject like this. I can also at this time look forward to my future. Thanks a lot very much for your reliable and results-oriented guide. I will not hesitate to suggest your web blog to any person who desires direction about this situation.

Leave a Reply