tags:

views:

178

answers:

7

I'm an experienced developer, but most of my OO programming experience has been with C++ (and a little Delphi). I'm considering doing some Android work, hence Java.

Coming from the C++ background, what areas of Java are most likely to surprise/annoy/delight me?

I felt sure this would already have been asked, but my searches haven't turned up a similar question.

CW, of course.

+2  A: 

Just remember that in Java everything is a reference. It seems so simple but as a C++ developer it will trip you up every time.

tloach
A: 
  1. You don't have to care about memory allocation and deallocation.
  2. No need to worry about assigning null to an object after its inception.
  3. By default a simple declaration of object would have null as its value. Everything in Java is an object
Bragboy
except int, char, boolean, etc...
tloach
We have wrappers for primitives too, dont we?
Bragboy
@Bragaadeesh: Yes, there are wrappers for primitives, but there are primitives that are not objects. It's an optimization that can have some unpleasant consequences if you aren't careful.
David Thornley
+6  A: 

There's an excellent article on wikipedia comparing the two.

James Kolpack
+3  A: 

I think one of the largest things coming from C++ is the lack of multiple inheritance. Java compensates (and in the opinion of many, surpasses) multiple inheritance by focusing on Interfaces and object composition.

You'll be forced to think differently about your OO design, but the end result can often be cleaner and more maintainable.

Jason Nichols
+1: Having learned Java after C++ myself, one of the two unexpected delights were the capabilities enabled by Interfaces (the other one was runtime reflection).
Lars
+2  A: 

If you use RAII, forget about it in Java. Without knowing when an object will be deleted it is not possible. Keep in mind when writing destructors that they could be executed well after your object goes out of scope.

tloach
Or they could just not be executed. I've never quite figured out what a finalize method that might be called sooner, later, or never was good for.
David Thornley
@David - ironically you could probably use it to determine if the GC had just run - letting you know that anything time-sensitive is now in trouble ;)
tloach
+7  A: 

surprise:

  • Almost everything is on the heap
  • It can be as fast as C++, even faster in a few cases
  • The autoboxing of primitives will occasionally cause headaches

annoy:

  • no unsigned integer types
  • no preprocessor directives of any kind
  • no operator overloading
  • generics are castrated templates

delight:

  • blessedly quick compilation
  • No memory management
  • No segfaults
  • Most error conditions result in a stack trace that often pinpoints the problem
  • Enums are really powerful
  • A standardized, Unicode-aware String class
Michael Borgwardt
A: 

As I learn Java, I thought it would be useful to update this with my own comments:-

Delight:

  • Eclipse! (Compared to C++ Builder, anyway...)
  • No header files
  • No preprocessor

Annoy:

  • Checked Exceptions. Surely there's a compiler flag to disable these "errors"?
  • No RAII. RAII does much, much more than just freeing memory. My code is going to be littered with "finallys", which I loathe.

More updates as I learn and appreciate more...

Roddy