Archived

This topic is now archived and is closed to further replies.

Java vs C++! How close are they really?

This topic is 5090 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sadly, while I'm going into game design and development as a future career and that uses C++, it is Java that is used for my up coming computer science courses. I've taken a few peeks at Java, but nothing serious. Enough to know it is similar in many ways to C++. My question is this though: HOW close is it to C++? Some of the syntax and such seems almost dead on exact to C++. If I end up having to learn Java as my primary language and put C++ on hold till college ends, how much will transfer over and how much will be useless to me? Thanks for any comments (Edit: Comments on C# and J# comparisons to C++ wouldn't be useless either :D) [edited by - Emonious on January 3, 2004 12:30:02 AM]

Share this post


Link to post
Share on other sites
I had no problems switching from Java to C++. The beginning hurt my head a little with headers and the different way of declaring classes and pointers, but it was worth the trouble. I am never going back! Yes I know that Java can do everything C++ can, but a lot of times it just doesn''t feel like it was made for it.

Share this post


Link to post
Share on other sites
Apart from some things that are "missing" in Java (features that C++ has, but not Java, such as operator overloading), the biggest difference between them is in how they handle memory. Because of that, it can be a little confusing switching between them after coding in one of them exclusively for a longer period.

Edit: Do a search if you want to know the concrete differences. Java vs C++ questions are extremely common.

[edited by - eighty on January 3, 2004 12:51:51 AM]

Share this post


Link to post
Share on other sites
I guess what I was hoping for was more of a few examples of the major differences that I''d find or run into.

Doing a search on such a large topic like this can take quite a while, when a question could be asked to the experts instead.

Share this post


Link to post
Share on other sites
I program in both C++ and Java fairly regularly, and the main differences that I can tell are that Java lacks generic functions(templates), 1.5 will fix that when it comes out. Java also lacks operator overloading, except they actually used it for the string class, which is stupid. Although I am sure calling a method for concatination would get painful after a while like String.equals("blah") is painful after a while. Although pointer syntax does gat some taking used to when going back and forth, the main differences is that both languages have their own application. And that is up to the programmer as which is easier for their particular project.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A big difference you''ll notice is that in java you declare a class in one file and everything must be in classes, including you''re main() function. In c++ you''ll usually have a header file (File.h) where you declare the variables and function prototypes and a File.cpp file where you implement everything. I hope this helps:

Java:
public class Example {
private int doggy;
private double cat;

public Example(int d, double c) { doggy = d; cat = c; }
public int getDog() { return doggy; }
public double getCat() { return cat; }
}

C++:
// File.h //
class Example {
private:
int doggy;
double cat;
public:
Example(int d, double c);
int getDog();
double getCat();
};

// File.cpp //
Example::Example(int d, double c) {
doggy = d;
cat = c;
}

int Example::getDog() {
return doggy;
}

double Example::getCat() {
return cat;
}

this was a silly example but i hope it gives some idea

Share this post


Link to post
Share on other sites
Really you don''t have to worry about it. You''re sweating the details and you don''t need to. It doesn''t matter if you use Fortran, Basic or Java in school. Focus on the concepts and the principles they teach you. Once you become fluent with one language picking up another is a simple matter of learning the syntax and features specific to that language. By the time you get to that point, it will be easier than you think. So don''t stress over it.

Share this post


Link to post
Share on other sites
Thanks for the explaination and examples guys, they are appreciated.

I''ll take Aldacron''s advice and just relax and learn what comes my way through the class. Nothing major it looks like difference wise.

Thanks again.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by eighty
Apart from some things that are "missing" in Java (features that C++ has, but not Java, such as operator overloading)
What about things that are missing in C++ but Java has: nested/anonymous classes with dynamic closures, and reflection (at least)?

Share this post


Link to post
Share on other sites
quote:
Original post by Aldacron
It doesn''t matter if you use Fortran, Basic or Java in school. Focus on the concepts and the principles they teach you. Once you become fluent with one language picking up another is a simple matter of learning the syntax and features specific to that language.


That''s only valid as log as you don''t learn some language like ML (the freshmen at my Uni do, it''s fun seeing them run some interpreter from 1980) which will probably help them less in the later software engineering courses than what they''ll learn in theory courses. Don''t get me wrong, the concepts of those languages might be all nice for their purpose but for quick hacking, C(++), Java or C# (which seems to be very close to Java in terms of functionality and use) seem to be better without much distinction between them.

As for getting into any language: a short primer on the basic control flow syntax and the respective details like pointers in C(++) should get you up and running in no time.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Shadowdancer
for quick hacking, C(++), Java or C# (which seems to be very close to Java in terms of functionality and use) seem to be better without much distinction between them.
For quick hacking, the ones you listed aren''t particularly good. Try a scripting language for quick hacks, e.g. Python or Ruby and waste half the time (or less) you would in the languages you mentioned.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
For quick hacking, the ones you listed aren''t particularly good. Try a scripting language for quick hacks, e.g. Python or Ruby and waste half the time (or less) you would in the languages you mentioned.


Better than ML. "Quick hack" as in not having to care about what lambda calculus is ;-)

