Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Generics are cool

Thread view: 
Joseph Dionne - 06 May 2005 04:00 GMT
I think the addition of "templates" (Generics) in 1.5 will reduce Class
casts considerably.  With Java API syntax approaching the wordiness of
COBOL, and retaining the need of casts from c, code gets "messy" to read.

Good move by me.

Joseph
hiwa - 06 May 2005 10:46 GMT
> I think the addition of "templates" (Generics) in 1.5 will reduce
> Class casts considerably.  With Java API syntax approaching the
[quoted text clipped - 4 lines]
>
> Joseph

I feel Java generics is good only on its thinnest skin, like:

ArrayList<String> as = new ArrayList<String>();

And under that, whole messiness.
It's very hard and coufusing to teach it to students and/or beginners.
I'm quite lost at that.
philip.goh@macosx.com - 06 May 2005 13:28 GMT
Well, Generics are also very cool because you can now use primitives
(floats, ints, doubles, etc) with the supplied Collection framework.

P.
Phillip Lord - 06 May 2005 13:41 GMT
>>>>> "philip" == philip goh <philip.goh@macosx.com> writes:

 philip> Well, Generics are also very cool because you can now use
 philip> primitives (floats, ints, doubles, etc) with the supplied
 philip> Collection framework.

I thought this was autoboxing, not generics.

Phil
Joseph Dionne - 06 May 2005 14:05 GMT
>>>>>>"philip" == philip goh <philip.goh@macosx.com> writes:
>
[quoted text clipped - 5 lines]
>
> Phil

In the context of Collection(s), it would is Generics and not
autoboxing.  Generics describe a new feature of Collection usage, where
the developer controls the contents of the Collection, and should not
have to cast the return Object to the data type his previous code added
into them.

joseph
Peter Ashford - 06 May 2005 14:34 GMT
>>>>>>> "philip" == philip goh <philip.goh@macosx.com> writes:
>>
[quoted text clipped - 10 lines]
> have to cast the return Object to the data type his previous code added
> into them.

Philip is correct - using primitives in collections is due to autoboxing
in 1.5, not generics.  Even without generics, you'd be able to use
primitives in 1.5 containers due to autoboxing.
Joseph Dionne - 06 May 2005 14:46 GMT
>>>>>>>> "philip" == philip goh <philip.goh@macosx.com> writes:
>>>
[quoted text clipped - 14 lines]
> in 1.5, not generics.  Even without generics, you'd be able to use
> primitives in 1.5 containers due to autoboxing.

Please correct me if my reading of autoboxing is wrong, but it appears
to me autoboxing is more to do about up casting a primitive to a
function return automatically than casting a generic Object to the type
of the object receiving the assignment.

As I read Generics, they only apply to containers capable of holding
Objects of any type.

joseph
Joseph Dionne - 06 May 2005 14:49 GMT
>>>>>>>>> "philip" == philip goh <philip.goh@macosx.com> writes:
>>>>
[quoted text clipped - 24 lines]
>
> joseph

On second thought, perhaps Generics simply provides javac the
information it needs to implement autoboxing.

joseph
Phillip Lord - 09 May 2005 12:39 GMT
>>>>> "Joseph" == Joseph Dionne <jdionne@hotmail.com> writes:

 >> Please correct me if my reading of autoboxing is wrong, but it
 >> appears to me autoboxing is more to do about up casting a
 >> primitive to a function return automatically than casting a
 >> generic Object to the type of the object receiving the
 >> assignment.  As I read Generics, they only apply to containers
 >> capable of holding Objects of any type.  joseph

 Joseph> On second thought, perhaps Generics simply provides javac
 Joseph> the information it needs to implement autoboxing.

No. The information is there anyway....

Collection c = new Collection (of somekind);
boolean b = false;

c.add( b );

boolean r = c.get();

We know that c is of type Collection and b is a primitive. Therefore
we autobox, compiling to effectively into something like..

Collection c = new Collection (of somekind);
boolean b = false;

c.add( new Boolean( b ) );

boolean r = c.get().booleanValue()

In the first case we know to use new Boolean( b ) because b is a
boolean, in the second we use booleanValue because r is.

Of course, generics would make this type safe.

Phil
Antti S. Brax - 09 May 2005 18:58 GMT
p.lord@cs.man.ac.uk wrote in comp.lang.java.programmer:
>>>>>> "Joseph" == Joseph Dionne <jdionne@hotmail.com> writes:
>  Joseph> On second thought, perhaps Generics simply provides javac
>  Joseph> the information it needs to implement autoboxing.
>
> No. The information is there anyway....

    No. There are only assumptions.

