Home > Java > Java for C++ folks

Java for C++ folks

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!

  1. Each file should be named after a public class in the file
  2. 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)
  3. Objects need not be explicitly deleted. All variables are garbage collected
  4. 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!

  5. All objects (NOT primitive types) are passed by reference. All variables in Java act like C++ aliases.
  6. Methods of the subclass *always* override superclass methods in Java. That is, All methods in Java behave simply like virtual methods in C++
  7. The default scope of any member/method of a class is package scope
  8. protected members in Java can be accessed by any code that’s in package scope of the class and by inheriting classes.
  9. 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
  10. Java doesn’t have multiple inheritance and you can apparently get away by using interfaces
Advertisements
Categories: Java
  1. June 17, 2007 at 2:18 pm

    One more: Its an error to declare a variable and not initialise it within Java methods.

  1. June 5, 2008 at 1:37 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: