Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java----C/C++

Started by gsmaster May 3, 2006 at 3:34 PM 47 replies 11.9k views
Original Post
gsmaster
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?
Simian Man
Simian Man
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
stecal849
stecal849
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
programwizard
programwizard
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.
gsmaster
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.
Sneftel
Sneftel
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.
gsmaster
gsmaster
Ahh, and can somebody explain the basic idea of a 'pointer'. Seems you all are skittish about em =D
stecal849
stecal849
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)

Photonman
Photonman
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.
Zahlman
Zahlman
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.)
Zahlman
Zahlman
Quote:
Original post by gsmaster
Ahh, and can somebody explain the basic idea of a 'pointer'. Seems you all are skittish about em =D


If you've ever shared Java objects between containers (or other objects), or created a graph of Java objects of the same (super-)type, you already have experience with them.

You might want to watch this - first the Java version, then the C++ version.
Zahlman
Zahlman
Quote:
Original post by Photonman
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.


You can always call mutators on a passed-in object and have the changes seen by the caller. If you wanted to *replace* a passed-in object with a different one, well, in Java that's a design smell. After all, there's no operator== in Java, or even a built-in moral equivalent. In Java, you usually think more in terms of aliasing objects than copying them, and only clone() when you really have to. After all, you don't have to worry nearly as much about object ownership.

If you need to modify passed-in primitives, then some hacking is needed :) Typically you wrap primitives either in a class (it might represent all the parameters to the function call, or just individual ones. Note you *cannot* use java.lang.Integer, etc. because they are "immutable"; the object provides no way to modify the represented value) or an array.

There are several pages on the c2.com wiki about all of this - have a look for yourself.
stecal849
stecal849
Quote:

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.


As far as I know java does support pass by reference.

Ste
Lazy Foo
Lazy Foo
Well if you plan to be a become a professional game programmer a strong background in C++ is a must. I see a lot of places demand 5 years of a experience.

Otherwise, just use java because it's what you're used to.
Learn to make games with my SDL 2 Tutorials
Lazy Foo
Lazy Foo
Quote:
Original post by Anonymous Poster
Quote:
Original post by Photonman
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.


You hate Java because you know nothing about it, all objects are passed by reference.


Yeah but it was annoying having to deal with bullshit wrapper classes for primitives.

Fortunately, Java 1.5 does some operator overloading to make the experience more tolerable.
Learn to make games with my SDL 2 Tutorials
Photonman
Photonman
To clear up the ambiguity before people FLAME ME, I was referring to POD types. Sorry if I thought that would be caught from context, considering the examples in my post only dealt with ints........

I just find not being able to change multiple POD (happy?) values with a function and have those changes apply to the scope that called the function without anything more complex than adding a couple of ampersands too limiting for my taste. I didn't expect the Spanish Inquisition......
______Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
stecal849
stecal849
Primitives (int, float, arrays etc ) are passed by value. Objects are passed by reference.

Ste
rohde
rohde
Quote:
Original post by Anonymous Poster
Quote:
Original post by stecal849
Primitives (int, float, arrays etc ) are passed by value.
Ste


Are you sure about arrays? Somehow I don't think so.


In Java arrays are pure objects and hence also passed by reference.

Actually when comparing Java and C++ it's not enough to say that Java passes objects by reference since we have pass by reference in C++ as well, but the semantics are different than in Java.

The 100% correct terminology is to say that Java passes references by value.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by Anonymous Poster
Basically, follow these rules with pointers:
-initialise all pointers to NULL, or to a value returned from 'new'.
-allways check if a pointer is not NULL before using it.
-after an object has been deleted, set all pointers which were pointing to it to NULL.
- When using 'new', ALLWAYS document who will keep copies of the pointer to the object, who is responsible for deleting the object, and how all pointers will be set to NULL when the object is deleted.

These are very good hints for C++ newcomers. I'd like to emphasis them more.

Quote:
Original post by Anonymous Poster
Going from Java to C++ is a LOT harder than going from C++ to Java.
Too many people who come from managed languages to C/C++ end up doing things like this -
char* myString;
char* otherString = "some text";
strcpy( otherString, myString );
Which will crash, or worse, wont crash and will just corrupt random memory.

Yes - that's why C++ newcomers should be teached std::string before C-style ASCIIZ strings. When I give C++ course (yeah, it happens), that's what I do.

You are the kind of AP I really like - the reason why AP should not be banned from this forum. Thanks a lot :)
Fred304
Fred304
There is no pass-by-reference in Java, Java only knows pass-by-value.

This means that changes to the contents of the formal parameters do not affect the actual parameters. An example:
public class Test{	public static void main(String[] args)	{		int a = 10;		LinkedList l = new LinkedList();		javaPassesByValue(a, l);		assert a == 10;		assert l.isEmpty();	}		public static void javaPassesByValue(int x, Collection c)	{		x = 5;		c = new ArrayList<String>(100);		c.add("test");	}}

In other words, you cannot change the reference. As already pointed out, the reference is passed by value.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.