Java----C/C++

Started by
47 comments, last by Fred304 17 years, 11 months ago
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.
Advertisement
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.
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
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

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

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

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

This topic is closed to new replies.

Advertisement