Jan 26 2012

Java stuff that you should know:What are the differences between C++ and Java?

Category: English posts,Miscellaneous,TechnicalIuliana @ 16:48

Java vs. C++ Because somebody brought up this subject here I decided that this is the question I should answer next. I am a developer from the old generation,at least this is how we call it here in Romania. Turbo Pascal was my first, (red cheeks :D) then I learned C, C++, Php, Java and .Net. With Java and C++ I have had the longest and most stable relationships. So I think I am able to answer this one, though I have to mention that nowadays you could be asked to compare Java to other languages too.

Again this can considered to be a difficult question mostly because there are a lot of differences between these two languages. That’s why you want to start your answer with the differences that remove any doubt that you know both languages pretty well, and usually this means getting technical. This is the how I would answer this question:

  • Java is multi-threaded, C++ is not
  • In Java primitive and reference data types are always passed by value, C++ uses also references and pointers.
  • Java has no pointers, C++ does
  • Java has implicit memory management through garbage collector, in C++ memory management is explicit
  • Java does not allow operator overloading,  C++ does
  • Java does not need destructors, C++ does
  • Java does not have type struct and union, C++ does
  • In Java, types cannot be overridden, in C++ we can do that.
  • Java does not provide multiple inheritance using classes (or virtual inheritance), C++ does
  • In Java, arrays have fixed sizes and attempt to read from an index bigger that size -1, will cause an exception. In C++ attempt to read from an index bigger that size-1 might even succeed and the value returned is the current value stored int that memory block.
  • Java programming model is totally compatible with OOP programming. C++ also supports functional programming, procedural and template programming.
  • Java is a WORA/WORE – write once run anywhere/everywhere (any compatible JRE ) , C++ is a WOCA – write once compile anywhere
  • Java has built-in support for comment documentation
  • Java has no native support for unsigned arithmetic, C++ does. (Has anybody used that in C++, ever?)
  • In Java there is no need for scope resolution operator (::)

Of course there’s more, but this should be enough at an interview. Fell free to add anything you might consider important. As always I am eager to learn new stuff.

Tags: ,