mani_jeddah
Jan 15, 2008, 10:09 AM
Hey
Am student of computer science
This semester we have covered object oriented programming in C++
In upcoming semester we will do it in java "oop in java"
So I want to ask about the similarities by the way c++ n java and diffrences wheather in concepts or scope
Kindly I want technical ans not flying on air!
slapshot_oi
Feb 20, 2008, 10:32 PM
I'm one semester ahead of you, I'm taking the OO course in Java now, I took C++ last semester. This is what I've learned in the three weeks of school I've had this semester.
______________________________________
C++
______________________________________
Call by Value language - Just like C.
Pointers and References - Yup, they still exist in C++, however, the C++ convention is to use references--aliases for objects, not a holder of an address--instead of pointers. They are safer to use. Only use pointers when you need to.
User defined operator overloading - This can get hairy, + can mean -, up can mean down, you can really get lost if you're not careful.
_______________________________________
Java
_______________________________________
Dynamic allocation - All objects in Java are instantiated with the "new" keyword and stored in the dynamically allocated heap area in memory, rarely will Java ever use the run-time stack. Remember malloc() in C? Yeah, what a piece of crap.
No pointers or references - Yes, no more pointers. This is a safety feature of Java so that programmers won't crash their programs and get the dreaded "Segmentation Fault". It makes programming easy but less powerful and slower.
Object Oriented - You will learn that C++ is not entirely object oriented. A true OO programming language has no primitive types (no int, char, long, etc.), Java is not truly OO in nature either, but it is closer than C++. Java has "wrapper classes", they are classes built around the primitive types to give them OO properties, like inheritance.
Call by value language alteration - In OO programming, it's good practice to pass objects to functions as references rather than making a copy of an object, this is simply because objects can be huge. In Java, you do not have to specifically send objects to methods (member functions as you would know them in C++) as references, Java does this automatically. This characteristic would make Java a Call by Reference language, however, Java does send a copy of an argument to a method if the argument is a primitive type, hence it is still Call by Value.
Garbage collection - No destructors, worrying about unfreed memory or dangling pointers, Java will do it for you. This was such a pain in the when dealing with C.
Everything must be a class - In C++, you'd write your main function as a separate C++ file and that would be it, making half your program OO. In Java, your main function has to be part of a class. Because of this, the main function has to be declared as "static" so it will be stored in the global and static data area of memory and will remain there throughout the remainder of the program. In C++, main was the first to be put in the run-time stack. This is probably th most confusing concept in Java, and yet no one bothers to explain well. Please pay close attention to this and your transition will go much more smoothly.
On a personal note, I like both languages, and I know you will too. I like C++ more because the programmer has more freedom and with that comes more responsibility, but I like Java more because it's easy to write GUIs and has excellent documentation. Java is easy to learn, but no way in hell would I recommend learning an OO programming language without learning a language as bare-bones as C.