Completing the Syntactic Comparison of Java and C/C++
This is the second half of the two-part series on the syntactic comparison of Java and C/C++. Before we begin, I’d like to encourage you to read its first part if by any chance you’ve missed it. It is called “Syntactic Comparison of Java and C/C++” and it’s published right here. You shouldn’t miss it because grasping the basics is crucial.
In the first part I showed you the main differences as far as data types, classes, and some additional important details on the semantics of those two languages. Now we’re going to discuss more thoroughly the topics of classes, command line arguments, unconditional transfers of control (goto, labels, break, continue), and so forth.
Moreover, before finishing this article I’ll point out the features that are present in C/C++ but missing from Java and vice versa. Furthermore, you might be expecting some kind of conclusion or final words at the end. I definitely won’t conclude that one is better than the other but I’m determined to give you my personal opinions regarding these two languages.
Now the time has come for us to begin. Ready, Steady, Go!
I’ve mentioned in my previous article that a class is one of the most important concepts in Java. That’s because everything must take part of a class. This wasn’t fashionable with C/C++. Even though classes have critical importance in C/C++ too, they aren’t necessary. In Java they are required and everything happens around them.
Should you desire to create global functions and use global variables, you are going to encapsulate everything inside a class. You do this by creating static methods and static data that ultimately are placed inside a class. Of course, encapsulation into multiple classes is possible too. Additionally, it is worth mentioning that there is a huge family of classes that has a so-called superclass (ancestor), the Object class.
Java also has a long list of access modifiers that can be implemented to classes, constructors, methods, etc. Each of these has their very own rules according to which they can be implemented. For example, out of the following 3 modifiers only one should be used when declaring a method: private, public, protected; or methods that have modified as abstract or native should have no body.
A final note on access modifiers is that they do not interfere with inheritance by prevention. On the other hand, they do affect which parts you can or cannot see within a subclass. Speaking of inheritance, another significant difference is that multiple inheritances aren’t allowed in Java. But there’s a so-called interface structure that is not a class but can inherit from a base class (superclass).
However, there is a workaround by implementing multiple interfaces. An interface is a class that contains nothing but abstract methods. Hence, their body must be defined in the inherited classes.