Help understanding the significance of using Java

I understand that Java is useful for portable gaming because it can be run on multiple platforms. However, if I’m building a video game with the intention of only having it be on the PC, is there any reason for me to use Java over, say, C++?

From what I understand Java is slower and not as efficient as C++, even if it is easier to use.

You underestimate the significance of the last point. Java is actually not that much slower than C++. It also takes a LOT of effort to have C++ run faster than Java, or so I’ve heard. The memory aspect is the only unfortunate thing about Java, but memory being so abundant today it’s not that big of an issue.

If you know neither, then Java is easier to learn. If you already know C++, then there’s no “big” reason to run out and learn Java.

Well, generally it is usually quite a bit faster to get something working and working well in Java with less hair loss, profanity, etc.

Java is also generally a fair bit slower than C++ - I’d say on average it runs about half the speed without extensive tuning and you particularly notice it in startup times. But then … the speed and ease of development bit really is extraordinarily significant. Part of the reason it’s slower is because it catches things at runtime that take an age to debug in C++.

Of course you could then compile the Java code natively using Excelsior JET and get the best of both worlds if you’re that serious about performance.

Cas :slight_smile:

Well as it stands I know both C++ and Java. I’m looking to start work on some projects primarily PC based since I don’t have a smart phone and I enjoy Java programming more, but am concerned about whether or not I should program in C++.

If you enjoy Java and you want to develop some not overly large projects for the PC, then I say you should go with Java :slight_smile:

One question that occurs to me is to ask what you are hoping to accomplish? Do you want to sell or market the game in hopes of making money? Is it a learning exercise? Is it something to point to as an accomplishment when applying for employment? Is it just for fun? Also, what are the performance requirements for the game?

The amount of performance one gets from Java is related to the extent to which one writes code the Java way, as opposed to the C++ way or Python way. Some of Java’s rep for slowness come from C++ programmers writing stylistically dubious Java code and using that as the basis for their comparision.

I recently discovered “Uncle Bob” (Robert C. Martin) and have adopted many of the Agile practices, and find it is cutting down the time spent debugging.

It could also be said that some of Java’s slowness comes from writing code the Java way instead of the C way… tough cookie to really nail down. But for anything other than AAA titles running cutting edge engines Java does great on the desktop and no obstacle to becoming a multi-millionaire with a game at all.

Cas :slight_smile:

I graduated with a degree in Game Design, although my school was in hindsight a bit derp and I really don’t have much experience actually making games. I want to build a portfolio of “I’m not an idiot please hire me” projects. They’re both learning experiences and portfolio fodder.

I think I’ll work with Java for now and switch the languages up after.

Ah, if you want hiring in the games industry, you are probably better off in C++.

Cas :slight_smile:

If you’re going to use hardware acceleration, the graphics card will be your biggest bottleneck and the speed differences between Java and C++ will mostly disappear.

See guys… people think this, and they will never stop
even though Java, Server JVM, has been faster than C++ for some time now.

“Java is slow” will never stop =)

Just like Cas said, most of the time Java is quite a bit slower than C/C++ (in the 20%-50% range).

Only in a very limited number of cases, Java is faster.

A lot of it is down to how Java uses memory rather than the code quality emitted by the latest Hotspot compilers. This can be worked around with them fancy mapped objects but it’s not trivial.

Also, the CPU is now more likely to be the bottleneck on modern desktops, as GPUs have gotten monster powerful. You have to be something of a wizard to overwhelm GPUs these days. Or pathologically determined.

But still: Excelsior JET. Whooooooosh!

Cas :slight_smile:

The CPU should never be the bottleneck in a decent new game. Almost all heavy computations can be done on the GPU, and the things that can’t can be threaded on the CPU. Look at Battlefield 3, they did it right.

I have this awesome argument: If you choose C/C++ over Java because you need the speed of it, you’re doing something wrong. You should have enough performance in Java that anything that you can program by alone (in contrast to a game company, I mean) should not need such insane performance. And if you actually happen to need more performance, you’d be forced to try out faster more advanced algorithms to get it running instead of just relying on the language being faster.

A lot of my point about GPUs being the bottleneck was that pushing the expensive operations to the GPU free up the CPU to do other things while the GPU is busy. Of course you need to use multithreading to truly take advantage of this. Also, when doing graphics work, you’re almost always doing “mapped” objects since vertex attribute data is usually packed into buffers or arrays, so you don’t run into the memory cache problems of java.

That doesn’t match my experience at all. I’ve written OpenGL code in C++ and Java and got the same performance (obviously, that’s because the gfx is the bottlekneck, but that is the case for a lot of games.

Additionally, I’ve done a lot of benchmarking of looping structures and calculations I was making and I found Java to be 80-105% the speed of the equivalent (optimised) C++ out of Visual C++ 2005.

The one case I found that Java ran really slowly compared to C++ was a memory thrashing test where I was doing allocation of randomly sized arrays in a loop (of course, a Java array is actually quite a lot more than a C++ array - a byte buffer would probably have been a fairer comparison)

I would agree that Java utilises memory worse than C/C++.

Just use JNI for the last bottleneck parts that have to be calculated at cpu side.

If you pick full c++ get a good book that you can read allways when you wait that your project is compiling. I mean books.

If you want to build up a portfolie as a game designer, you might be better off with some modding projects or Unity (which allows for C# scripting, so not so much different than Java).

The slow bits in Java - which are the CPU bottlenecks - are the bits where you go traversing hundreds of thousands of little objects scattered all over memory in order to pack a simple bytebuffer with, say, 30,000 vertices. In C++ the hundreds of thousands of little objects would generally easily fit into a design using value classes embedded in each other, probably with various unions, and the whole thing would basically be a contiguous block of memory you’d traverse from start to finish, achieving pretty phenomenal performance.

In Java land, you will generally have followed the path of least resistance, and allocated a bunch of little objects all referencing each other in a nice easy to understand OOP design, which will then gradually get scattered all over memory and effectively accessed in an almost completely random manner after a very short while, leading to almost pathologically crap cache usage and slowing the CPU down to about a tenth of its potential speed. No amount of threading saves you from the horrors of the knackered cache - in fact it makes it worse.

And thusly, I conclude that despite only drawing 4000 sprites, I am actually CPU limited unless I rewrite everything from the ground up to use mapped objects in ByteBuffers. YMMV but you need to know this right at the start of your design. Unfortunately my stuff evolved from just over 10 years ago when the graphics card was indeed the bottleneck and it didn’t make any difference.

Cas :slight_smile: