Syntactic Comparison of Java and C/C++

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.

Throughout this two-part series we’ll endeavor into comparing the syntax of two of the most popular programming languages while heavily pointing out the similarities and differences. Needless to say, I won’t claim that one is better than the other. The main focus should be on learning both up to an intermediate level from which we can grow later on.

In the upcoming sections of this article and the next you will also find a discussion of particular features that are present in one but not in the other. All in all, the target audience is everybody that is at least a little curious about picking up either JAVA or C/C++, and preferably has a bit of experience in one.

Therefore, skilled enthusiastic coders with vast experience in both languages aren’t our focus at this time. This series of articles wasn’t written for the pros; however, they might as well refresh their memory with the help of my comparison. Furthermore, if you are a novice in the field of computer programming just keep on reading, studying, and practicing; don’t let the enormous amount of information overwhelm you.

Let’s begin.

{mospagebreak title=Data Types}

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



Signed integer on 8 bits.


Signed integer on 16 bits.


Signed integer on 32 bits.


Signed integer on 64 bits.


Floating-point on 32 bits.


Floating-points on 64 bits.


Unicode character on 16 bits.


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.

{mospagebreak title=Pointers}

Pointers grant a lot of flexibility to software engineers in C/C++. There are various operators that allow transferring of variables like: “->, *, &, .”. However, you cannot do this in Java. Java ultimately does not have raw pointers; at least not like C or C++ has direct hardware address pointers.

Pointers are replaced by references. You can think of a reference as a pointer that won’t cause potential danger anymore because references are indeed very different from C/C++ pointers; they aren’t true raw pointers either. These references lack the so-called “pointer arithmetic” feature and many others. It’s also worth noting that pointers can only be used for objects and not variables per se.

Moreover, you can construct a new reference only with the help of new. Pointers truly are a source of various problems; many of these are random values due to failure of initialization, mismatching unions and/or types, or pointing to a wrong place because pointers are able to point into the middle of an object, too. These aren’t possible with references. References are powerful, but also safer.

Now you might ask “How am I supposed to return values?” In short, you cannot return values as parameters because the change occurs only within the function; hence the newly modified value won’t affect our value that is outside of the function. In C/C++ you could return using the well-known pass-by-reference/address technique.

You can opt for the traditional C-like pass-by-reference/address technique. You can do that by creating a new class that would contain the variable, then an object of this class would be passed via address. This is the easiest solution for returning more value(s).

Java is strictly pass-by-value. This means parameters to a function are passed by value. The parameter(s) is/are evaluated and then their resulting value is copied to a specific place in memory that’s temporary and holds the value until the function lasts.

In Java the actual process of fully copying objects (including address and value) is done with the function called clone(); assuming that the class is cloneable. The same is true for arrays. To copy each element of an array you either do it one-by-one or use the function System.arraycopy(). In C/C++ you could just assign one of the objects or arrays to another and the job is done.

Another important objects-related difference is that the == operator does not check the true value-equality because it only checks the equality of references (are they referring to the same object?). Should you need to check the full equality of two objects (value + address) you’d opt for equals(). Example: “if (obj1.equals(obj2))”.

We all know that in C++ the NULL is a constant that is predefined to be 0 or 0L. In C it is defined as ((void *)0). They essentially meant the same thing. However, in Java the null is a reserved name, which means that an object or array does not refer to anything at all — thus, it is a void reference. In fact, it refers to nothing. But it’s not 0.

{mospagebreak title=Summing Up}

Now is the perfect time to sum up what we’ve just learned. First of all, Java as a programming language was consciously simplified and the most common sources of trouble were eliminated, either completely or by incorporating specific safer workarounds.

You can add comments the same way in both languages, either the // per line or the /* */ for multiple lines. These are called implementation-comments and they’re derived from C/C++. However, Java has document-comments too. You can delimit these using /** */ tags. Additionally, these can be exported to HTML using javadoc.

Using operators = and == in Java considers only the references. If the coder wants to either copy or test the equality of two objects/arrays s/he should use clone() and equals(), respectively. A null in Java is not akin to 0 per se, it stands for a void reference.

C-like pointers do not exist in Java. They are created as references and de-referenced automatically. Some might argue that Java has some kind of pointers; well it has, but they are not true pointers from a C/C++ point of view — there is no pointer arithmetic at all.

The idea of restrict programmers from directly manipulating addresses of the memory was mostly carried out to enhance the security of the language. Yes, one of the reasons is to provide more of a type-safe coding. But the most important underlying reason is that it gives less control over the system’s resources to programmers. Thus it drastically decreases the opportunities to code malicious programs (e.g., buffer and/or stack overflow, etc.).

The last syntactic difference between Java and C I want to point out before I finish this half of the two-part series is about global data and variables. In Java there are no structs or unions. Everything is part of a class. Static methods behave like globals inside a class.

Until next time, keep coding! Stick around; you won’t want to miss the next part.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye