Nowadays millions of computer programmers want to become multi-lingual; for that reason they try to acquire understanding of multiple programming languages. Experienced coders know that it is possible to get the hang of a new language in a fairly short amount of time. Being able to compare the syntax of different languages makes learning new ones easier.
Before we move on to the actual syntactic comparison of these two languages, I must confess that due to the overwhelming reputation of ANSI C and C++ being fairly accepted as one of the most widely spread programming languages, I’m going to approach this comparison from a C-centric point of view. That is, I assume that most of my readers have a basic understanding of C and not necessarily Java.
As a result, I will sort of introduce the Java programming language while comparing it simultaneously with C/C++ and pointing out the differences and similarities in their syntax.
First, check out the next table of primitive data types that are present in Java.
Primitive Data Type
Description
byte
Signed integer on 8 bits.
short
Signed integer on 16 bits.
int
Signed integer on 32 bits.
long
Signed integer on 64 bits.
float
Floating-point on 32 bits.
double
Floating-points on 64 bits.
char
Unicode character on 16 bits.
boolean
True or False.
The first two differences that one might notice right away while skimming through the above table are the lack of ‘unsigned’ data types and the presence of the ‘boolean’ type per se. All of the primitive data types that are present in Java are signed. And the boolean type isn’t possible by default with C/C++; however, a few of the latest C++ compilers added this data type.
The rest of the data types look quite similar to those used by C/C++ but there is a significant difference in their “inner mechanism.” In Java the sizes of these data types is clearly specified and exact. This was incorporated to maintain a high level of portability for the Java programming language.
Because most of today’s machines are still running on 32 bits, Java developers had taken this into consideration. Consequently, an integer is stored on 32 bits while a long is on 64 bits. This differs from C/C++ where 16/32 bits and 32/64 bits are possible for these two, respectively. C/C++ mostly focuses on maintaining a relative comparison of the data types (a long data type is longer or equal to an int) while Java specifies their sizes exactly.
Now that we’re speaking about data types, what about conversions? The actual conversion procedure is done with a casting procedure in Java. However, Java isn’t doing any kind of automatic casting. The programmer must explicitly use casting like so: “IntVar=(int)OtherVar” (without quotation marks). In C/C++ if the conversion isn’t pointed out explicitly the compiler does it anyway; this could turn into the cause of multiple problems depending on the particular scenario.
The conditional and negation operators are working on a ‘boolean’ basis in Java. Due to this and the aforementioned non-automatic conversions, it is really important to realize that conditions like “a && b” won’t result into “a!=0 && b!=0” like they do in C/C++.
It is also worthwhile to point out that overloading operators is forbidden in Java. This was considered to be the root of certain problems and therefore wasn’t implemented. Nevertheless, I know that many C/C++ coders get frustrated at first. It’s a feature that certainly helps but we should be fine without it.