aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool" Watch ""new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool" New topic
Author

"new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool

Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
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
x3 and x4 are primtive data type.
Firas Zuriekat
Ranch Hand

Joined: May 09, 2006
Posts: 144
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
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: 2367
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "new Integer("5")" creates a fresh object & not utilizing the pool- like strings pool