views:

1537

answers:

15

I understand that C++ is generally considered better than Java for games (at least larger-scale games). Why is this?

What is keeping Java from being competitive in this field? Which reasons against using Java for game programming have the most basis, and which ones are myths?

EDIT: Am a bit unfamiliar with C/C++, and did not think to differentiate between the two at line 1 >.<

+8  A: 

High performance and the inertia of C and C++ traditionally being used for games.

Choosing based on performance isn't that big a priority unless you are making a 3D extravaganza.

frou
Inertia is probably the biggest thing here. Java can be very fast, and most games are GPU limited.
Byron Whitlock
Well, Java does not easily give you the low-level access that some games require. Also, don't forget about console platforms.
Ed Swangren
Good point - obviously commercial stuff for consoles have a ton of restrictions on how you build your game!
frou
Although that intertia isn't without warrant. I'm sure EA has a pretty robust set of C++ and C libraries by now. I'd consider java for the game server side since it's easy to scale.
reccles
FWIW, you can't even ship games written in java on PS3, XBOX-360, Wii, PSP, GBC, etc. It's not inertia, it's simply *NOT* *ALLOWED*.
Adisak
What AAA game is not a "3d extravaganza"?
Crashworks
+5  A: 

Because:

  1. Java is not compiled to native code, meaning that there is a performance hit the first time the code is run.
  2. Java does not give you a predictable memory model (console games need this)
  3. Java does not give you deterministic object finalization.
  4. Java is not as close to the hardware as C is, an essential for a lot of professional 3D game programming.
  5. Console programmers likely don't have a JVM that runs on the PS3, X-Box, etc.
  6. Runtime performance penalties.
  7. You will never be able to squeeze as much performance out of a Java app as you can with a C++ app.

There are probably more reasons, like the fact that they are using pre-existing code that was written in C or C++.

EDIT: As an aside, I don't think that many modern games are written in C. OOP lends itself to game development, and C++ is the de facto language of choice.

Also, I won't add it to my list, but as others have mentioned there is a lot of pre-existing code that works very well that is used in the game industry. It would not be practical to rewrite all/most of your tools just to switch to a new language especially when that switch could cause you a lot of headaches.

Ed Swangren
The BestGameEver, Quake III Arena, is written in C :-)
frou
C++ != OOP. It is a tool that can facilitate OOP. You can write procedural code in C++ as much as you can write object-oriented code in C.
Daniel Pryden
I should have said not many *modern* games, and I don't think you can argue that. @Daniel: I don't know what your point is there.
Ed Swangren
The JIT does compile bytecode to native code and profiles it on the fly. In principle, JIT compiled native code is faster than ahead-of-time compiled code because the JIT can optimize based on profile info, which an AOT compiler can't. Your points 6 and 7 are vague and don't say anything.
Jesper
"Runtime performance penalties", as in, array bounds checking, etc. And yes, JIT compiled code can be optimized for the machine it is running on, but you do not have the control to take it further than that if needed. You can talk about this all you want, but the fact remains that professional games are not being written in Java.
Ed Swangren
Aside from point 4, I find your list of points quite vague or irrelevant.
Pool
Ummm...what exactly is vague or irrelevant? I believe they are all very relevant and clear.
Ed Swangren
A: 

I wouldn't say that it is always true that C is better than Java for game programming.

For example, if you want to write a game client which can be hosted in a web page, Java could be better.

When writing games which produce amazing 3-D graphics (e.g., Halo on the X-box), usually there is barely enough computing speed to generate all the pixels for each frame. In this kind of game, C would be preferable to Java because it allows the programmer to write faster programs -- at a large expense in terms of difficulty in extracting that speed.

Heath Hunnicutt
+1  A: 

I think it's mostly because c lets developers squeeze every last little bit of performance out of hardware, whereas Java doesn't, it's not low level enough for things like high end 3d video renderers. Basically c lets you squeeze out a couple more frames per second in your next gen shooter.

Kris
+7  A: 

I would say, despite the other answers pointing to a lack in speed caused mainly by the JVM, that the real reason people don't code games in Java is the lack of support for environments such as DirectX and OpenGL (which actually remove the need for your code to be close to he hardware as it was suggested by some answers). They are the base frameworks that people generally use to code games, especially nowadays with 3D games being everywhere - and lack of support for them is the reason why Java is not considered as a language for game development.

