Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a unusual codes

Reply
Thread Tools

a unusual codes

 
 
Piotr Kobzda
Guest
Posts: n/a
 
      07-24-2006
Chris Uppal wrote:

> Mark Space wrote:
>> Would be the correct syntax? Or do primitives get promoted to objects
>> somehow?

>
> Yes, the compiler (since 1.5) automatically converts the original expression
> into something similar to what you have. It's called "autoboxing" and is a
> bloody stupid idea.


What's stupid in that idea? Can you argue please?

>
> The compiler actually generates code to call the static valueOf(<primitive>)
> method rather than using an explicit constructor, which can in some cases make
> use of cached lists of pre-allocated instances. E.g. the line stuff[4] = 123
> will generate a call to the (new in 1.5) method Integer.valueOf(int), which
> (from the 1.5 code, not the spec) will in fact use the cache since 123 is in
> the cached range [-128, 127].


That's from spec (JLS 3rd ed.):

"5.1.7 Boxing Conversion

(...)

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."


piotr
 
Reply With Quote
 
 
 
 
Bart Cremers
Guest
Posts: n/a
 
      07-24-2006

Piotr Kobzda schreef:

> Chris Uppal wrote:
>
> > Mark Space wrote:
> >> Would be the correct syntax? Or do primitives get promoted to objects
> >> somehow?

> >
> > Yes, the compiler (since 1.5) automatically converts the original expression
> > into something similar to what you have. It's called "autoboxing" and is a
> > bloody stupid idea.

>
> What's stupid in that idea? Can you argue please?


I'm not totally against autoboxing, but it should be used with great
care. Specially when used in conjunction with generics. Take following
code:

List<Integer> carefull = new ArrayList<Integer>();

carefull.add(1);
carefull.add(43);
carefull.add(227);

carefull.remove(43);

System.out.println(carefull.size());

This compiles fine and the output seems pretty straightforward, but
what you get is this:

Exception in thread "main" java.lang.IndexOutOfBoundsException:
Index: 43, Size: 3

on the remove line.

Regards,

Bart

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      07-25-2006
Piotr Kobzda wrote:

> > Yes, the compiler (since 1.5) automatically converts the original
> > expression into something similar to what you have. It's called
> > "autoboxing" and is a bloody stupid idea.

>
> What's stupid in that idea? Can you argue please?


The basic problem is that it makes it look as if one thing is happening, when
in fact something completely different is happening. That is just bad language
design, for both beginners and experts. /Especially/ when it doesn't add to
the expressive power of the language.

(Of course the /real/ problem here, is the use of non-object primitive types at
all -- at least as the "normal" way to express numeric values -- but that's a
different debate...)

It (autoboxing, or indeed any other sleight-of-hand[*] in the compiler) can
cause the unwary to introduce performance problems, or semantic problems,
without realising it. E.g, some time ago, there was a person posting about
memory problems here -- it was only after some considerable amount of
speculation and debate that we realised he was holding around a million
"floats" in an ArrayList (or something like that). Since he didn't realise
that his -- otherwise perfectly reasonable -- Java code wasn't doing what he
thought it did, he didn't initially mention how he was storing all those
values. That only emerged later.

Autoboxing causes (yet more) ugly little glitches with generics too -- but I
can't remember exactly what and it's too hot to go searching


> That's from spec (JLS 3rd ed.):


Aha! Thank you for the correction.

-- chris
[*] "Sleight-of-hand" as in the shell-and-bean game, or its variants, where the
unwary punter is induced to bet, unwisely, that the bean is under the shell it
/seemed/ to be placed under....



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write codes to connect access database using html codes hiralcp Software 0 12-18-2007 08:49 PM
How to use file I/o codes with form and controls codes Allen ASP .Net 1 12-03-2007 12:04 AM
Virtual Key Codes, Scan Codes and ASCII Codes in C gj_williams2000@yahoo.co.uk C Programming 2 08-20-2005 11:04 AM
RegEx replace of html codes to ascii codes Greg -- ASP .Net 4 08-09-2005 07:27 PM
ICS unusual problem richardewing@gmail.com Wireless Networking 2 04-14-2005 09:35 AM



Advertisments