Skip to main content
gamedev.net
🔒 Locked

Should I use Java?

Started by gandolf1212 17 replies 2k views
Original Post
gandolf1212
gandolf1212
I posted I slightly modified version of this post on the offical Java forums, and figured I mirror it here in hopes of getting a slighly less biased responce. The thing of it is, I like Java - which is to say I like the Java language. I like object oriented programming, I like the built in swing and AWT API's, I like how it uses exceptions to avoid runtime errors, I like how the compiler reports things like precision loss, I like this board, which seems to have any answer I need on all things java. The problem I am having involves performance and distribution – or their lack of. The performance problems are not in areas where I would expect performance problems. One example would be a button I made that has a mouse listener. When clicked too much, it sets a message to “You must wait to perform another action”. The first time it is called, the entire program halts. It takes seconds to run just a few lines of code; one would think the program was generating 3d models as much as it lagged. Only thing is that it only does it the first time, after that it runs fine. After some reading, I found out this has to do with JIT, and how it handles code. I also have no idea how to distribute this. I was hoping to get it to native code, in hopes of increasing performance. This isn’t looking good though as native compilers are all 3rd party and either expensive or pretty poor looking. (at least the ones my google search found) What should I do if all I want is a good Windows program? Is there some way to make my code more JVM friendly? Is there a way to make my Java code into good native code? Should I just use a bat file / 3rd party distribution package and ignore the fact that my 2d game will have larger requirements than Doom 3? Really just need some advice.
I think there's to much blood in my caffeine system.
Conner McCloud
Conner McCloud
Quote:
Original post by gandolf1212
I posted I slightly modified version of this post on the offical Java forums, and figured I mirror it here in hopes of getting a slighly less biased responce.

This is generally frowned upon, so don't be surprised if one or the other gets the ax. Just a heads up.
Quote:
Original post by gandolf1212
I also have no idea how to distribute this. I was hoping to get it to native code, in hopes of increasing performance. This isn’t looking good though as native compilers are all 3rd party and either expensive or pretty poor looking. (at least the ones my google search found) What should I do if all I want is a good Windows program? Is there some way to make my code more JVM friendly? Is there a way to make my Java code into good native code? Should I just use a bat file / 3rd party distribution package and ignore the fact that my 2d game will have larger requirements than Doom 3?

Never tried it, but gcc has a java compiler. I don't think it is 100% there, but possibly worth a looksie if you haven't seen it yet.

CM
gandolf1212
gandolf1212
By offical I meant the one Sun owns, not that I double posted it on gamedev.

And I currently have GCC-Java 75% downloaded. Not too optimistic about it though for many reasons, aparently it doesn't have support for awt.

Wonder how hard it would be to convert a C++ compiler to a java one (not the framework, just the language)

I think there's to much blood in my caffeine system.
Alpha_ProgDes
Alpha_ProgDes
Try JCreator. It's a good program. The compiler was written in C++ so it's faster than most Java compilers.

Also here are some extra links (the site was down when i tried it).
Link 1
Beginner in Game Development?  Read here. And read here.  
stormrunner
stormrunner
Quote:
I was hoping to get it to native code, in hopes of increasing performance.

Indeed, GCJ is what you want to convert your java class files to native code. And your performance will increase. GCJ aside, you might look into JET, though I've never used it.

Quote:
Is there some way to make my code more JVM friendly?

Using a strict standards compliant compiler such as Jikes as well as a different JVM implementation (such as IBM's JDK) are a few steps in the right direction, since they've been proven to be significantly faster than Sun's. Furthermore, when you compile make sure you include the target VM (1.4 is usually a good choice) using -target=1.4.

Quote:
What should I do if all I want is a good Windows program?

What do you mean ? If you're trying to make a .exe (or the rought equivalent, there are several options :
1) Use GCJ, pretty self explanatory.
2) Use a batch file renamed to a .exe.
3) Create and use an executable JAR.

Quote:
When clicked too much, it sets a message to “You must wait to perform another action”.

Really ? What JVM were you using ? I've never had anything like that happen before, what exactly was your application doing when the mouse got clicked ? As for the application stopping, I guess that depends ... the only time I've had that happen was on a low end system that didn't have a whole lot of RAM.
stormrunner
NexusOne
NexusOne
J# = Java which compiles to an exe
Graphics make the game! 8-)
OrangyTang
OrangyTang
Quote:
Original post by gandolf1212
I also have no idea how to distribute this. I was hoping to get it to native code, in hopes of increasing performance. This isn’t looking good though as native compilers are all 3rd party and either expensive or pretty poor looking.

