Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Wednesday, September 13, 2023

Difference between Java and C++ Constructor - Interview Question

If you are a C++ Programmer, now learning Java then you will find a lot of similarity between two of the most popular object-oriented programming languages e.g. both support Abstraction, Encapsulation, Class, Object and other OOP concepts. But, they are some subtle differences as well, e.g. 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 new operator. This is a good interview question for programmers who knows both Java and C++.

I know, 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 candidate has. An experienced C++ programmer which has worked in Java for a couple of years should know the different on top of their head.

BTW, if you are from C++ background and looking for a good book to learn Java then check out Core Java, Volume 1 by Cay S. Horstmann. Cay has taken care to put C++ perspective of things 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 quickly.




Difference between C++ and Java Constructor

As I said, both C++ and Java support constructor but the way they are invoked is the difference. You cannot invoke Java constructor without new()operator, they are called implicitly by JVM when you use new() operator, but you can call the constructor without new operator in C++.

In fact, this is one of the most common mistake C++ programmer make while doing Java programming e.g, following will work fine in C++ but not in Java :

Course scala("Scala", 2, 300);

where 2 is duration in week and 300 is course fee in USD. In Java, you must do 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.

BTW, here are some more obvious differences between C++ and Java :
  • C++ supports pointer arithmetic but Java doesn't.
  • C++ support multiple inheritances but Java doesn't, curious why? see here
  • C++ don't have a garbage collector and memory management is developer's responsibility, Java has GC.
  • C++ is not platform independent but Java is, curious why? see here
Difference between Java and C++ constructor



One more difference between C++ and Java Constructor

There is one more significant different between C++ and Java related to constructor and destruction of the object is that C++ has both Constructor and Destructor but Java only have 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, Garbage collector reclaims the memory from an object. Garbage collector is part of JVM.


To confuse things little bit, java does provide a finalize() method, which is often mistaken by C++ developers as Destructor, which is incorrect. finalize() method doesn't reclaim memory, its not even guaranteed to be called by JVM when Garbage collector reclaims memory.

Its specification says, it may be called just before an object is a garbage collected, gives the object last chance to clean up the resource it is holding. Though it's not advised to call finalize method or do some cleanup , because it's not guaranteed, see Joshua Bloch's advise about finalize in Effective Java book as well.


The best advice regarding finalize is that, read Effective Java item 7 and then don't use it, and this comes none other than by Google itself, as shown below :

Java vs C++ constructor difference

If you depend on finalize() for freeing up system resources e.g. database connectionfile handles etc, you will most likely end up with resource leak in your program.


That's all on the difference between C++ and Java constructors. They work similarly e.g. 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 C++ background, make sure you always use new keyword with constructor in Java.

Further Reading
Java Fundamentals, Part 1 and 2
Java Programming Interview Exposed
Cracking the code interview - 189 questions and solutions


Let me know if you have worked in both Java and C++ and noticed these difference, if you have anything to add feel free to suggest. 

8 comments :

Arul said...

C++ is not platform dependent but Java is, curious why? --- Is this statement correct?

Anonymous said...

C++ is not platform dependent but Java is, curious why?
Wrong. Right should be: C++ is platform dependent, when Java is not.

javin paul said...

@Arul and @Anonymous, You guys are right, C++ is platform dependent, you need to build it separately for windows, Linux or Mac OS X, but you can run same JAR file in all those operating systems.

Actually, it was typo, I meant to say that C++ is not platform independent, but Java is :) Some how that "in" get lost.

Unknown said...

While the same binary cannot be ran on different OSs/architectures, If written well C++ code can be quite portable, only requiring a recompile... But this of course requires forethought on the part of the developer... Java will run without changes anywhere the JVM is implemented.

javin paul said...

@Christopher, on that context Java developer also needs to be careful, not to code their application in such way that makes it platform dependent. Simplest example is hard coding path separator, which is different in Linux, Windows and Mac OS X.

Anonymous said...

Java is much closer to C# than C++, comparing with C++ is old story.

Unknown said...

Java is not platform independent but C++ is Platform dependent..
B'coz Java Compiler generates which is Independent...!!!
And C++ Directly Generate OBJ Code which is machine Dependent...!!

Its my Request to Them Plzz.. correct it..!!
Thank you

javin paul said...

@Tapshware, you are getting confused with words, you wrote "Java is not platform independent"
but I know what you mean. To make it clear,

Java is platform independent because it generates bytecode, which is executed by JVM and not the machine itself.
C++ is platform dependent because it generates native code which is executed directly by machine.

Post a Comment