8
\$\begingroup\$

Developing in Android is going to be different for me, having spent the last 10 years coding games in C++. What's the main differences / pitfalls / bottlenecks to developing for the Android? My big worry is moving to Java, what am I going to miss most?

To be clearer: what would I miss if I coded the whole thing in Java compared to the way I normally code C++ for consoles?

\$\endgroup\$
2
  • \$\begingroup\$ Title could be a bit better. Just to be clear are you asking "What is the difference between C++ and Java on the Android Phone?" \$\endgroup\$ Commented Aug 17, 2010 at 13:22
  • 6
    \$\begingroup\$ I think if you use the NDK (developer.android.com/sdk/ndk/index.html) then you can write most of your code in C++. \$\endgroup\$
    – Chris Howe
    Commented Aug 17, 2010 at 13:59

3 Answers 3

6
\$\begingroup\$

The single biggest thing that you'd probably miss would be explicit memory management. On the plus side, Java has run-time type information and real generics. Also see this link for some tips from Google on how to keep your Java code efficient.

Raymond Chen has recently been venting about garbage collection and memory management in the CLR and his blog posts are great for understanding how it works. (I know the following links are for the CLR, but they're still relevant for Java development). See especially here and here.

That being said, there isn't that much difference between Java and C++, and as Chris pointed out, you can code in C/C++ on Android using the NDK.

\$\endgroup\$
2
  • \$\begingroup\$ thanks, "you can code in C/C++ on Android using the NDK", yeah I appreciate that, but why stick to old guns all the time if something actually turns out projects better / faster. \$\endgroup\$ Commented Aug 17, 2010 at 15:08
  • 2
    \$\begingroup\$ Oh, I see what you're getting at. Java has the bonus of having a massive standard library and an extremely competent library for dealing with XML. The Android platform extends the standard library with libraries for all of the fancy hardware goodies and some extra, user-side functionality. Java also supports incremental compilation/error checking, so you'll get feedback on errors pretty much as soon as you type them. Honestly, you shouldn't fear performance either. It's not going to be as fast as C on bare metal, but it's still quite speedy. I would say that there isn't much to worry about. \$\endgroup\$
    – brett
    Commented Aug 17, 2010 at 15:17
4
\$\begingroup\$

Google's Chris Pruett talked about writing real-time games for android in Google I/O 2009 and again in 2010. He talks about Java performance on Android among other topics. He also made a game, Replica Island, in Java and you can download the source code to get an idea of how an Android Java game might look like. His blog about the game also has several insightful articles.

The typical pitfalls are similar to those of writing in other garbage collected languages, you use a object pools and allocate stuff in advance to avoid having the GC run when you don't want it to. Methods are virtual by default in Java and it helps to mark them as final when you don't need to override them. The Android Eclipse plugin comes with some neat tools to help you profile your application and monitor heap allocations.

\$\endgroup\$
2
\$\begingroup\$

You are worrying too much. The API for OpenGL in Android, OpenGL-ES, was deliberately kept C-like. See the OpenGL specs at http://www.khronos.org/opengles/ for details.

It is even possible that you won't miss much of anything from C++. A lot of developers, even the famous programming author Bruce Eckel ('Thinking in C++', 'Thinking in Java'), have found Java a much more effective language for object-oriented programming. Largely because the things they 'missed' at first turned out not to be so important after all.

That said, what I miss most is the ability to override operators, not just methods. The syntax really is clumsier when you have to define/override methods instead of operators.

Consider, for example complex number arithmetic. It is really elegant in C++. Not so in Java. But this turns out to have minimal impact on most real projects.

Some people complain about the inability to do multiple-inheritance. But the most useful kind of multiple inheritance IS the kind that Java does with the 'interface', so you shouldn't really miss it that much.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .