"new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool
Firas Zuriekat
Ranch Hand
Joined: May 09, 2006
Posts: 143
posted
0
public class Tester { public static void main (String[] args) { int x = 5; Integer x1 = x; Integer x2 = x; int x3 = new Integer(5); int x4=new Integer("5"); System.out.print(x1.equals(x)); System.out.print(x1 == x); System.out.print(x2.equals(x1)); System.out.print(x2 == x1); System.out.print(x2 == x3); System.out.print(x2.equals(x3)); System.out.print(x4==x3); // x4, x3 aren't in the pool because of "new" was used to create a fresh objects. So why this prints "true"? } }
OUTPUT: truetruetruetruetruetruetrue
So why "new" here doesn't create a new object for x3 and x4? ("true" printed for all!!!) [ May 31, 2006: Message edited by: Firas Zureikat ]
wise owen
Ranch Hand
Joined: Feb 02, 2006
Posts: 2023
posted
0
x3 and x4 are primtive data type.
Firas Zuriekat
Ranch Hand
Joined: May 09, 2006
Posts: 143
posted
0
Thank you...I just noticed....It's so important to look really close on the code :-)
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
posted
0
what is the concept of pooling in primitives. Any one please explain
"Know where to find the solution and how to use it - that's the secret of success."
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
This is what the Java Language Specification says.
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.