> Collection c = new Collection (of somekind);
> boolean b = false;
[quoted text clipped - 3 lines]
> In the first case we know to use new Boolean( b ) because b is a
> boolean, in the second we use booleanValue because r is.

    In the second case you do not know what comes out of the
    collection. You only _assume_ it is a boolean, since the
    programmer wants one.

Signature

Antti S. Brax                  Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/         http://www.cs.helsinki.fi/u/abrax/hlb/

              [1385 messages expunged from folder "Spam"]

Phillip Lord - 10 May 2005 11:14 GMT
>>>>> "Antti" == Antti S Brax <asb@iki.fi.NO.SPAM> writes:

 Antti> p.lord@cs.man.ac.uk wrote in comp.lang.java.programmer:
 >>>>>>> "Joseph" == Joseph Dionne <jdionne@hotmail.com> writes:
 Joseph> On second thought, perhaps Generics simply provides javac
 Joseph> the information it needs to implement autoboxing.
 >>
 >> No. The information is there anyway....

 Antti>     No. There are only assumptions.

The information that you need for autoboxing is there.

Actually, not quite, as I left the cast out which should have been
there.

 >> In the first case we know to use new Boolean( b ) because b is a
 >> boolean, in the second we use booleanValue because r is.

 Antti>     In the second case you do not know what comes out of
 Antti>     the collection. You only _assume_ it is a boolean,
 Antti>     since the programmer wants one.

Yes. But we are talking about autoboxing here. There is no requirement
for autoboxing to be type safe.

Collection c;
boolean b;
int i;

c.add( b );
i = (int)c.get();

will crash with a ClassCast. But there again so will this code.

Collection c;
Boolean b;
Integer i;

c.add( b );
i = (Integer)c.get();

In the first case you have all enough information to work out that
autoboxing should be compiled in.

c.add( b );

You are calling a Collection method which takes Object with a
primitive int. Therefore it should be boxed with an Integer.

i = (int)c.get();

You are returning an Object where only a int can be
expected, therefore it should be unboxed with an IntegerValue, or
Integer.valueOf, or however it is compiled.

What generics gives you in addition is the guarantee that the second
call will not crash with ClassCast. But you could have had autoboxing
without generics, and some people will use it that way.

Cheers

Phil
Antti S. Brax - 11 May 2005 06:40 GMT
p.lord@cs.man.ac.uk wrote in comp.lang.java.programmer:
>>>>>> "Antti" == Antti S Brax <asb@iki.fi.NO.SPAM> writes:
>
[quoted text clipped - 21 lines]
> Yes. But we are talking about autoboxing here. There is no requirement
> for autoboxing to be type safe.

    Correct. But then, there is no requirement for a programming
    language to be type safe either.

    And I was not challenging type safety or whether autoboxing
    should be type safe. Only pointing out that in the example
    code of the original article there were no facts involved,
    only assumptions.

Signature

Antti S. Brax                  Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/         http://www.cs.helsinki.fi/u/abrax/hlb/

              [1385 messages expunged from folder "Spam"]

Alex Buell - 06 May 2005 15:28 GMT
>> > > > > > "philip" == philip goh <philip.goh@macosx.com> writes:
>>
[quoted text clipped - 9 lines]
> controls the contents of the Collection, and should not have to cast the
> return Object to the data type his previous code added into them.

Autoboxing, not generics.

Signature

http://www.munted.org.uk

Drive carefully and find your true love.

Joseph Dionne - 06 May 2005 15:43 GMT
>>> > > > > > "philip" == philip goh <philip.goh@macosx.com> writes:
>>>
[quoted text clipped - 12 lines]
>
> Autoboxing, not generics.

I'll stand corrected. Either way, the functionality is still very cool.
Tim Tyler - 09 May 2005 16:48 GMT
Joseph Dionne <jdionne@hotmail.com> wrote or quoted:
> >> Phillip Lord wrote [or quoted]:

> >>> philip>  Well, Generics are also very cool because you can now use
> >>> philip>  primitives (floats, ints, doubles, etc) with the supplied
> >>> philip>  Collection framework.
> >>>
> >>>  I thought this was autoboxing, not generics.

[...]

> > Autoboxing, not generics.
>
> I'll stand corrected. Either way, the functionality is still very cool.

IMO, autoboxing sucks.

AFAICS, it's only good if your point of comparison is the previous version
of Java.

Take a slightly less myopic perspective - and surely the best thing is
never to have had primitive types in the first place.
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Lee Fesperman - 09 May 2005 22:09 GMT
> IMO, autoboxing sucks.
>
[quoted text clipped - 3 lines]
> Take a slightly less myopic perspective - and surely the best thing is
> never to have had primitive types in the first place.

I'd view that as a myopic perspective. Java wouldn't be appropriate for certain
high-performance applications if that were true. In that case, my company would not have
built an RDBMS in Java, which would be too bad because we support Java objects in the
database and run their methods natively ... something Oracle, SQL Server, DB2 can't do.
We see that as a big win.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Lasse Reichstein Nielsen - 09 May 2005 22:39 GMT
>> Take a slightly less myopic perspective - and surely the best thing is
>> never to have had primitive types in the first place.
>
> I'd view that as a myopic perspective. Java wouldn't be appropriate
> for certain high-performance applications if that were true.

Sure it would. You don't have to make an inefficient implementation
just because primitive types looks like objects at the language level.

Other languages have had primitive types as objects with efficient
implementations (starting with Smalltalk, and probably including C#).

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Lee Fesperman - 10 May 2005 01:26 GMT
> >> Take a slightly less myopic perspective - and surely the best thing is
> >> never to have had primitive types in the first place.
[quoted text clipped - 7 lines]
> Other languages have had primitive types as objects with efficient
> implementations (starting with Smalltalk, and probably including C#).

I know. It just seems pretty hard with dynamic loading, serialization, ... And, I'd be
looking for an absolute guarantee. In Java, I can predict the performance of code almost
as well as C. Even though in C I knew the machine code generated.

I don't think that high performance type applications are common in Smalltalk. Are there
several RDBMSs written in Smalltalk?

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Chris Uppal - 10 May 2005 11:16 GMT
> Sure it would. You don't have to make an inefficient implementation
> just because primitive types looks like objects at the language level.

I imagine that there are some circumstances where the even the very moderate
slow-down caused by using these implementation techniques would be
unacceptable.   Perhaps not many, but certainly some.  So one option would have
been to make "normal" numbers be represented as full objects (but not, of
course, allocated from the heap) using the standard "unboxed representation"
(nothing to do with auto-boxing), but also allow programmers to use primitive
types for those rare occasions where the normal representation wasn't fast
enough.

Interestingly, Java's static typing would allow an unboxed representation to be
/more/ efficient than it is in Smalltalk[*], since quite a few runtime tests
(is this "pointer" a real object or is it an unboxed integer) could be
optimised away by the JITer.

([*] Or, more accurately, to allow a given level of performance to be achieved
with less effort put into the VM implementation.)

> Other languages have had primitive types as objects with efficient
> implementations (starting with Smalltalk, and probably including C#).

I think the first language to use unboxed integers was LISP, though the same
trick has been used in Smalltalk for many years.

   -- chris
Chris Uppal - 10 May 2005 11:16 GMT
> I'd view that as a myopic perspective. Java wouldn't be appropriate for
> certain high-performance applications if that were true. In that case, my
> company would not have built an RDBMS in Java, which would be too bad

I /very much/ doubt whether Java with Smalltalk/LISP style integers would be
any less suitable for implementing an RDMS.

Of course, I could be wrong -- perhaps your DB's efficiency is dominated by
numeric calculations (rather than by disk-seeks and other IO), but if so then
I'd say there something Very Wrong with your DB...

   -- chris
Lee Fesperman - 10 May 2005 21:44 GMT
> > I'd view that as a myopic perspective. Java wouldn't be appropriate for
> > certain high-performance applications if that were true. In that case, my
> > company would not have built an RDBMS in Java, which would be too bad
>
> I /very much/ doubt whether Java with Smalltalk/LISP style integers would be
> any less suitable for implementing an RDMS.

To me, this looks like pure conjecture on your part. I know of at least 5 non-trivial
RDBMSs implemented in pure Java. I don't know of any implemented in Smalltalk or Lisp.

> Of course, I could be wrong -- perhaps your DB's efficiency is dominated by
> numeric calculations (rather than by disk-seeks and other IO), but if so then
> I'd say there something Very Wrong with your DB...

Disk I/O is not necessarily an issue since the DB also has an in-memory mode.

Is your "Very Wrong" comment based on doing a non-trivial implementation of an RDBMS?
Perhaps you could explain.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Chris Uppal - 11 May 2005 08:52 GMT
Lee Fesperman wrote [quoted in full for reasons that will become evident]:

> > > I'd view that as a myopic perspective. Java wouldn't be appropriate
> > > for
[quoted text clipped - 21 lines]
> Is your "Very Wrong" comment based on doing a non-trivial implementation
> of an RDBMS? Perhaps you could explain.

I can't be bothered with this.

   -- chris
Lee Fesperman - 11 May 2005 09:43 GMT
> Lee Fesperman wrote [quoted in full for reasons that will become evident]:
> > >
[quoted text clipped - 6 lines]
>
> I can't be bothered with this.

Right, I figured you couldn't back up your rash comment.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Tim Tyler - 10 May 2005 17:12 GMT
Lee Fesperman <firstsql@ix.netcom.com> wrote or quoted:

> > IMO, autoboxing sucks.
> >
[quoted text clipped - 6 lines]
> I'd view that as a myopic perspective. Java wouldn't be appropriate for
> certain high-performance applications if that were true. [...]

That appears to be the idea that having no primitive types necessarily
involves a performance hit.

Performance may have been one of the reasons why primitive types were
included in Java in the first place - but I think these days the idea
that having everything be an object involves a performance hit is
regarded as a fallacy.

Essentially, a JIT should be capable of massaging a program into
a functionally equivanent form, and whether things are objects
or not internally becomes largely irrelevant at that stage.

That's not to say Java's designers made a mistake here - Java got
enough criticism about performance at the time - and given the
state of JIT technology at the time, having no primitive types
would probably have made things worse initially.

However the decision of Java's designers had the effect of
prioritising short term performance considerations over Java's
longevity.

Their design decision never got fixed - and now Java appears
to be stuck forever with primitive types - and it's a factor
that looks likely to be significantly implicated in Java's demise.
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Lee Fesperman - 10 May 2005 21:53 GMT
> Lee Fesperman <firstsql@ix.netcom.com> wrote or quoted:
>
[quoted text clipped - 11 lines]
> That appears to be the idea that having no primitive types necessarily
> involves a performance hit.

True.

> Performance may have been one of the reasons why primitive types were
> included in Java in the first place - but I think these days the idea
> that having everything be an object involves a performance hit is
> regarded as a fallacy.

Do you have evidence of significant high performance applications written in such a
language?

> Essentially, a JIT should be capable of massaging a program into
> a functionally equivanent form, and whether things are objects
> or not internally becomes largely irrelevant at that stage.

Obviously, I have doubts that it is true in *all* cases. One area I mentioned was heavy
use of dynamic loading.

> Their design decision never got fixed - and now Java appears
> to be stuck forever with primitive types - and it's a factor
> that looks likely to be significantly implicated in Java's demise.

Ultimately, that may be true, but I don't think that is a widely held belief in relation
to a truly general purpose language.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

Tim Tyler - 12 May 2005 08:25 GMT
Lee Fesperman <firstsql@ix.netcom.com> wrote or quoted:
> > Lee Fesperman <firstsql@ix.netcom.com> wrote or quoted:

> > > > IMO, autoboxing sucks.
> > > >
[quoted text clipped - 19 lines]
> Do you have evidence of significant high performance applications
> written in such a language?

Since such languages include Python, C#, Ruby, Scheme, Dylan, ML,
and Smalltalk I'd say yes.
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Chris Uppal - 12 May 2005 09:26 GMT
> > Do you have evidence of significant high performance applications
> > written in such a language?
>
> Since such languages include Python, C#, Ruby, Scheme, Dylan, ML,
> and Smalltalk I'd say yes.

I'm not sure that I'd lump Python and Ruby in with Scheme, Dylan, ML, and
Smalltalk as being suitable for high-performance applications.  Python and Ruby
are (in their standard, interpreted, implementations) considerably slower than
the compiled languages.

Yes, there are applications where throughput is important, and which have been
successfully been implemented in Python and Ruby (Zope comes to mind, and the
new Ruby web framework, the name of which escapes me, seems to be generating
quite a bit of attention too), but such cases depend on the application being
limited by factors such as IO rather than by raw processing power.

(Such applications become more common as machines get faster of course.  Indeed
there's an argument to be made that compilation, as an implementation
technology, is an unnecessary complication for many purposes.  For instance the
Smalltalk implementation that I prefer to use is unique, among the commercial
implementation, in that it is implemented as a bytecode interpreter (albeit, an
impressively fast interpreter) rather than by JITing -- that looses it a factor
of maybe 2 or 3 compared with the compiled Smalltalks, and maybe 5 or 6
compared with compiled /and optimised/ C, but that makes no noticable
difference for most of my applications.)

BTW, I wouldn't lump C# in with Scheme, etc, here.  At least, as far as my
limited understanding of C# implementation goes, I think that it uses
/auto-boxing/ rather than /unboxed/ ints, so the implementation technology is
more like Java's (with consequent compromises in the semantics) than like
Scheme/Smalltalk/ML/et al.

   -- chris
Dale King - 26 May 2005 04:46 GMT
> That appears to be the idea that having no primitive types necessarily
> involves a performance hit.
[quoted text clipped - 7 lines]
> a functionally equivanent form, and whether things are objects
> or not internally becomes largely irrelevant at that stage.

I think that is an oversimplification of the issues. If Java primitives
were objects just like the current objects that would be a nightmare
performance and understanding wise. The real issue here is value
semantics vs. reference semantics.

In value semantics means that a variable contains the actual value and
in reference semantics the variable only holds a reference to the actual
value.

With value semantics there is no concept of identity while in reference
semantics different instances of different identity. In other words the
value 3 is the value 3 no matter where it is stored. But for example two
instances of Integer may contain the same value but still maintain
different identities.

Inheritance and polymorhpism only work with reference semantics.

So we definitely want to be able to have objects with reference
semantics because we want polymorphism. For numbers and the like you
really want value semantics.

So Java made all objects have reference semantics and they created
primitives for value semantics. To have the "primitive" types be objects
is not as simple as just saying that they are all objects. They would
need to be objects with value semantics, which Java does not have. So
you still end up with something that is not the same as normal Java objects.

The way C# handled it is that there is two types of objects. Those with
reference semantics and those with value semantics. Classes are
reference, but they also have structs which are value semantics. The
primitive types are value objects. Autoboxing and Unboxing is a process
to convert between value and reference semantics.
Signature

 Dale King

Chris Uppal - 26 May 2005 10:14 GMT
> If Java primitives
> were objects just like the current objects that would be a nightmare
> performance and understanding wise. The real issue here is value
> semantics vs. reference semantics.

I don't want to sound hostile, but this is completely wrong.

Take a look at a typical Smalltalk implementation.  (BTW, this post isn't
intended as a plug for Smalltalk -- though readers are welcome to take it that
way if they wish -- but as an experience report from someone who has used a
language with the features under discussion.)

The typical /implementation/ strategy is the old "unboxed representation" trick
(encoding smallish int values in what otherwise would be a pointer to an
object's physical representation -- /NOT/, please note, auto-boxing, which
would indeed be a performance problem).  This does cause a small slowdown
relative to the use of "raw" primitive types, which -- in the case of
Smalltalk -- is exacerbated by the dynamic nature of its type system making it
harder (but not impossible) for the JIT to optimise arithmetic operations, but
the slowdown is fairly small (I don't have figures for modern implementations,
but I'd guess 10-20% for raw arithmetic) and would easily be accommodated for
/most/ purposes.

For integer values that need more than about 30 bits the system must switch to
a boxed representation.  That indeed causes a noticeable slowdown (mainly
because of all the object allocations), but on the other hand there is no
problem with integer overflow (I still can't understand why Java -- supposedly
so safe -- allows /silent/ integer wraparound by default).

That's the implementation side.  Now for the semantics.  To be honest I don't
really understand what you think the problem would be.  In Smalltalk integers
are objects, and that causes no problems that I can see.  It's not obvious to
me what value "value semantics" would be supposed to add.  Integers are either
BigIntegers (boxed) or SmallIntegers (unboxed), both are subclasses of Integer.
Integer and Float are subclasses of Number.  Normal class-based polymorphism is
used extensively and in complete accordance with how all the other objects
work.  No need for "widening conversions", overloaded arithmetic operators (0.1
+ 22) and all the other junk that Java has to add to its semantics in order to
hack around the problems caused by primitive types.  It all works very well.

BTW, I'm not saying that it would be appropriate for a language like Java not
to have primitive types at all.  The nature of the language (specifically its
type system) means that they could be included as an expert-friendly feature
available for use by people who knew what they were doing when (say) adding two
integers together, or comparing two floats.  But what I /do/ think is that
machine primitives should never have been the /default/ representation of
numbers.

   -- chris
Dale King - 01 Jun 2005 04:08 GMT
>>If Java primitives
>>were objects just like the current objects that would be a nightmare
>>performance and understanding wise. The real issue here is value
>>semantics vs. reference semantics.
>
> I don't want to sound hostile, but this is completely wrong.

If it is wrong, then you did not show that it was wrong. I said that if
Java primitives were just like normal java objects that would be bad.
You go on to describe a system where they could be treated in a way that
is *NOT* like normal Java objects.

> Take a look at a typical Smalltalk implementation.  (BTW, this post isn't
> intended as a plug for Smalltalk -- though readers are welcome to take it that
[quoted text clipped - 11 lines]
> but I'd guess 10-20% for raw arithmetic) and would easily be accommodated for
> /most/ purposes.

IMO 10-20% slowdown pretty much falls in the "nightmare" category.

> For integer values that need more than about 30 bits the system must switch to
> a boxed representation.  That indeed causes a noticeable slowdown (mainly
> because of all the object allocations), but on the other hand there is no
> problem with integer overflow (I still can't understand why Java -- supposedly
> so safe -- allows /silent/ integer wraparound by default).

Once again you agree that it would have performance problems.

> That's the implementation side.  Now for the semantics.  To be honest I don't
> really understand what you think the problem would be.  In Smalltalk integers
[quoted text clipped - 6 lines]
> + 22) and all the other junk that Java has to add to its semantics in order to
> hack around the problems caused by primitive types.  It all works very well.

Another way of saying value vs. reference semantics is copy vs.
reference semantics or aliasing.

Consider these:

Object o1 = new Object();
Object o2 = o1;

int i1 = 5;
int i2 = i1;

There are vastly different semantics for these two. o1 and o2 both refer
to the same object, but i1 and i2 are copies of the same value. O1 is an
alias for o2.

There are all sorts of combinations of semantics in various languages,
but all I am saying is that it would be a pretty major shift in
semantics for Java to have numeric values be "objects". The only
semantics that Java has for objects (reference semantics) would not
necessarily be the best for numerical values.

> BTW, I'm not saying that it would be appropriate for a language like Java not
> to have primitive types at all.  The nature of the language (specifically its
[quoted text clipped - 3 lines]
> machine primitives should never have been the /default/ representation of
> numbers.

And I was saying that the nature of the language means that it would not
be appropriate. It would take a major change to the language, not only
in technical ways but in basic understanding of the semantics of the
language. I'm not saying that Java could not have been created with more
object-like primitives, but it would be different than what the objects
are right now.

Signature

 Dale King

philip.goh@macosx.com - 06 May 2005 17:57 GMT
My bad.

I've only just installed Java 1.5 on my Mac and haven't really used
collections with primitives yet. Turns out generics in Java is
sufficiently different from generics in C++. List<int> doesn't work in
Java....

ah well...

P.
Murat Tasan - 07 May 2005 00:16 GMT
On Fri, 06 May 2005 09:57:11 -0700, philip.goh wrote:

> My bad.
>
> I've only just installed Java 1.5 on my Mac and haven't really used
> collections with primitives yet.

did i miss something?  i'm pretty sure java 1.5 STILL (ahem, apple...) has
not been released for macos 10.

> Turns out generics in Java is
> sufficiently different from generics in C++. List<int> doesn't work in
> Java....

correct.  generics in Java works only with reference objects.  although,
you can certainly create a list such as:

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

and then add ints via autoboxing:

myList.add(5);

although, be sure to read about autoboxing/unboxing as for
high-performance applications this is not a good way of introducing
collections and primitives.  if your app is of that nature
(high-performance), it is still better to use your own simple (and often
array-based) data structures.  the Collection Framework is good to work
with at times, but can cause your app to be unbearably slow at times
(especially when considering the extra call stack items that are run when
doing auto boxing/unboxing).
philip.goh@macosx.com - 07 May 2005 08:59 GMT
It has been released for OS X and can be downloaded from
http://www.apple.com/support/downloads/java2se50release1.html
Joseph Dionne - 07 May 2005 14:58 GMT
> On Fri, 06 May 2005 09:57:11 -0700, philip.goh wrote:
>
[quoted text clipped - 27 lines]
> (especially when considering the extra call stack items that are run when
> doing auto boxing/unboxing).

You make an interesting observation, that autoboxing affects
performance.  Do you know by how much?  Will using a Hotspot VM regain
any performance loss?
Murat Tasan - 09 May 2005 17:38 GMT
> You make an interesting observation, that autoboxing affects
> performance.  Do you know by how much?  Will using a Hotspot VM regain
> any performance loss?

i'm not sure how much performance loss is done, but even the people at Sun
themselves make a point of saying not to use autoboxing/unboxing for
high-performance apps.

check out the last paragraph on this page:

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

murat
Joseph Dionne - 09 May 2005 17:58 GMT
>>You make an interesting observation, that autoboxing affects
>>performance.  Do you know by how much?  Will using a Hotspot VM regain
[quoted text clipped - 9 lines]
>
> murat

I read that this weekend.  Oh, well, yet another Java feature I will not
be using.  Too bad.
Phillip Lord - 09 May 2005 18:43 GMT
>>>>> "Joseph" == Joseph Dionne <jdionne@hotmail.com> writes:

 Joseph> Murat Tasan wrote:
 >>

 >>> You make an interesting observation, that autoboxing affects
 >>> performance.  Do you know by how much?  Will using a Hotspot VM
 >>> regain any performance loss?

 >> themselves make a point of saying not to use autoboxing/unboxing
 >> for high-performance apps.  check out the last paragraph on this
 >> page:

 >> http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

 Joseph> I read that this weekend.  Oh, well, yet another Java
 Joseph> feature I will not be using.  Too bad.

Don't be so hasty. If you write an app which this feature a lot, then
it might cause performance problems. But it is nearly always wrong to
assume that know what will cause performance problems, with the broad
acceptance of algorithmically expensive code (anything in exponetial
or worse time is always going to be a problem soon or later; probably
sooner).

Premature optimisation is a cause of many bugs. If autoboxing makes
code easier to read (which I think it will), you should throw it away
only if you know it too be causing problems, that is, if you have
profiled your code.

Besides which autoboxing should cause no performance problems that

c.add( new Boolean ( false ) );

does not. It's the same thing. If you are writing performance code
then you should be wary of this anyway. The only thing that autoboxing
does is hide the object creation (in much the same way as the string +
operator).

Phil
Bent C Dalager - 09 May 2005 22:55 GMT
>Besides which autoboxing should cause no performance problems that
>
>c.add( new Boolean ( false ) );
>
>does not. It's the same thing.

I believe it's more akin to
c.add(Boolean.FALSE);
which should be rather more effecient.

For Integers, I think it has a pool of all ints from 0 to 1000 or
somesuch to optimize commonly used numbers.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Tim Tyler - 10 May 2005 17:19 GMT
Bent C Dalager <bcd@pvv.ntnu.no> wrote or quoted:

> For Integers, I think it has a pool of all ints from 0 to 1000 or
> somesuch to optimize commonly used numbers.

Java 5 caches integers smaller than 128 and bigger than -129.

Earlier versons do not - leading to non-backwards-compatible behaviour:

``Immutable Objects
 
 Consider the following code fragment:
 
    Integer i1 = 100;
    Integer i2 = 100;
    Integer i3 = 1000;
    Integer i4 = 1000;
    System.out.println(i1==i2);
    System.out.println(i3==i4);
 
 Can you guess what will be printed on the screen? If your
 answer is false--well, you're wrong.
 
 In this case, J2SE 5.0 works differently. Certain ranges of
 values are stored as immutable objects by the Java Virtual
 Machine. So, in this case, the output is:
 
 true
 false
 
 Normally, when the primitive types are boxed into the
 wrapper types, the JVM allocates memory and creates a new
 object. But for some special cases, the JVM reuses the same
 object.
 
 The following is the list of primitives stored as immutable
 objects:
 
     * boolean values true and false
     * All byte values
     * short values between -128 and 127
     * int values between -128 and 127
     * char in the range \u0000 to \u007F''

- http://today.java.net/pub/a/today/2005/03/24/autoboxing.html
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Bent C Dalager - 10 May 2005 18:20 GMT
>Java 5 caches integers smaller than 128 and bigger than -129.
>
>Earlier versons do not - leading to non-backwards-compatible behaviour:

Which earlier versions? 1.5 betas and release candidates? 1.4.x
doesn't have autoboxing, so Integer caching shouldn't be an issue
there.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Tim Tyler - 12 May 2005 08:05 GMT
Bent C Dalager <bcd@pvv.ntnu.no> wrote or quoted:

> >Java 5 caches integers smaller than 128 and bigger than -129.
> >
> >Earlier versons do not - leading to non-backwards-compatible behaviour:
>
> Which earlier versions?

All earlier versions.

> 1.5 betas and release candidates? 1.4.x doesn't have autoboxing, so
> Integer caching shouldn't be an issue there.

Exactly: in the supplied [but snipped :-(] example, Java 5 behaves
differently from all previous versions of Java - in a manner that is
not backwards-compatible with them.
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Chris Uppal - 12 May 2005 09:03 GMT
> Exactly: in the supplied [but snipped :-(] example, Java 5 behaves
> differently from all previous versions of Java - in a manner that is
> not backwards-compatible with them.

Can you exhibit code that compiles under /both/ 1.4 and 1.5 and has different
behaviour ?  Or, even more interesting if you can find an example, code that
compiles under 1.4 but has different behaviour when run under 1.4 and 1.5 ?

As far as I can see Sun have been pretty careful only to use the cache for
Integers that are "created" via paths that did not exist in 1.4.  E.g.
Integer.valueOf(int) uses the cache, and so does auto-boxing, but the
Integer(int) constructor does not, and neither does Integer.valueOf(String).

Perhaps /too/ careful -- though I'm certainly impressed by the care they've
taken, the end result is that the various versions of Integer.valueOf() are no
longer consistent between themselves, nor do they follow the pattern of cache
usage previous set by, say, Boolean.

   -- chris
Bent C Dalager - 12 May 2005 12:41 GMT
>Exactly: in the supplied [but snipped :-(] example, Java 5 behaves
>differently from all previous versions of Java - in a manner that is
>not backwards-compatible with them.

I see. The difference you referred to was more the presence of
autoboxing than the caching of Integers? If this is the case, I don't
see why the difference is particularly relevant. Noone is going to be
burned by it because code that requires autoboxing wouldn't compile at
all in 1.4 anyway. Well, except if you're writing code using the 5.0
compiler and want the code to be backwards compatible I suppose, but
then your build process should include building for target 1.4 etc.,
which would have you catch it anyway.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

Chris Uppal - 10 May 2005 11:18 GMT
>   >> themselves make a point of saying not to use autoboxing/unboxing
>   >> for high-performance apps.  check out the last paragraph on this
[quoted text clipped - 8 lines]
> it might cause performance problems. But it is nearly always wrong to
> assume that know what will cause performance problems

The problem is not so much that autoboxing /may/ or /does/ cause performance
problems (and IMO the problems are more likely to be in memory consumption and
GC load than execution time), as that the code is actually doing something much
more complicated than it appears.  That throws programmers' intuitions off, and
can (and does) lead people into making expensive mistakes, which they may find
difficult to diagnose.

There's nothing wrong (for most purposes) with stuffing a few int-s into a
Collection using auto-boxing.  The problem is that that doesn't scale, but it
/looks/ as if it is harmless.

(For a recent example on this ng: the gent who was keeping around 10 million
32-bit int-s in ArrayLists -- and couldn't understand why he was running out of
memory.  That thread was quite illuminating, since there were all sorts of
suggestions as to what his problem might be, and it wasn't until the
conversation had gone on for quite some time that the real problem emerged.  He
had, of course, no idea that auto-boxing was behind his problems, and so he
didn't mention that he was keeping lots of int-s in ArrayLists.)

   -- chris
Phillip Lord - 10 May 2005 11:58 GMT
>>>>> "Chris" == Chris Uppal <chris.uppal@metagnostic.REMOVE-THIS.org> writes:

 Chris> There's nothing wrong (for most purposes) with stuffing a few
 Chris> int-s into a Collection using auto-boxing.  The problem is
 Chris> that that doesn't scale, but it /looks/ as if it is harmless.

 Chris> (For a recent example on this ng: the gent who was keeping
 Chris> around 10 million 32-bit int-s in ArrayLists -- and couldn't
 Chris> understand why he was running out of memory.

Of course, this is true. Although this does turn in to a having cake
and eating it argument.

The whole point of autoboxing is that it is automatic, and doesn't
require programmer annotation.

There are a lot of things like this in Java. The automation of GC for
example, can lead you to leave large numbers of objects dangling on a
static variable (been there, done that...)

Cheers

Phil
Murat Tasan - 11 May 2005 16:46 GMT
> i'm not sure how much performance loss is done, but even the people at Sun
> themselves make a point of saying not to use autoboxing/unboxing for
> high-performance apps.

i just wrote a little test program that loops through and performs a
single addition to an integer 10 million times.

using a primitive integer, the loop takes on average just about 100
milliseconds.

rewriting the program to represent the integer as an Integer object, then
leaving the rest of the loop as it is for the autoboxing/unboxing, the
loop now takes on average about 2000 milliseconds.  thats 20x slower!

then i rewrote the program again to store the cumulative result back in
the original primitive type, but now i'm constantly to that primitive type
an Integer object.  this experiment should test for unboxing, rather than
both.  the average time is about 150 milliseconds.

this indicated that the boxing is what takes a long time.  so... i wrote
one final program that only does the boxing by storing the results of an
operation with two primitive types into an Integer object.  the results
were once again an average time of well over 1000 milliseconds.

so, the lesson from this is that unboxing is relatively cheap and easy and
can be used in your app even if performance is a consideration.  if many
autoboxes are going to occur however, consider using primitive types
instead.

hope this helps a bit.

murat
Bent C Dalager - 12 May 2005 12:45 GMT
>> i'm not sure how much performance loss is done, but even the people at Sun
>> themselves make a point of saying not to use autoboxing/unboxing for
[quoted text clipped - 9 lines]
>leaving the rest of the loop as it is for the autoboxing/unboxing, the
>loop now takes on average about 2000 milliseconds.  thats 20x slower!

It would be interesting to know what percentage of your autoboxed
Integer objects got cleaned up by the GC before program exit. If a
majority were, the figure may be accurate, but if you had enough
memory available for most of them to remain unreclaimed, boxing might
be somewhat more expensive than this in a long-running app.

Cheers
    Bent D
Signature

Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
                                   powered by emacs

ridvansg@gmail.com - 10 May 2005 19:32 GMT
I come from the world of C++ which is full with templates, belive me
templates are everything but not cool, they are aproppriate for a
language like perl  but not java.

Just my opinion


Free Magazines

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

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.