What's the Difference Between Java and C++ Constructors?
Want to learn more about the difference between Java and C++ constructors? Check out this post to better differentiate between the two and how these methods are called.
Join the DZone community and get the full member experience.Join For Free
If you are a C++ programmer who is now learning Java, you will find a lot of similarities between these two popular object-oriented programming languages. Both of these languages support abstraction, encapsulation, class, object, and other OOP concepts. But, there are some subtle differences as well. Both Java and C++ have a constructor, and they work the same way in Java as they do in C++. But, the way they are called is different.
For example, in Java, a constructor must be called by using the
new() operator. There is no other way to explicitly call the constructor while creating an object, but in C++, you can do it without a new operator. This is also a good interview question for programmers who know both Java and C++.
Now, switching context between C++ and Java is not easy, especially during the interview, but that's also a perfect way to test how much experience the candidate has. An experienced C++ programmer that has worked in Java for a couple of years should know the difference of the top of their head.
If you are from a C++ background and looking for a good book to learn Java, then check out Core Java, Volume 1 by Cay S. Horstmann. Horstmann puts C++ into perspective when introducing new concepts in Java. So, you will find a comparison of Generics in Java with templates in C++, which helps to understand the concept rather quickly.
Difference Between C++ and Java Constructor
As I said, both C++ and Java support the constructor, but the way they are invoked is different. You cannot invoke a Java constructor without the
new() operator. They are called implicitly by the JVM when you use the
new() operator, but you can call the constructor without the new operator in C++.
In fact, this is one of the most common mistakes C++ programmers make while doing Java programming; the following will work fine in C++ but not in Java :
Course scala("Scala", 2, 300);
This is where 2 is the duration in a week and 300 is the course fee in USD.
In Java, it should look like:
Course scala = new Course("Scala", 2, 300);
Also, Java objects are always constructed in the heap, even if you create it inside a method or block.
Here are some more obvious differences between C++ and Java:
- C++ supports pointer arithmetic, but Java doesn't.
- C++ supports multiple inheritances, but Java doesn't. Are you curious about why? See here.
- C++ doesn't have a garbage collector, and memory management is the developer's responsibility; Java has GC.
- C++ is not platform independent, but Java is. Are you curious as to why? Click here for more!
C++ vs. Java Constructor
There is one more significant difference between C++ and Java in relation to the constructor. And, the destruction of the object is that C++ has both constructor and destructor, but Java only has a constructor.
There is no destructor in Java. Once an object becomes eligible for garbage collection, i.e. once its job is done and there is no live reference pointing to it, the garbage collector reclaims the memory from an object. The garbage collector is part of the JVM.
To confuse things a little bit, Java does provide a
finalize() method, which is often mistaken by C++ developers as the destructor, which is incorrect. finalize() method doesn't reclaim memory; it's not even guaranteed to be called by the JVM when the Garbage collector reclaims memory.
Its specification says that it may be called just before an object is garbage collected and gives the object one last chance to clean up the resource it is holding. Though it's not advised to call the finalize method or to do some cleanup, it's not guaranteed. To learn more, check out Joshua Bloch's advice about finalizing in his book Effective Java.
In addition to information on using finalize, the following was found by Google itself :
That's all on the difference between C++ and Java constructors. They work similarly and both are used to create an object and initialize them, but the slight difference comes in how they can be called. You can invoke a constructor in C++ without using the new keyword, which is not possible in Java. So, if you are coming into Java from a C++ background, make sure you always use a new keyword with the constructor in Java.
Thanks for reading this article so far. If you like this article, then please share with your friends and colleagues. If you have any questions or feedback, please drop a comment.
All the best with your Java learning!
Opinions expressed by DZone contributors are their own.