Java
  Home arrow Java arrow Page 3 - Syntactic Comparison of Java and C/C++
FaxWave - Free Trial.
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 
eWeek
 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Syntactic Comparison of Java and C/C++ - Pointers
    (Page 3 of 4 )

    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.

    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