Re: primitive wrapper classes
Ed wrote:
On Sep 21, 10:13 pm, Patricia Shanahan <p...@acm.org> wrote:
Ed wrote:
On Sep 21, 2:34 pm, "mike7...@gmail.com" <mike7...@gmail.com> wrote:
I was just wondering what the main purpose of the primitive wrapper
classes
is in Java.
Thank you.
They can be more useful when arrays of int(s), float(s) and so on are
required!
Here is an example:
Integer[] ints = new Integer[2];
ints[0]=5;
ints[1]=10;
int int0=ints[0].intValue();
int int1=ints[1].intValue();
System.out.println("int0 :"+int0);
System.out.println("int1 :"+int1);
Regards,
Ed
What is the advantage of that compared to the following?
int[] ints = new int[2];
ints[0] = 5;
ints[1] = 10;
int0 = ints[0];
...
Patricia- Hide quoted text -
- Show quoted text -
There are some, like providing a ceratin number of utility methods!
Most of the Integer utility methods are static, and use int, rather than
Integer, for any integer parameters. Another subset provide Integer
substitutes for int casting.
The remainder are the basic Comparable Object methods. We don't need
Comparable to sort an int[], because Arrays has a sort that applies to
int[] directly. For more general use, Integer equals and compareTo
reproduce the effect of int comparison. Integer hashCode merely returns
the int value, which we have anyway in an int[].
I still don't see how any of the utility methods would drive the use of
an Integer[] in preference to int[]. Perhaps you could give an example
of some code where Integer[] would be better than int[]?
Of course, the wrapper types are very useful for java.util Collections,
but that is a different issue from freestanding arrays.
Patricia