• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gsmaster

Java----C/C++

48 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Ahh, and can somebody explain the basic idea of a 'pointer'. Seems you all are skittish about em =D
0

Share this post


Link to post
Share on other sites
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)

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.)
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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......
0

Share this post


Link to post
Share on other sites
Primitives (int, float, arrays etc ) are passed by value. Objects are passed by reference.

Ste
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0