Difference between Java reference and C++ pointers

02/13/2018 06:02 Jason-#1
When we create an object in java : ( Say that we have a class called A )
Code:
A obj = new A();
And when we create an object in c++ :
Code:
A *ptr = new A();
In java it's called reference to access the object but in C++ it's called pointers
i know pointers as well but my question does reference is the same for pointers ? since both of them are allocated on the stack and holds the address of the object so i just want to know if reference of java differ from the pointers of c++.
Thank you.
02/13/2018 11:27 Menan#2
Actually there are no real pointers in java. In general you hold a reference to an object in your attribute/variable. When you call a method with this object you always give the reference to your object to the method. It's not like in C++ where you either can give a reference to a function or make a local clone of the variable.

Code:
//C++ for creating a local reference
void functionName(object a);
//C++ for using the reference
void functionName(object& a);
In java normal function calls are always like reference calls in c++. You only get a copy of an object, when you manually instantiate another object and copy the values. So you always work on the object you pass to the function, except you manually copy them.

Hope it got more clearer.
02/13/2018 20:26 Jason-#3
Well bro you actually didn't get my question :
I asked what's the difference between the reference in java and the c++ pointers.
I asked that since both of them are allocated on the stack and holds the address of memory locations of the object. For ex:
Code:
C++:
A *ptr = new A();
Java :
A ptr = new A();
both of ptr are allocated on the stack and the value of ptr is the address of the memory location of the object but in c++ it's called pointers and in java it's called reference so my question is does java reference is the same of c++ pointers ? ( i know that java doesn't support pointers )
02/13/2018 21:05 Serraniel#4
A Java reference only is defined as a possibility to access an object in Java. A reference technically is implemented as a pointer usually, but it isnīt required to be. Additionally, as far as I know, you donīt have the possibility to use pointer arithmetics with references in Java.
So all in all pointers are more powerful than a reference in Java is (maybe more dangerous for new people as well).
02/13/2018 21:39 Jason-#5
Quote:
Originally Posted by Serraniel View Post
A reference technically is implemented as a pointer usually
Well this was the answer that i was looking for.
But my last question do you mean by :

Quote:
Originally Posted by Serraniel View Post
A as far as I know, you donīt have the possibility to use pointer arithmetics with references in Java.
You mean by saying the pointer arithmetics the pointer symbol ( * ) ?
or you mean the usage of c++ pointers that we can create pointer that points to null and we can play with it etc .
02/13/2018 22:52 algernong#6
You can change the value of a pointer by adding or subtracting an integer, i.e.

Code:
int i = 42; 
std::cout << "&i = " << &i << std::endl;
std::cout << "(&i) + 1 = " << (&i) + 1 << std::endl;
Adding "+1" to a pointer moves that pointer by (size of the type) bytes, i.e. by one byte if it's a char pointer, by 4 bytes if it's an int pointer, by 8 bytes if it's a pointer to a struct containing two integers and so on.
Doing that is called pointer arithmetic and you can't to that with Java:
Code:
// C++
Obj *obj = new Obj();
obj + 1; // valid expression, points to sizeof(Obj) bytes after obj

// Java
Object obj = new Object();
obj + 1; // compiler error
02/14/2018 18:23 Menan#7
As addition i want to mention, since the JVM (Java Virtual Machine) manages the memory allocation for you you cant access the pointer (address in memory) of a variable in java. You can with Reflection Tools, modifying your jvm at runtime, but you shouldnt do that. Also i generally think there is no need for it in java, since you always will be above the jvm layer.
02/15/2018 17:44 warfley#8
Quote:
Originally Posted by algernong View Post
You can change the value of a pointer by adding or subtracting an integer, i.e.

Adding "+1" to a pointer moves that pointer by (size of the type) bytes, i.e. by one byte if it's a char pointer, by 4 bytes if it's an int pointer, by 8 bytes if it's a pointer to a struct containing two integers and so on.
Doing that is called pointer arithmetic and you can't to that with Java:
What has to be mentioned with this answer, this is because in C arrays are just pointer arithmetics.
Code:
int arr = new int[5];
// all these are the same
arr[3] = 5;
3[arr] = 5;
*(arr+3) = 5
In java only the array notation arr[3] is possible due to the lack of pointer arithmetics