Java
  Home arrow Java arrow Page 3 - Syntactic Comparison of Java and C/C++
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
JAVA

Syntactic Comparison of Java and C/C++
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

     
     
    ADVERTISEMENT


    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
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek