Java for C++ folks
June 17, 2007
1 comment
I’ve been learning Java for a while now, and I’m trying to highlight the major differences between Java and C++ here. This is for all you C++ people who’ve always wanted to look into Java.
Readers are encouraged to comment on the things that I’ve missed.
Note to the newbie : Don’t try to understand what I’m trying to say, This is more like a table of contents. Google for what they all mean!
- Each file should be named after a public class in the file
- All non-local variables get initialized. All objected references get initialized to null
Also: Its an error to declare a variable and not initialize it within Java methods.(Thanks vijay03) - Objects need not be explicitly deleted. All variables are garbage collected
- Local Variables override scope in c++. It’s simply an error to do so in Java.
eg:void foo() { int a=2; { int a; a = 4; } }Valid in C++, Invalid in Java
PS: Doing this is EVIL! - All objects (NOT primitive types) are passed by reference. All variables in Java act like C++ aliases.
- Methods of the subclass *always* override superclass methods in Java. That is, All methods in Java behave simply like virtual methods in C++
- The default scope of any member/method of a class is package scope
- protected members in Java can be accessed by any code that’s in package scope of the class and by inheriting classes.
- Java doesn’t have templates. But then i heard they had something called generics that is close to templates. I haven’t personally gone through them
- Java doesn’t have multiple inheritance and you can apparently get away by using interfaces
Categories: Java