Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #25069
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Autoboxing and Performance ? |
| Date | 2013-08-20 08:08 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <kuvm84$11g$1@dont-email.me> (permalink) |
| References | <fcudnTCLD6xZ3I7PnZ2dnUVZ_gadnZ2d@giganews.com> <wbsfj3kz9x6.1hthr4dxr2yvl$.dlg@40tude.net> <cLydnRPXuKJn2I7PnZ2dnUVZ_jydnZ2d@giganews.com> <slrnl16i32.q59.avl@gamma.logic.tuwien.ac.at> <Wt2dnWWN8Yvf0I7PnZ2dnUVZ_r2dnZ2d@giganews.com> |
On 8/20/2013 7:04 AM, Peter Olcott wrote:
> On 8/20/2013 5:43 AM, Andreas Leitgeb wrote:
>> Peter Olcott <OCR4Screen> wrote:
>>> On 8/20/2013 5:20 AM, Joerg Meier wrote:
>>>> On Tue, 20 Aug 2013 05:15:30 -0500, Peter Olcott wrote:
>>>>
>>>>> From Java A Beginner's Guide, Fifth Edition by Herbert Schildt,
>>>>> page 425:
>>>>
>>>>> class AutoBox3 {
>>>>> public static void main(String args[]) {
>>>>> Integer iOb;
>>>>> iOb = 99;
>>>>
>>>>> // The following automatically unboxes iOb,
>>>>> // performs the increment, and then reboxes
>>>>> // the result back into iOb
>>>>> ++iOb;
>>>>
>>>>> It seems to me that all this unboxing and reboxing is a complete waste
>>>>> of CPU time. It would make much more sense to provide an
>>>>> Integer.Increment() member function that directly operates on the
>>>>> contained data.
>>>>
>>>>> Why does Java waste so much CPU resources with its unboxing and
>>>>> reboxing
>>>>> in cases such as this, and not simply provide member functions that
>>>>> directly operate on the contained data?
>>>>
>>>> Because IntegerS and other Number classes are immutable. The
>>>> contained data
>>>> is not allowed to be changed under any circumstances.
>>>>
>>>> Liebe Gruesse,
>>>> Joerg
>>>>
>>>
>>> So the original object is replaced with a new object?
>>>
>>> What is the benefit (of immutability) that is worth the cost of this
>>> poor performance?
>>
>> The lesson probably should be, that if you intend to do math on
>> a variable, that you should use the primitive types instead.
>>
>> class NoAutoBox {
>> public static void main(String args[]) {
>> int iOb;
>> iOb = 99;
>> ++iOb; // value is overwritten; no objects created.
>> }
>> }
>>
>> PS: doing math on them is not the only use for integral numeric types.
>>
>
> I am trying to understand the Java language design principles that are
> underlying the reasoning why there is such a thing as unboxing and
> reboxing. I can see why class objects should be kept in a box, no
> unboxing or reboxing required.
>
> I do not see why primitive objects must be kept in a box, or why they
> must be unboxed and reboxed if they are to be operated upon.
There's no "must," there's a "may."
At a low level Java cannot do arithmetic with Integer (or
Long or ...) objects, but only with int (long, ...) primitive
values. And in the early days this was explicit: In order to
perform the increment in your example, you had to write
Integer iOb = Integer.valueOf(99);
iOb = Integer.valueOf(iOb.intValue() + 1);
... or something in that vein. Boxing and unboxing just amount
to the compiler writing the boiler-plate for you; the same stuff
is still going on behind the scenes.
So, why have Integer (Long, ...) objects in the first place?
Because there are contexts where you *must* have an object instance,
and a primitive value will not do. For example, an ArrayList cannot
hold primitives, only object references. The keys and values in a
HashMap cannot be primitives, only object references. You cannot
synchronize on a primitive value, you cannot set a primitive to
`null', you cannot apply the `instanceof' operator to a primitive,
and the list goes on. That's what Integer and its pals are for:
When you you want to use a value in a context that needs an object.
IMHO boxing and unboxing are snares of Satan, creating more
confusion (e.g., this thread!) and trouble than they're worth.
The deeper problem, it seems to me, is that Java is unable to treat
a primitive the same way it treats an object: Why *shouldn't* you
be able to put 3.14159 in an ArrayList, or ask whether it is an
`instanceof short'? However, the primitive-vs-reference chasm is
a deep divide in the design of Java, and there seems little chance
that it could ever go away. Boxing and unboxing bridge the chasm,
but the bridge is rickety and you should tread it with care.
--
Eric Sosman
esosman@comcast-dot-net.invalid
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Autoboxing and Performance ? Peter Olcott <OCR4Screen> - 2013-08-20 05:15 -0500
Re: Autoboxing and Performance ? Joerg Meier <joergmmeier@arcor.de> - 2013-08-20 12:20 +0200
Re: Autoboxing and Performance ? Peter Olcott <OCR4Screen> - 2013-08-20 05:33 -0500
Re: Autoboxing and Performance ? Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-08-20 10:43 +0000
Re: Autoboxing and Performance ? Peter Olcott <OCR4Screen> - 2013-08-20 06:04 -0500
Re: Autoboxing and Performance ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-20 08:08 -0400
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-20 20:15 -0400
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-20 20:12 -0400
Re: Autoboxing and Performance ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-21 03:52 +0300
Re: Autoboxing and Performance ? Sebastian <news@seyweiler.dyndns.org> - 2013-08-21 07:27 +0200
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-21 18:43 -0400
Re: Autoboxing and Performance ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-22 08:08 +0300
Re: Autoboxing and Performance ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-22 07:56 +0300
Re: Autoboxing and Performance ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-08-22 10:51 -0700
Re: Autoboxing and Performance ? Martin Gregorie <martin@address-in-sig.invalid> - 2013-08-22 19:36 +0000
Re: Autoboxing and Performance ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-08-22 13:01 -0700
Re: Autoboxing and Performance ? Martin Gregorie <martin@address-in-sig.invalid> - 2013-08-22 21:19 +0000
Re: Autoboxing and Performance ? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-08-22 14:39 -0700
Re: Autoboxing and Performance ? markspace <markspace@nospam.nospam> - 2013-08-22 19:50 -0700
Re: Autoboxing and Performance ? Martin Gregorie <martin@address-in-sig.invalid> - 2013-08-23 23:16 +0000
Re: Autoboxing and Performance ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-24 17:20 +0300
Re: Autoboxing and Performance ? Lew <lewbloch@gmail.com> - 2013-08-23 11:08 -0700
Re: Autoboxing and Performance ? Joerg Meier <joergmmeier@arcor.de> - 2013-08-23 22:52 +0200
Re: Autoboxing and Performance ? Jim Gibson <jimsgibson@gmail.com> - 2013-08-23 16:38 -0700
Re: Autoboxing and Performance ? "John B. Matthews" <nospam@nospam.invalid> - 2013-08-23 06:17 -0400
Re: Autoboxing and Performance ? Jeff Higgins <jeff@invalid.invalid> - 2013-08-23 17:25 -0400
Re: Autoboxing and Performance ? Jeff Higgins <jeff@invalid.invalid> - 2013-08-25 18:34 -0400
Re: Autoboxing and Performance ? Jeff Higgins <jeff@invalid.invalid> - 2013-08-28 10:52 -0400
Re: Autoboxing and Performance ? Martin Gregorie <martin@address-in-sig.invalid> - 2013-08-23 23:37 +0000
Re: Autoboxing and Performance ? Robert Klemme <shortcutter@googlemail.com> - 2013-08-26 19:33 +0200
Re: Autoboxing and Performance ? Sven Köhler <remove-sven.koehler@gmail.com> - 2013-08-29 10:09 +0300
Re: Autoboxing and Performance ? Jan Burse <janburse@fastmail.fm> - 2013-08-29 14:14 +0200
Re: Autoboxing and Performance ? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-08-29 21:28 -0300
Re: Autoboxing and Performance ? Jan Burse <janburse@fastmail.fm> - 2013-08-20 20:03 +0200
Re: Autoboxing and Performance ? Lew <lewbloch@gmail.com> - 2013-08-20 16:23 -0700
Re: Autoboxing and Performance ? Jan Burse <janburse@fastmail.fm> - 2013-08-21 11:51 +0200
Re: Autoboxing and Performance ? Lew <lewbloch@gmail.com> - 2013-08-21 08:39 -0700
Re: Autoboxing and Performance ? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-21 16:51 +0100
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-21 19:19 -0400
Re: Autoboxing and Performance ? Jan Burse <janburse@fastmail.fm> - 2013-08-22 09:49 +0200
Re: Autoboxing and Performance ? Jan Burse <janburse@fastmail.fm> - 2013-08-21 11:54 +0200
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-20 20:06 -0400
Re: Autoboxing and Performance ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-20 21:19 -0400
Re: Autoboxing and Performance ? Joerg Meier <joergmmeier@arcor.de> - 2013-08-21 10:05 +0200
Re: Autoboxing and Performance ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-21 07:19 -0400
Re: Autoboxing and Performance ? Joerg Meier <joergmmeier@arcor.de> - 2013-08-22 00:18 +0200
Re: Autoboxing and Performance ? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-21 19:58 -0400
Re: Autoboxing and Performance ? Lew <lewbloch@gmail.com> - 2013-08-21 08:36 -0700
Re: Autoboxing and Performance ? taqmcg@gmail.com - 2013-08-21 09:37 -0700
Re: Autoboxing and Performance ? Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2013-08-20 23:28 -0500
Re: Autoboxing and Performance ? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-21 10:09 +0100
Re: Autoboxing and Performance ? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-21 19:04 -0400
Re: Autoboxing and Performance ? Arivald <NOSPAMarivald@interia.pl> - 2013-08-22 07:39 +0200
Re: Autoboxing and Performance ? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-08-29 01:42 -0300
csiph-web