Why is java.lang.Integer.valueOf a flyweight pattern?
I tried to find the reason but wasn’t able to.
Why is java.lang.Integer.valueOf a flyweight pattern? I tried to find the reason but wasn’t
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If we look at the source for
valueOf, we can get a hint:Source of java.lang.Integer lines 638-643:
It looks like the Integer class maintains a cache of Integer objects for common values. Rather than make a new one every time somebody asks for
valueOf, it just returns a reference to one that already exists. Therefore, if you callInteger.valueOf(1)multiple times, you’ll end up getting the same object back every time (not just equivalent objects).