Pointers in C++ vs References in Java

INTRODUCTION

The objective of this small article is to give an introduction about the pointers subject considering the C/C++ programming language, give an introduction how it is implemented with the Java programming language and a conclusion of why pointers are not implemented in Java as it is in c++.

THE COMPUTER MEMORY

According to Hubbard, 2000, the computer memory can be imagined as an object formed by cells, which can be compared to a large array of bytes. A computer with 256mb of RAM (random access memory) for instance, may be represented as the image below:

memory1

The image on the left illustrate a computer memory starting from 0 to 268,435,455, which is commonly represented as 0x00000000 to 0x0fffffff.

Considering the declaration of the variable ‘n’ as:

int n;

And supposing it is stored at the address of 0x0064fdf0, this can be illustrated as:

memory2

The variable will occupy 4 bytes of memory, considering 0x0064fdf0, 0x0064fdf1, 0x0064fdf2, and 0x0064fdf3 (considering in C++ int types occupies 4 bytes of memory).

Considering the variable n = 44, the representation looks like:

memory3

According to Prinz & Crawford, 2006, “a pointer is a reference to a data object or a function. Pointers have many uses: defining “call-by-reference” functions, and implementing dynamic data structures such as chained lists and trees, to name just two examples.”

Basically in C++ you can declare a pointer using the following syntax:

int *iPointer; // this line declares iPointer as a pointer to int

In declarations, the asterisk (*) means “pointer to.” (Prinz & Crawford, 2006).

What happens with a pointer, is that you may reference the same area of the memory to the address, and so you are able to use the same area of the memory with a different variable, or even passing the reference, which will simply means you are working with the same area of the memory.

If you want a pointer to point to another area of the memory, you just need to use the &, which means “reference to”

iPointer = &iVariable;

The code above give the instruction of the variable iPointer to reference the area of memory of &iVariable.

POINTERS IN JAVA

Java is intended for writing programs that must be reliable in a variety of ways. According to Horstmann and Cornell, 2008, “Java puts a lot of emphasis on early checking for possible problems, later dynamic (runtime) checking, and eliminating situations that are error-prone”. One of the big differences between Java and C++ is that Java has a pointer model that eliminates the possibility of overwriting memory and corrupting data, taking off the power of the programmer in making mistakes what results in a more security and efficient programming language.

That means you cannot explicitly point to another area of the memory in Java, on the other hand there are ways which you can reference objects in Java, taking advantage of the same instance in memory, however by using the language standards.

REFERENCES IN JAVA

Object instances in Java are stored in the memory of the Java Virtual Machine. For instance, when declaring the variable car below and making a new instance of the object, as seen below, a new reference in the memory will be allocated to store this object.

Car car = new Car();

In the case, of referencing the same object, as below:

Car car2 = car;

The reference to the car will be shared, and so, both car and car2 will point to the same area in the memory. This is the basically the way the Java programming language implements referencing.

CONCLUSION

Pointers are a powerful resource programmers may use to take advantage of many resources, however, the responsibility of dealing with the computer memory directly is not a resource a language which looks to be simple and security may implement. In my point of view, for languages such as C and C++, pointers and references are trivial. However, considering the kind of application Java looks to provide and implement, which are mostly Enterprise and final user applications, the resource of having direct access and control over the memory is not crucial and may lead to more secure and stable applications.

REFERENCES

Hubbard, J. R., 2000. Programming with C++. 2nd ed. USA: McGraw-Hill.

Prinz & Crawford, 2006. C in a Nutshell. 1st ed. California, USA: O’Reilly Media.

Horstmann & Cornell, 2008. Core Java – Volume 1 fundamentals. 8th ed. California, USA: Sun Microsystems Press.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s