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

Pointers - 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

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 & 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: