Java----C/C++

Started by
47 comments, last by Fred304 17 years, 11 months ago
I'm currently a fairly moderate Java programmer, and have been thinking of getting into C/C++ mainly because it seems to have the capability to make more complex games more easily, from what I have seen. What I'm wondering is, in terms of coding, and not the capabilities of language, how does Java differ from C/C++? Will having a backround in Java help you to more understand C/C++ at all?
Advertisement
The big differences between Java and C++ are the things C++ lets you do that Java doesn't:

- Procedural Programming
- Generic Programming (Java has this too, but it's not so strong)
- Multiple Inheiritance
- Operator Overloading
- A host of dangerous things involving pointers

Whether these things are good or not is open to debate, but C++ generally assumes you know what you are doing. In my opinion, I like this better because it allows me to program however I want and not the 1 paradigm Java is good for.

That said, from a utility point of view, there is no real reason to move to C++ if you are happy with Java.

Just my two cents
Quote:Original post by gsmaster
I'm currently a fairly moderate Java programmer, and have been thinking of getting into C/C++ mainly because it seems to have the capability to make more complex games more easily, from what I have seen.

What I'm wondering is, in terms of coding, and not the capabilities of language, how does Java differ from C/C++? Will having a backround in Java help you to more understand C/C++ at all?


Hi,

I was in a relatively similar position a couple of years back. Java was the language of choice for my comp sci degree so I decided that C++ would be great to use along side Java.

The differences between coding for both languages are in Java you technically dont need to worry about memory when creating objects on the fly (although you should because it will make for better applications). With C++ you are the garbage collector. That is, you have to make sure that when you allocate memory you must deallocate it when your done otherwise everybodies loveable friend - the memory leak - comes round for dinner and has your program for the main course :)

This is true with the ability to use pointers in C++ that need to be fully understood before you start using them in serious development applications. I learned through mistakes and triumphs and I thoroughly recommend that you play about with pointer aritmatic and generally just get to know the language and its differences. Coming from a similar style of language you should have an understanding of all the object orientated techniques and how to use them and this will be an advantage to you.

Good Luck

Ste
Java would probably be more adventageous in understanding C++; the syntax's are almost indentical, and you will be used to writing "good" C++ code (OOP) because Java forces you to do many of these things. Other than that, there aren't a whole lot of differences: make sure you pick up pointers, templates, and a few other C++-specific features of Classes.

EDIT: Beaten to it...
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
And,on average, there are a lot more readily available C/C++ engines out there than Java ones, right? I realize making your own engine can be the best way, of course.
Quote:Original post by gsmaster
I realize making your own engine can be the best way, of course.

It's pretty rare for that to be the most practical option. Many people do it, but that's because they're more interested in making a game engine than in making a game.
Ahh, and can somebody explain the basic idea of a 'pointer'. Seems you all are skittish about em =D
Quote:Original post by gsmaster
And,on average, there are a lot more readily available C/C++ engines out there than Java ones, right? I realize making your own engine can be the best way, of course.


True, C++ built engines are in abundance and certainly at least attempting to write an engine of some sort in C++ will help your development. For my honours project I did just this and chose to implement a ray tracing application in C++ instead of Java. I learned alot through this and although its only a simple ray tracing application its made me a better C++ and Java programmer imo. So best thing to do would be to choose a subject that interests you and attempt to write the application in C++. It wont be the best application the world has seen but you learn through doing.

Get cracking lad!

Ste

(http://www.flickr.com/photos/93606551@N00/ i can send you the source if your interested, just pm me or fire an email)

Pointers scare a lot of people because it is very easy to do something wrong with them and make your whole program lose. A pointer is, simply enough, a variable that holds the address of another variable. They are declared by putting a * after the type of variable pointed to. ex)

int x;//An integer variable
int* p;//A pointer to an int capable o f holding and int's memory address

Now, you can set a pointer to point to another type like so:

p=&x

& is the address-of operator in this context. It returns the address of where x is in memory. The rest of the statement puts that address into p. Now p points to x. You can not modify the value of p, but you can (and this is why pointers are cool and also why they are troublesome) you can change x through p. Consider:

*p=5;

This "dereferences" p and assigns 5 to the value in it. In simpler terms, it puts 5 in the memory location that p points to. So now, given that p points to x, if you check the value of x, it will be 5. Fun, eh? Well, doing things wrong with pointers can be tricky to spot. However, their power is extremely useful.

Pointers can also be used to change the value of a variable passed to a function, although reference paramaters are WAY easier to use in this context. You will find that pointers and references do almost the same things. Stroustrup introduced references to be an easier to use kind of pointer, but could not remove pointers entirely because of backcompat with C, so C++ has both.

Pointer and Reference arguments allow you to change more than one value with a function. I hate the fact that Java does not have this. It is too useful. Just being able to modify one value in a function via return is too limiting. Note that C++ still has return, and functions can work just like Java methods using it.

There is more depth to pointers than this bare introduction, though. Check out the previous resources to learn more.
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
Quote:Original post by Simian Man
The big differences between Java and C++ are the things C++ lets you do that Java doesn't:

- Procedural Programming


Don't be ridiculous; you can do "procedural" programming in anything, including Smalltalk. In Java you typically accomplish this by egregious abuse of statics. :)

Quote:- Multiple Inheiritance


Of implementation, yes. Java allows multiple inheritance of interface.

Quote:Whether these things are good or not is open to debate, but C++ generally assumes you know what you are doing. In my opinion, I like this better because it allows me to program however I want and not the 1 paradigm Java is good for.


I think it's just as important to say that C++ makes you ask for everything you want, but doesn't charge for the things you don't ask for. Whereas in Java, everything is already virtual, array indices are bounds-checked, etc. without you having to say anything, but you don't have a way to reject that functionality.

As for the original question, a Java background will help understand C or C++ (these are separate languages. You really should not try to learn C specifically unless you need to target very specific devices, and you should be very careful when selecting C++ tutorials and references, because many give bad advice based on C legacy), but only marginally more than an X background would help with Y, for any X and Y. I say marginally rather than not at all because most of the syntax is the same and you have the same basic rules for what an expression can contain, etc. (These are things that you normally would not have to worry about much, unless you were switching to something like Lisp. But then, Lisp syntax and code structure have a number of great advantages; they're just more irritating ;) ) On the other hand, a Java background would probably be quite helpful for learning Python or vice-versa, because of the similar object models and the concept of garbage collection. (Although Python is still definitely higher-level than Java, I can't really think of anything that's really intermediate between the two.)

This topic is closed to new replies.

Advertisement