To emphasize my point, I would suggest you take a look at Microsoft's XNA which is currently optimized for coding in C# via the .NET framework (which like Java is Just-In-Time-Compiled and doesn't run natively per se). The XNA framework interfaces with DirectX which talks to the hardware and so it is very fast.

EDIT

@Ed Swangren's comment made me realize yet another distinction between .NET vs Java when considered for game development. I think another strong point to .NET is that if you do need to be able to squeeze out that last bit of performance and do some pointer math or implement a sophisticated high-performance algorithm it's a lot easier thanks to the unsafe mode. Of course you can even go beyond that and write native libraries to be used by your C# code which is made pretty simple thanks to P/Invoke.

Miky Dinescu
Yeah, until you write a serious game and find that you need to eek out a few more FPS, but... oops, you don';t control the code doing the real work.
Ed Swangren
there are java bindings for opengl. google "jogl".
asveikau
Also google LWJGL which has been used commercially and is very fast. Java could link up to DirectX if someone wrote it - it's just a main reason for Java is to be cross platform - and DirectX is not.
Eli
This is getting slightly off topic now, but http://www.jmonkeyengine.com/ is a great little java 3d toolkit for those interested in a hobby.
reccles
+4  A: 

Leaving C(++) aside for the moment... I am inclined to say much of the reason is that Java lacks anything like XNA. What advantages does Java actually have over a language such as C++ when it comes to game development? You have to consider that several of it's typical advantages disappear for the specific area of game development, while C++ gains several.

XNA is what made C# a highly popular language for amateur game development, and contrary to common belief, a quite viable option for commercial development too. C#/.NET being a parallel to Java in so many ways (and arguably a better framework nowadays), when people now have the option for game development with a higher-level language, C# would seem like the much more appealing one, unless cross-platform support is essential (then again, we have Mono and OpenGL for .NET).

C (or rather, C++) has long been the language of game development due to their low-level nature (thus performance benefits) and the host of graphics frameworks (DirectX, OpenGL) and engines that primarily target them. It's usage is embedded in game development and been used virtually since the inception of the industry - and won't disappear any time soon, I suspect.

Noldorin
+3  A: 
  1. Java doesn't have as controllable performance
  2. Its highly reversable so harder to protect
  3. Many Games employ scripting languages like lua or python to get "higher" level programming
  4. the API of most systems is C oriented.
  5. Java can be used for back end server systems that games connect to
  6. Flash games seems to of taken the niche Java games could have had.
Keith Nicholas
+17  A: 

The reason Java (and C#/.NET) is not a viable option for AAA titles at this point is the established game engines and their toolchains are written in C++. Game development is all about getting a title on the market in the shortest amount of time, and budgets don't allow for piddling in things like a new language/engine when several are already available, work well, and have an extensive set of editors and tools backing them.

Moving to Java (or C#) would also require a new performance-driven JVM (or CLI) across the big-3 (PC, X360, PS3) or big-5 (add Wii, iPhone). It's technically doable, but not financially viable.

Edit: Anyone with low-level knowledge of both virtual machines and the current state of game engines can tell you that a JVM or CLI could unquestionably be implemented with a new game engine to beat the performance of current C++ engines. The preventing factor is time and money, nothing more and nothing less.

280Z28
+1 - Your edit seems to be the only answer from anyone who has actually messed around with the speed of the JVM.
Eli
Although none of this applies to console development, where the big money is.
Ed Swangren
You'd be surprised how many game *tools* are written in java - especially custom inhouse tools. Java is easier to program, and a game tool wont require the bare metal speed a game needs, thus java is a good choice.
Chii
@Ed: Did you read my post? I'm talking specifically about consoles.
280Z28
Sorry, I think I was actually looking at another post when I left that. You are correct that (of course), virtual machines could be created that run on consoles, it is simply not a practical thing to do.
Ed Swangren
+1  A: 

I think C or C++ is a better language for building many types of games because it is closer to the hardware and likely to be the first language implemented on any new hardware. Not only that the libraries for accessing many of the advanced features of today's hardware are likely to be implemented in C.

Your typically general purpose higher level language has no easy way to access features of the hardware unless it uses some type of binding layer to call the libraries which are written in C.

For instance how do you write code to access a GPU, or write a custom Shader, or write code that run well on a Cell chip, or run on an Iphone, or on a Blackberry in a high level language. Even when these things are supported, they come out well after other people are able to write games in C that use these features in games.

One compromise you can make is to use a higher level language like Java for most things and C where its needed. You will limit the types of platforms you support though.

Java might also be good for client/server games where the server is written in Java.

BeWarned
A: 

Inertia mainly... Although it's a bit old now, you can look at Jake2 (which is a pure Java port of Quake 2 with jogl as the openGL lib).

It can perform up to 85% as fast as the C++ original, which means it's fine for most games; especially modern ones which are more social and game play based rather than the limited "hard core" games.

I'd also suggest that most of the answers you get here are coming from a gaming geek/"I want the coolest hardware and games". For [these] hard core 3D games and gamers, that final 15% is hugely important, as that's what separates the $150 graphics card from the $500 one they just bought.

As John Carmack is reported to have said (something along the lines of): "If I were to enter games programming now, I'd program for the iPhone". e.g. it's not nearly as much fun to make the fastest game with the best 3d engine as it is to make the best game.

Stephen
+1  A: 

Java can be optimized to be very fast, as is evident by an interview I had recently with a high-frequency trading company, where they do use Java, as well as C++.

Java has OpenGL bindings, as others have pointed out, so getting to the hardware isn't such a problem, esp since not all games need that, some commercial games have been written for Java3D.

You can use Scala or F# if you want some more performance for multi-threaded or numerically intensive operations, and just tie those in with the GUI.

But, as others have mentioned, the tools that are used tend to be written for C++, and some companies feel more comfortable doing some optimizations in assembly, but, given the fact that the new cpus are very complicated, with multicores, it is unlikely you will get any performance increase over the optimizations from the compilers, but, as long as companies feel these optimizations are still needed, they will stay with C++.

If some developers wanted to write commercial-grade tools for Java or .NET, there could be a market opportunity there, but it will be a great deal of work to make it as good as what is already out there.

James Black
A: 

The console game market is about 10X larger (financially) than the PC game market. For current generation consoles, all the major programming is in C or C++ with some little bits of intrinsics or assembler. If you want to be a professional Game Programmer and work at any major game company, then learn C and C++.

More to the point, all the commercial cross platform engines that are that starting points for many commercial games are in C or C++ right now as well.

You may be able to work on small games in java and flash or even casual Windows games in C#. You can even code C# for XNA games but if you want to make REAL (pressed on DVD) XBOX 360 games you need to learn C++.

Speaking as a game developer at WBGames Chicago. We would not hire a programmer who didn't have strong C++ skills. The same is true at every other game studio that I know of.

Adisak
Oh, and FWIW, you can't even ship games written in java on PS3, XBOX-360, Wii, PSP, GBC, etc.
Adisak
A: 

The short answer is because almost no gaming platforms support Java, or if they do there is a much better alternative.

On the game consoles, seasoned game developers are using C because that's what they've been doing for 20 years. There's no compelling reason for Java games to exist there.

On the desktop, there are a few Java games, but they can't compete with the market leaders such as World of Warcraft.

On the web, Applets are long dead, and if you're going to make a casual game it's going to be in Flash.

In the mobile space, JME has had it's time in the sun but it's on its way to obsolescence in the face of far superior platforms such as the iPhone.

The perfect world scenario for Java to be a heavy hitter in the gaming world would be for Sun/Oracle to deliver a gaming platform like the Wii or the iPhone - a must-have device that everybody owns. Then, there is a good reason for developers to invest time into experimenting with new technology. But given Sun and Oracle's apparent lack of interest in the consumer market, the chances of them making a gaming rig are slim to none.

bpapa
+1  A: 

It's very hard to write a program that runs in constant memory without garbage collection in Java.

Crashworks
No, garbage collection is really not a significant problem.
Pool
People keep telling me this, but no one has yet shown me a garbage-collected runtime that can run in a constant 256mb of memory without fragmentation or hitches of more than 1ms.
Crashworks
A: 

It's not a technical issue but purely a "client optics" issue for this.

Good Java code can run just as fast as good C++ code. It can access OpenGL just as fast as native C++ via projects like LWJGL and JMonkeyEngine.

The real reason is that Sun never put any effort into highlighting Java for games and they've never made any deployment particularly easy for Java games. It's a LOT of work and effort of the developer / team to create a seamless / smooth installation (and launch?) experience for the player.

Customers just don't "trust" Java games and bail during the install process when they see the logo.

eyuzwa