Java
  Home arrow Java arrow Page 2 - Syntactic Comparison of Java and C/C++
CIO Insight
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Syntactic Comparison of Java and C/C++
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-11-21

    Table of Contents:
  • Syntactic Comparison of Java and C/C++
  • Data Types
  • Pointers
  • Summing Up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Syntactic Comparison of Java and C/C++ - Data Types
    (Page 2 of 4 )


    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 Articles
    More By Barzan "Tony" Antal


       · Thanks for taking time to read the first part of this 2-part series. Don't...
       · it was a nice discussion it clearly differentiate the nature of the most powerfull...
       · Thanks for your input regarding my article. I appreciate your willingless to leave a...
       · Specifically, it should be going live tomorrow morning. :)
     

       

    JAVA ARTICLES

    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup
    - Using RPC-Style Web Services with J2EE
    - Integrating XML with J2EE
    - Taming Tiger: Concurrent Collections
    - Combating the ‘Object Crisis’

    Iron Speed



    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway