Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Java----C/C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
48 replies to this topic

#1 gsmaster   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 May 2006 - 09:34 AM

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?

Sponsor:

#2 Simian Man   Members   -  Reputation: 1010

Like
0Likes
Like

Posted 03 May 2006 - 09:51 AM

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

#3 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 03 May 2006 - 09:51 AM

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

#4 programwizard   Members   -  Reputation: 100

Like
0Likes
Like

Posted 03 May 2006 - 09:52 AM

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.

#5 gsmaster   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 May 2006 - 09:55 AM

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.

#6 Sneftel   Senior Moderators   -  Reputation: 1781

Like
0Likes
Like

Posted 03 May 2006 - 10:00 AM

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.

#7 gsmaster   Members   -  Reputation: 120

Like
0Likes
Like

Posted 03 May 2006 - 10:02 AM

Ahh, and can somebody explain the basic idea of a 'pointer'. Seems you all are skittish about em =D

#8 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 03 May 2006 - 10:03 AM

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)



#9 Photonman   Members   -  Reputation: 174

Like
0Likes
Like

Posted 03 May 2006 - 10:56 AM

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.

#10 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 03 May 2006 - 11:44 AM

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.)

#11 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 03 May 2006 - 11:48 AM

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.


#12 Zahlman   Moderators   -  Reputation: 1682

Like
0Likes
Like

Posted 03 May 2006 - 11:53 AM

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.

#13 stecal849   Members   -  Reputation: 144

Like
0Likes
Like

Posted 04 May 2006 - 12:03 PM

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

#14 Lazy Foo   Members   -  Reputation: 1105

Like
0Likes
Like

Posted 04 May 2006 - 12:38 PM

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


#15 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 01:53 PM

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.

#16 Lazy Foo   Members   -  Reputation: 1105

Like
0Likes
Like

Posted 04 May 2006 - 01:58 PM

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


#17 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 02:07 PM

Quote:
Original post by Zahlman
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.


Honestly, the only time I needed to do this after years of Java programming was when I started using OpenGL binding for Java, which copies C function by function. C developers seem to have this need of passing primitives and changing them inside of methods, so weird.

#18 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 02:10 PM

Quote:
Original post by Lazy Foo
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.


I don't think so. I use the autoboxing because I have no choice, once the feature is there you MUST know how to use it, you like it or not, but I think this is a disaster waiting to happen to many projects.

Objects are nullable, primitive types aren't! Guess what happens when you try to "autobox" a null object?

I'd much rather be 100% sure that some value would never be null.



#19 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 02:22 PM

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.

Java wraps up all of the dangers features of programming for you, so you cant hurt anything (besides causing unhandled exceptions).

Java and C++ both have the 'new' keyword, but in java, the only way to create an object, is with 'new'. 'new' dynamically allocates memory on the heap, and returns an integer which represents the position in memory - we call this integer an 'pointer', because it points to an object.
Pointers are dangerous, if you arn't careful, and the only way to get better at using them is by using them.
The other big difference is that Java doesnt have a 'delete' keyword like C++. java just cleens up your shit, where as you have to tell C++ when you are finished with a heap-allocated object.

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.

If you dont know what I mean by heap-allocated objects and stack-allocated objects, please, please get some books on computer organisation and data structures, or do some computer science courses in them!
Compared to Java, which runs on a virtual computer, C/C++ is a low-level language which interacts directly with the the hardware, and if you dont fully understand the organisation of the computer hardware and the operating system modules which sit on top of it, you will not be an effective programmer.

I reccomend these books:
Every C++ programmer MUST read 'Effective C++ 3rd edition', i think its by scott myers. Again, once you learn C++ and think you're good at it, you MUST read this book to actually use C++ effectivly!

For learning classical data structures, Mark Allen Weiss has some very good books in both Java and C.

For understanding memory management (good idea if you're new to pointers!) get "Operating Systems: A Modern Perspective". It also covers how the computer deals with threading, the file system and other goodies which will greatly improve your comprehension of what you are actually doing when you program! Theres a lot of deprecated behaviour which is covered, but its a good history lession to know what problems we have had to overcome to get where we are with computing, and how to solve them.

#20 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 May 2006 - 02:28 PM

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.


No, all Java objects are passed by pointer.

In C++ you have pass-by-value, pass-by-pointer, and pass-by-reference.
Many people (even teachers) often mistake pass-by-pointer for pass-by-reference, and end up thinking they are the same thing. But in C pointers and references are quite different.

But yes, they both allow the passed in argument to be modified within the function, so Photonman is mistaken to say Java cannot do that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS