开发者

Switching to Java from C++: What are the key points? [closed]

开发者 https://www.devze.com 2022-12-24 19:31 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 7 years ago.

Improve this question

I'm an experienced developer, but most of my OO programming exp开发者_如何学Goerience 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.


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


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


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.


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.


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.


  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


The Peter Norton's Guide to Java Programming, "Making the Transition from C/C++ to Java" chapter.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号