Home arrow Java & J2EE arrow Page 2 - Syntactic Comparison of Java and C/C++

Data Types - Java

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.

TABLE OF CONTENTS:
  1. Syntactic Comparison of Java and C/C++
  2. Data Types
  3. Pointers
  4. Summing Up
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 14
November 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement


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.



 
 
>>> More Java & J2EE Articles          >>> More By Barzan "Tony" Antal
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: