Java Forum / General / September 2007
primitive wrapper classes
mike7411@gmail.com - 21 Sep 2007 22:34 GMT I was just wondering what the main purpose of the primitive wrapper classes is in Java.
Thank you.
Manish Pandit - 21 Sep 2007 22:43 GMT 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 are used extensively in collections and other places where 'Object' is required (like reflection). However, since 1.5, due to autoboxing/unboxing you do not have to use wrappers anymore for collections.
-cheers, Manish
Daniel Pitts - 22 Sep 2007 18:37 GMT > On Sep 21, 2:34 pm, "mike7...@gmail.com" <mike7...@gmail.com> wrote: > [quoted text clipped - 11 lines] > -cheers, > Manish Actually, you DO have to use the wrappers, its just more transparent that you're doing so.
Roedy Green - 21 Sep 2007 23:46 GMT On Fri, 21 Sep 2007 14:34:31 -0700, "mike7411@gmail.com" <mike7411@gmail.com> wrote, quoted or indirectly quoted someone who said :
>I was just wondering what the main purpose of the primitive wrapper >classes >is in Java. Just in case you want to change them later to do something interesting, e.g. data validation, monitoring, virtual computed fields, caching etc etc. Your client code is all ready to go.
Other languages such as Eiffel handle this more elegantly without dummy wrappers.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Manish Pandit - 22 Sep 2007 00:52 GMT On Sep 21, 3:46 pm, Roedy Green <see_webs...@mindprod.com.invalid> wrote:
> On Fri, 21 Sep 2007 14:34:31 -0700, "mike7...@gmail.com" > <mike7...@gmail.com> wrote, quoted or indirectly quoted someone who [quoted text clipped - 13 lines] > Roedy Green Canadian Mind Products > The Java Glossaryhttp://mindprod.com I believe the OP meant Integer vs. int, Boolean vs. boolean, Float vs. float, et. al. wrappers.
-cheers, Manish
Roedy Green - 22 Sep 2007 04:17 GMT On Fri, 21 Sep 2007 16:52:22 -0700, Manish Pandit <pandit.manish@gmail.com> wrote, quoted or indirectly quoted someone who said :
>I believe the OP meant Integer vs. int, Boolean vs. boolean, Float vs. >float, et. al. wrappers. that what did you mean Mike? I answered as if you meant get/set wrapper methods around primitive instance variables.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Ed - 22 Sep 2007 05:50 GMT 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
Patricia Shanahan - 22 Sep 2007 06:13 GMT > 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 [quoted text clipped - 17 lines] > 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
Ed - 22 Sep 2007 06:57 GMT > > 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 [quoted text clipped - 29 lines] > > - Show quoted text - There are some, like providing a ceratin number of utility methods!
Patricia Shanahan - 22 Sep 2007 07:45 GMT >>> 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 [quoted text clipped - 26 lines] > > 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
Ed - 22 Sep 2007 08:16 GMT > >>> 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 [quoted text clipped - 47 lines] > > - Show quoted text - Patricia, what the hell, are you talking about!
Ben Phillips - 22 Sep 2007 09:16 GMT > Of course, the wrapper types are very useful for java.util Collections, > but that is a different issue from freestanding arrays. Freestanding arrays have more or less outlived their usefulness anyway. ArrayList is better. And (here's a use for Integer, involving of course a java.util collection) a HashMap with an Integer key type is a quick way to get a sparse array.
I can only see myself using a freestanding array when it's a static, constant array of primitives used for some arcane purpose that should probably be encapsulated. Otherwise, an array under the hood implementing a data structure that is encapsulated, and I have performance concerns about using a collection object in its place, backed up by data. (If it runs 20% faster implemented on an array instead of an ArrayList, go for it!)
Ed - 22 Sep 2007 17:49 GMT > > Of course, the wrapper types are very useful for java.util Collections, > > but that is a different issue from freestanding arrays. [quoted text clipped - 11 lines] > backed up by data. (If it runs 20% faster implemented on an array > instead of an ArrayList, go for it!) Ben, Patricia just wanted to argue with me! He just wanted to discredit me, as he is in the same gang of idiots who are attacking me, here, so that's why I did ignore him! You should not have wasted your time, responding to him, on the issue!
Joshua Cranmer - 22 Sep 2007 18:06 GMT > Ben, Patricia just wanted to argue with me! He just wanted to > discredit me, as he is in the same gang of idiots who are attacking > me, here, so that's why I did ignore him! You should not have wasted > your time, responding to him, on the issue! I disagree with that prognosis. You did not state any reason to use Integer[] over int[] except for a vague reference to utility methods. Patricia then asked you for an example and you reply by ignoring the posts altogether.
In no way, shape, or form did Patricia attack you. It is actually you who is doing the attacking here.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Roedy Green - 22 Sep 2007 06:26 GMT On Fri, 21 Sep 2007 14:34:31 -0700, "mike7411@gmail.com" <mike7411@gmail.com> wrote, quoted or indirectly quoted someone who said :
>I was just wondering what the main purpose of the primitive wrapper >classes >is in Java. If you mean the immutable wrappers like Long and Double, for one thing you can write a method that takes a Number, and pass it a Long, Double, Integer etc. The method can process the parm differently depending on its actual type.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Eric Sosman - 22 Sep 2007 13:41 GMT > On Fri, 21 Sep 2007 14:34:31 -0700, "mike7411@gmail.com" > <mike7411@gmail.com> wrote, quoted or indirectly quoted someone who [quoted text clipped - 8 lines] > Double, Integer etc. The method can process the parm differently > depending on its actual type. That doesn't seem like a big advantage. If the method processes the argument differently depending on its actual type, then it's really just N methods folded into one and divided up by `instanceof' tests or equivalents. (The dodge of using doubleValue() on all types indiscriminately runs into trouble with Longs of large magnitude.)
Note, too, that Number is non-final: a method that takes a Number and makes decisions based on `instanceof' cannot hope to enumerate all subclasses of Number. Indeed, the suite of native-to-the-JRE classes that implement Number has grown with succeeding Java versions. (Of course, things like BigDecimal are not wrappers for primitives, and things like AtomicLong are not immutable. Still, the problem of writing the method remains: You might test for a fixed set of "expected" Number subclasses and throw IllegalArgumentException if given something else, but ...)
This isn't to say that methods taking or returning Number can't be useful; they certainly can be. But methods that choose different paths by peeping through the Number curtain limit their own usefulness.
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Lew - 22 Sep 2007 14:49 GMT Roedy Green wrote:
>> If you mean the immutable wrappers like Long and Double, for one thing >> you can write a method that takes a Number, and pass it a Long, >> Double, Integer etc. The method can process the parm differently >> depending on its actual type.
> That doesn't seem like a big advantage. If the method > processes the argument differently depending on its actual > type, then it's really just N methods folded into one and > divided up by `instanceof' tests or equivalents. (The dodge > of using doubleValue() on all types indiscriminately runs into > trouble with Longs of large magnitude.) In general a switch on type is a strong indicator of a need for overloads or overrides.
 Signature Lew
Joshua Cranmer - 22 Sep 2007 15:20 GMT >> On Fri, 21 Sep 2007 14:34:31 -0700, "mike7411@gmail.com" >> <mike7411@gmail.com> wrote, quoted or indirectly quoted someone who [quoted text clipped - 13 lines] > different paths by peeping through the Number curtain limit > their own usefulness. I see the biggest use for Number being in writing generified mathematics code:
public class Matrix<T extends Number> { }
Although auto{un}boxing uses the primitive wrappers "under the hood," the primitive wrappers probably remain the most useful now when dealing with generics.
 Signature Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
Eric Sosman - 23 Sep 2007 18:15 GMT >>> On Fri, 21 Sep 2007 14:34:31 -0700, "mike7411@gmail.com" >>> <mike7411@gmail.com> wrote, quoted or indirectly quoted someone who [quoted text clipped - 19 lines] > public class Matrix<T extends Number> { > } Writing the signatures for such a class is no great hassle, but writing the methods themselves can be. For example, how would you implement the Matrix instance method
/** Calculates the determinant of a square matrix. * @return The matrix' determinant. * @throws UnsupportedOperationException if the matrix * is not square. */ T determinant() { // TODO: code }
The challenge is to write code that works for all possible T: Integer, Double, Complex, BigDecimal, Fraction, ... Just to get started, how do you calculate the sum of two T values?
 Signature Eric Sosman esosman@ieee-dot-org.invalid
Roedy Green - 22 Sep 2007 18:23 GMT On Sat, 22 Sep 2007 08:41:37 -0400, Eric Sosman <esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted someone who said :
>> If you mean the immutable wrappers like Long and Double, for one thing >> you can write a method that takes a Number, and pass it a Long, >> Double, Integer etc. The method can process the parm differently >> depending on its actual type. > > That doesn't seem like a big advantage. That is how Sun uses them in JSpinner for example. Another way of looking at it is you can implement a generic interface in only a specific way.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 22 Sep 2007 18:29 GMT On Sat, 22 Sep 2007 05:26:50 GMT, Roedy Green <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted someone who said :
>If you mean the immutable wrappers like Long and Double, One advantage of wrapped values is they can have methods attached, and you can override the methods, like compareTo.
Another advantage is you can use wrapped values any place an "object" is required. You can't use primitives there. The creators of ArrayList had to specially code versions for the various primitives.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Jean-Baptiste Nizet - 24 Sep 2007 13:13 GMT On Sep 21, 11: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. Another purpose that nobody has mentioned yet is that instances of primitive wrappers may have one useful additional value to their primitive counterpart: null.
It's very important for database-based applications, for example, where a column containing primitive values might also contain null values. In this case, Integer is used instead of int to store the value, in order to support the null value.
JB.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|