Firstly, AWT/Swing is not good for high-performance stuff, as you've found. The first use of any action tends to trigger all sorts of internal caching and rebuilding, which makes the initial use seem really slow. Fortunatly if you're doing a game then you can happily throw away AWT/Swing and go with either Java2D or OpenGL.

Native compilers are, largely, useless. The GCC compiler is missing some important functionality, and the Jet one costs an arm and a leg. Past real-world apps have found that other than the initial startup (first 30s) that performance was identical.

Distribution can be done either as a normal installer (which you would bundle a JVM with incase the user didn't have one) or via Webstart. Webstart is nice and gets you auto-update and single-click launch for free, but requires the user already have a VM installed. Either way you bundle your app as executable jars, and if you want to keep a windows feel you can write a simple exe wrapper which starts the correct jar for the user (theres at least a couple of tools on sourceforge that do this).

Quote:
J# = Java which compiles to an exe

Nope, J# is MS's 1.1 version of Java, with proprietry windows extensions. It's not cross platform and now completely unsupported. Avoid like the plauge.

Quote:
Try JCreator. It's a good program. The compiler was written in C++ so it's faster than most Java compilers.

JCreator is an IDE, it has no compiler. It uses the JDK's compiler, which is written in Java, and plenty fast enough. For an alternative compiler, download Eclipse, which includes IBM's compiler (and does neat stuff like on-the-fly compilation).


Edit: Actually, your performace problems sound suspiciously like you're doing all your work in the event thread, and jamming up the rest of your GUI because of that. If your app really is that small then post the code so we have something concrete to go on.
snk_kid
snk_kid
Quote:
Original post by gandolf1212
... aparently it doesn't have support for awt.


It does just not 100% complete (last time i checked).

Quote:
Original post by Anonymous Poster
Most of the time, I did not finish my projects because they were progressing too slowly due to the time spent in support task (configuration mamagement, code maintenance, memory management,...).

Java proves to let me reach my aim more quickly (it is easier to program, requires less code maintenance, comes bundled with lots of usefull and standard libraries, and you can use Eclipse).


In favour of argument in the case of C++ as a general purpose language, C++ has standard library and personally i think C++'s standard library containers & algorithms are much better than Java's collection framework even the original design was based on STL and still doesn't do a better job even with Java's "fake" generics, static imports, etc, etc to be frank it's pathetic. Then there is the C++ boost library that and the standard library just those two together lets you reach your aim more quickly and less code maintaince. C++ has more third-party libraries than virtually all languages in existence and Eclipse has plugin support for C++.

So that pretty much invalidates that comment, and that only scratches the surface i didn't even mention C++/CLI for one thing.

I'm not saying C++ is the perfect but as a general purpose language it is definitely more expressive & flexible than Java ever will especially with the advent of C++/CLI & C++0x lurking around the corner (which are not mutually exclusive either).

@gandolf1212 - At the risk of starting a flamewar (and i take full responsibility) to answer your original question i would say no, there are much more better general purpose (and specialized) languages out there. I've had 3/4 years of exprinice with Java so i think i should have enough knowledge to give an unbiased opinion and in my personal opinion Java isn't expressive, i find it restricting and verbose it just gets in the way.

I am also not saying you should use C++ either if you want strictly a Java alternative C# is much better you should see what is happening in C# 3.0/4.0. I recommend spending some time exploring other languages.

In terms of games development even though the games industry haven't moved on from C++ as there main language there is a valid reason for this and it's not used for everything besides, it's typically used as the core of a game engine and then rest is done in much a higher level (scripting) languages full of expressiveness & flexibility.

[Edited by - snk_kid on October 16, 2005 7:31:48 AM]
snk_kid
snk_kid
Quote:
Original post by Anonymous Poster
Now that's a pretty interesting statement as Java's collections framework has always been one of the most respected parts of Java (created by the genius Joshua Bloch). It's very nicely designed (great use of interfaces), documentations leave no questions and performs very well.


Yes maybe with-in Java itself but that is a different story Java's lack of expressiveness & flexibility is the main problem why Collections framework is not so hot. For instance try sorting/searching an array of primitive type using a custom predicate with the collections framework, you can not without using an array of boxed types.

This brings me to another (slightly unrelated) point, i'm sorry but faking closures with anonymous classes implementing interfaces just doesn't cut it, compared to functions as first-class entities, real closures and lambda expressions if you haven't come across these you don't know what your missing in which case i suggest playing around with some new languages (preferably NON C-derived languages).

Sure in C++ functions are not first-class entities but we have functors, C++ has support for closures, lambda expressions, and functional programming in general via libraries okay it's not as great as having it as a language level feature but it's not so bad at least it is a better attempt than what Java has to offer.

About interfaces they can be done in C++ but interfaces are not everything. In java you are forced to use them for virtually everything since it lacks expressiveness. Concepts are much more richer than interfaces and a more powerful abstraction though they are not mutually exclusive and solve different problems. C++ standard library containers & algorithms are documentated well too, check SGI's reference for instance.

Quote:
Original post by Anonymous Poster
When making statements like this, it's always a good idea to say why X is better than Y. This kind of statements is exactly what starts the flamewars.


Well i kind of did i said look at whats going on with C# 3.0/4.0 to see for yourself, i don't need to say why it's better you only need to look at the C# 2.0/3.0 specifications to see it's language features.

[Edited by - snk_kid on October 16, 2005 4:45:19 PM]
Raghar
Raghar
Quote:
Original post by gandolf1212
The problem I am having involves performance and distribution – or their lack of. The performance problems are not in areas where I would expect performance problems. One example would be a button I made that has a mouse listener. When clicked too much, it sets a message to “You must wait to perform another action”. The first time it is called, the entire program halts. It takes seconds to run just a few lines of code; one would think the program was generating 3d models as much as it lagged. Only thing is that it only does it the first time, after that it runs fine. After some reading, I found out this has to do with JIT, and how it handles code.


You need to take some lesson on GUI design. What you did was, most probably, a request for some expensive computation and you asked for it in the event loop. So no other things were processed.
It's also generaly nice idea to disalow any other computation until the first one is finished. You could change text on button to "working at xxx", and ignore all clicks on it, until calculation ends.

Look at Bruce Eckel's Thinking in Java. It might help you, and it's freely downloadable book.

Compilation into native code will not help by a single bit. It speeds up an application loading somewhat, however it will crush majority of optimalizations, and will make additing a new executable code on runtime difficult. (If you have implemented AI that is using self generated executable code, or beanshell, you know benefits of a safe JIT compilation without pointers.)

If you believe that 2D game might have requirements of Doom3, it might current GFX cards are optimalized for 3D, and they are using 4D transformations. ^_^

However when I last created program with 2D graphic, it was able to draw 1000 small images on screen at aprox 30 FPS, and 30 - 70 additional with full alpha. It was on pretty outdated graphic card and I didn't have problems with speed.
Actually for game programming you need nothing fancy. Just JFrame, BufferedImage, and possibly two JPanels, one of them for starting options and other readable, and clickable stuff, the rest of it is just taking Graphics2D object and doing your stuff. Look also at Buffer strategy and GraphicDevice. And don't forget to test for isHeadLess() some Unix systems are weird.
Of course you could throw away the 2D stuff, and mess with the 3D stuff. Look at LWJGL.
Raghar
Raghar
Quote:
Original post by snk_kid

Sure in C++ functions are not first-class entities but we have functors, C++ has support for closures, lambda expressions, and functional programming in general via libraries okay it's not as great as having it as a language level feature but it's not so bad at least it is a better attempt than what Java has to offer.



Functor is in fact a pointer / or refference on a place in memory where starts executable code that was defined by function. (If that function is definable.) This is dissalowed in Java by default because:
1. Compiled code location and its size are not known in advance.
2. Memory access check would be neccessary before each functor call.
3. Inlining works better.
4. Something like that happens on background anyway without messing the source code, and alowing a programmer to make a really spectacular errors. It alows also better branch analisys when programmer don't have ,in Java like language, easy access to functors.
5. It would be clear invitation to viruses, and hacks.
6. I really hate that message: JVM error in native code... Now there could be simillar message for functor...

Anonnymous inner classes were done to create better abstraction and provide encapsulated functionality.

And don't forget on preprocessor. (I recall an advice of C++ programmer, use preprocessor so my source file would be readable.) Java has also nice libraries for additional languages. The funny thing is to look when you and others would use closure, functional programming, and lambda expressions. I preffer working code first, then a code with a lambda expression through an external library.

Have you listened saying too many cooks spoiled the soup? Too many language features mixed together would make a mess.


Conner McCloud
Conner McCloud
Quote:
Original post by Raghar
Functor is in fact a pointer / or refference on a place in memory where starts executable code that was defined by function. (If that function is definable.)

Not neccessarily. Although a function pointer does qualify as a functor, a class that overloads the function call operator does as well. So a() is just syntactic sugar over a.function() in this case.

CM
snk_kid
snk_kid
Quote:
Original post by Raghar
Functor is in fact a pointer / or refference on a place in memory where starts executable code that was defined by function. (If that function is definable.)


I'm talking of functors specifically in the C++ sense, a user-defined type that has overloaded the function call operator and not functors in the functional sense. C++ functors can be viewed as your description in some ways but most of the time with compiler optimizations on and in trivial cases an instance of a functor is typically optimized away and/or the call is inlined out too.

Quote:
Original post by Raghar
Anonnymous inner classes were done to create better abstraction and provide encapsulated functionality.


Tell me something i don't already know, there are other methods of achieving the same thing equally if not more elegantly.

Quote:
Original post by Raghar
And don't forget on preprocessor. (I recall an advice of C++ programmer, use preprocessor so my source file would be readable.)


Forget what about it? make what aspect of code more readable? need more details than that really, you could have got some advice from a mediocre C++ coder or an expert/guru with a valid statement for all i know, it depends.

If you're implying that the C preprocessor is evil or a sin i beg to differ, it can be used in that way in the wrong hands but in the right hands in right place it can be a powerful tool for metaprogramming, Boost.Preprocessor + Boost MPL lets you do the unimaginable (still not a touch on lisp macros though [grin]), almost eliminating boilerplate code in C++. Metaprogramming is a legitimate art & practice and useful tool.

Quote:
Original post by Raghar
... The funny thing is to look when you and others would use closure, functional programming, and lambda expressions. I preffer working code first, then a code with a lambda expression through an external library.


I find that a slightly odd comment, being able to express yourself directly & concisely in a language will increase your chance of getting code working from the start thus being productive, avoiding boilerplate code (did i mention how much i hate repetitive boilerplate code?), making it fun to code and a more enjoyable experience.

[Edited by - snk_kid on October 16, 2005 8:22:55 PM]
Matt Green
Matt Green
Quote:
Original post by Raghar
Have you listened saying too many cooks spoiled the soup? Too many language features mixed together would make a mess.

Hey, don't badmouth Java 5!

Just kidding. Sort of.

Seriously, what use is a "clean" language? It naively assumes that all problems can be elegantly solved by an intentionally narrow featureset. Expressiveness is traded for simplicity, and we are supposed to be appreciative. Just look at all the features Java has acquired over its relatively short lifespan. It started out "clean" and "pure" and then people started using it for everything under the sun, at which point it started slowly accumulating features and moving away from the "purity" (which is really a euphemism for simplicity here) of the original language. Don't even try to argue that the necessity of code generation tools in some common Java programming tasks (J2EE for one) is worth the cost of a pure language, because it isn't. It indicates a lack of expressiveness and a failing in both the language and the runtime environment.

For what its worth, I think Sun is getting the message. Java 5's addition of seemingly innocent 'little' features that they would have snubbed earlier in their ivory towers shows that the language and the designers are coming of age. The language is only going to become more dirty with time, however.
stonemetal
stonemetal
Not to interrupt the c++/java commentary, but if you are looking for a decent fast gui library for java you might try
SWT by IBM. On the c++ side I have to recommend checking out the cross platform libraries(wxwidgets, fox, gtk) they are a lot nicer and cleaner than win32/mfc. For the executeable just use an executable jar file or create a short cut to act as your program launcher.

As far as the java vs C++ thing here are what I see the main differences as: Java's libraries are more coherent since they were put together by one group. I prefer stl to Java's collections. Personally I think C++ is faster but only 10% or less(when not dealing with the bloat that is swing), and C++ uses less memory. Now some might say java is better because of garbage collection but you can get the same leak free memory going in c++ with auto pointers and/or some of the neat things from the loki library. For me though the consistency of resource management in c++ out weighs gc benefits.

So use java if you like it better than c++. If you like C++ better use that instead.

Topic Locked

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

Sign in to reply to this topic.