Of course, nothing beats Perl in terms of most unreadable code in a minimum of time.

Share this post


Link to post
Share on other sites
Java and C++ is an interesting thing... cos they look very similar on face-value (a smart move on Sun''s* part)

However, when you get down to some proper programs (ie, a cut above the usual 100-300 liners that a CompSci course tends to require), then they are pretty different.

I always find that Java is more like a cocooned shell - I like it because I know that it''s reasonably hard to break, and I''m not likely to hurt myself with it yet its more of a world-of-its-own, and you don''t get much "feel" of the OS or hardware that you''re using. Oh, and whilst its nice to have a built in UI/GUI library (Swing), it really is butt-ugly

As for C++, it''s more like programming a specific computer - you are controlling the CPU (making sure you don''t screw up the memory and so on), and you have a much better feel of what the OS/hardware you''re using is like... The design style that ptrs lead towards is interesting. However, you screw up and it''ll make you very aware of it and there''s rarely a nice safety net to fall back on!

hth
Jack




*I know they didn''t actually create the first version of Java, but I forget who it was...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by jollyjeffers
*I know they didn''t actually create the first version of Java, but I forget who it was...
Gosling was the main designer of Java, and I''m pretty sure he did work for Sun when he was designing it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Doesn''t IL2 Sturmovik the WW2 simulator use Java? I think I''ve noticed that while installing it, a JVM was also installed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Brazillian universities are also teaching Java instead of C++. In mine, we studied C structures and pointers, but OOP and everything else about it was teached in Java, so that we could use switch between C++ / Java as projects would need.

What I would like to know is, the performance of C++ programs are second to none, and that''s why they are a good choice for game developing. But, is there a way to "speed up" java, so that we could take advantage of it''s features?

Share this post


Link to post
Share on other sites
The main problems Java has in games is the garbage collector. Avoid reallocating objects over and over in your inner loops. Reuse when possible. Make an explicit call to the garbage collector before the action starts (for example after you have loaded the level data).
Also, learn a bit about how JITs work. What they can optimize and what causes problems. Some OOP practices are bad for game performance (like too many unneeded news), other actually help the JIT to optimize something.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
But, is there a way to "speed up" java, so that we could take advantage of it''s features?
Just remember the 80-20 rule and only write some of the most time-demanding parts in C++. There are commercial high quality games that use languages as slow as Python (without JIT it''s over 10 times slower than Java), but still maintain pretty high execution speed because the slowest parts are C or C++. Lisp has Java-like speed and is used in e.g. Spiro The Dragon for Playstation. Some game programmers tend to overemphatize speed even though the most successful games (GTA3, Sims) don''t have very optimized code. Silly.

Share this post


Link to post
Share on other sites
Whether you can use Java as an embedded language or create a full scale Java app depends on your project too. I wouldn''t write an FPS shooter in all Java (although there are people who would do it, but FPS is not my kind of game anyway) but writing a turnbased strategy game in complete Java has much more benefits in my opinion.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I can''t imagine a whole game written in java. it doesn''t seem to have any good graphics capabilities or operability with native code... except if you take J# maybe

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Doesn''t IL2 Sturmovik the WW2 simulator use Java? I think I''ve noticed that while installing it, a JVM was also installed.


it may have been using java for a scripting language.. i remember reading a postmortem for vampire: the masquerade(sp?) where they said that one of the main things that went right for them is the java interpreter they used for scripting.



Get busy livin'' or get busy dyin''... - Shawshank Redemption
If a man is talking in the forest, and no woman is around to hear him, is he still wrong? - Unknown
Fulcrum

Share this post


Link to post
Share on other sites