Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java becomes more bearable

Started by benjamin bunny Dec 7, 2002 at 2:47 PM 20 replies 800+ views
Original Post
benjamin bunny
benjamin bunny
Something which has been missing from Java for a long time is about to be added in version 1.5: the ability to declare a dynamic collection, declaring the type at the same time (like in stl for example). see here This is a very good thing IMHO. It means you don't have to cast objects when accessing the list, which is, frankly, a pain in the arse. I never understood why it wasn't like this in the first place. You have to wonder about the mentality of a programmer who wants to put objects of completely unrelated types in a collection together. BTW, You'd declare your list like this: LinkedList stringList = new LinkedList(); Look familiar? A few more changes like this, and they'll have c++ [edited by - benjamin bunny on December 7, 2002 3:48:27 PM]
OrangyTang
OrangyTang
LinkedList<type> stringList = new LinkedList<type>();

You mean?

quote:
You have to wonder about the mentality of a programmer who wants to put objects of completely unrelated types in a collection together.


Yup, sounds like a good addition to me, I can honestly say I've never used a collection to store objects that were unrelated. Different objects inhereting from a standard base class yes, but never freaky combinations of objects.

[edited by - OrangyTang on December 8, 2002 8:25:31 AM]
HenryApe
HenryApe
quote:
It means you don''t have to cast objects when accessing the list, which is, frankly, a pain in the arse.

You are missing the main benefit of generics, compile-time type checking. Casts are only checked at runtime so the only way to get the compiler to do the type-checking for you when casting objects is to wrap your collections in type-safe wrapper classes.
benjamin bunny
benjamin bunny
quote:
It means you don't have to cast objects when accessing the list, which is, frankly, a pain in the arse.


quote:
Original post by HenryApe
You are missing the main benefit of generics, compile-time type checking. Casts are only checked at runtime so the only way to get the compiler to do the type-checking for you when casting objects is to wrap your collections in type-safe wrapper classes.


Compile time checking is exactly why I like generics.

The only problem I have now is I still won't be able to use [] to access elements, but that's a minor complaint. (and it's unlikely that Java will implement full-scale operator overloading any time soon.)

OrangyTang: that's the one.

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on December 8, 2002 12:13:59 PM]
kill
kill
Just use C#, it''s everything that Java promises to be but isn''t.
benjamin bunny
benjamin bunny
I use it for uni work, so I don''t get a choice.

Correct me if I''m wrong, but doesn''t C# only run on Windows machines? If it can''t do platform independence, it kindof defeats the object as far as I''m concerned.

____________________________________________________________
www.elf-stone.com
Null and Void
Null and Void
quote:
Original post by benjamin bunny
Correct me if I''m wrong, but doesn''t C# only run on Windows machines? If it can''t do platform independence, it kindof defeats the object as far as I''m concerned.

C# is somewhat portable (less so than Java currently), but much of the .NET framework is not. And, it doesn''t support generics (yet?) (which is what this thread is about, isn''t it ?).

Arild Fines
Arild Fines
quote:
Original post by Null and Void
it(C#) doesn''t support generics (yet?)

http://research.microsoft.com/projects/clrgen/



For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
CGameProgrammer
CGameProgrammer
Modifying the code in the page bunny linked to, I would prefer if the language allowed something like this:

while(listIterator.hasNext()) {
18.
19. ListElement element = listIterator.next();
20. if( element.type == String )
21. {}
22. else if( element.type == Integer )
23. {}
24. }

...because I think template-style notation ( LinkedList<String> ) is ugly.

~CGameProgrammer( );

[edited by - CGameProgrammer on December 10, 2002 1:54:54 AM]
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Graylien
Graylien
/Me hands CGameProgrammer a flame retardant suit

Seriously, you''re overlooking the fact that templates are a compile-time overhead. And what happens when your code gets a list of widget objects that it cannot handle (perhaps the widget class wasn''t written when you wrote your code)? Answer: a code maintenance nightmare!
------When thirsty for life, drink whisky. When thirsty for water, add ice.
Graylien
Graylien
One more note to follow up the previous post by CGameProgrammer.

The language does easily support the style you prefer. It''s inelegant and slow, but if you really want to do it that way then it would look like this:


  
while(listIterator.hasNext()) {
Object element = listIterator.next();
if(element instanceof String) { }
else if(element instanceof Integer) { }
}
------When thirsty for life, drink whisky. When thirsty for water, add ice.
CGameProgrammer
CGameProgrammer
Oh, I didn''t know that, Graylien. Yeah, that''s exactly what I was talking about. But why is it slow? Or inelegant?

~CGameProgrammer( );
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Arild Fines
Arild Fines
quote:
Original post by Graylien
It''s inelegant and slow,

I doubt there''s any substantial overhead involved with this approach in Java. I agree that it''s inelegant, though.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Graylien
Graylien
quote:
Original post by Arild Fines
I doubt there's any substantial overhead involved with this approach in Java.


It's slow if you have to test a whole bunch of different types. Imagine if you had 100 different types of characters in your game and you had a list containing all the people currently active in the level; this would quickly become unbearable.

As for the inherent overhead of the instanceof keyword, that's harder to say. Someone in my lab did some pretty thorough testing of control flow structures and how they are implemented in various JVM's (and across multiple platforms). I seem to remember that the instanceof technique was rejected outright because it was always much slower than using switch statements, nested if trees, table dispatch, etc.

I'd link the study if I could but our website is undergoing a very heavy facelift (well, that and it would violate certain rules about making things publicly available before they're published). Until then, don't take what I've said about instanceof to be 100% correct; it's been a while so I could be totally wrong.

[EDIT] LOL at my 133t UBB skills. < CODE > and < PRE > don't work as expected...

[edited by - Graylien on December 10, 2002 3:51:36 PM]
------When thirsty for life, drink whisky. When thirsty for water, add ice.
HenryApe
HenryApe
quote:
...because I think template-style notation ( LinkedList ) is ugly.


quote:
Imagine if you had 100 different types

I think the inelegant conditional type-testing code that CGameProgrammer suggests has little to do with the addition of generics to Java since the point of that addition is to specify one class/superclass for all types in a collection.

If you were storing different kinds of types in the list (such as Integer and String objects), then having generics in the language would mean very little since the only common type would be java.lang.Object and you would you still have to do some kind of type-testing to cast the elements correctly.

[edited by - HenryAPe on December 10, 2002 4:36:25 PM]
Oluseyi
Oluseyi
quote:
Original post by benjamin bunny
Correct me if I''m wrong, but doesn''t C# only run on Windows machines?

The Mono Project, current release 0.17 provides ADO.Net, ASP.Net and C# compiler. Mono BASIC (VB.Net) compiler set for next release.
benjamin bunny
benjamin bunny
quote:
Original post by Anonymous Poster
Easy....Buy a new copy of XP and go to sites that had applets that worked before...Get the picture.


Please go and troll somewhere else.

Oluseyi: That looks pretty interesting, although somehow I can''t envisage many unix people rushing to start using .net

____________________________________________________________
www.elf-stone.com
Promit
Promit
quote:
Original post by CGameProgrammer
Oh, I didn''t know that, Graylien. Yeah, that''s exactly what I was talking about. But why is it slow? Or inelegant?


It''s kind of ugly looking. It''s not any slower than anything else, though, because Java automatically uses RTTI because you WILL use it, and if you don''t you can go to hell cause the compiler will put it there anyway.

I don''t like Java much.

____________________________________________________________
Direct3D vs. OpenGL
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.