Register for your free account! | Forgot your password?

Go Back   elitepvpers › Coders Den › Java
You last visited: Today at 06:40

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Difference between Java reference and C++ pointers

Discussion on Difference between Java reference and C++ pointers within the Java forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
Difference between Java reference and C++ pointers

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.
Jason- is offline  
Old   #2


 
Menan's Avatar
 
elite*gold: 0
The Black Market: 169/0/0
Join Date: Sep 2008
Posts: 9,484
Received Thanks: 3,109
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.
Menan is offline  
Old   #3
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
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 )
Jason- is offline  
Old   #4



 
Serraniel's Avatar
 
elite*gold: 0
The Black Market: 205/1/0
Join Date: May 2010
Posts: 6,857
Received Thanks: 5,113
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).
Serraniel is offline  
Thanks
1 User
Old   #5
 
elite*gold: 0
Join Date: Aug 2017
Posts: 6
Received Thanks: 0
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 .
Jason- is offline  
Old   #6
 
elite*gold: 0
Join Date: May 2015
Posts: 700
Received Thanks: 444
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
algernong is offline  
Thanks
1 User
Old   #7


 
Menan's Avatar
 
elite*gold: 0
The Black Market: 169/0/0
Join Date: Sep 2008
Posts: 9,484
Received Thanks: 3,109
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.
Menan is offline  
Old   #8
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 574
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
warfley is offline  
Reply


Similar Threads Similar Threads
Difference between 1.6 and 2.5?
03/11/2009 - CO2 Private Server - 7 Replies
What`s the difference between the 1.6 and the 2.5 binaries? And are there any more publicly available versions out there?
What is the difference between thanks and thanks in posts?
09/10/2007 - Conquer Online 2 - 5 Replies
as i said in the title...what is Thanks for and what is thanks in number of posts for. I think i know what thanks in number of posts does..but what is thanks for..yesterday i had 3 and today i have 4 =/.:confused:
Difference Between Luck and Chance
02/01/2007 - CO2 Guides & Templates - 69 Replies
To be honest when people always say its 4/10 chance (or what ever) and it's then argued with, "No it's luck".. I wanna say what the F**K! For all those too stupid stupid to understand, here finally, the answer!! - When you say it will happen when you are lucky, that is when the chance that it will happen actually happens! Lets say for example: "The chance of these dropping a met is 1/150" "No it's luck" ^ EXACTLY U DUMB ASS



All times are GMT +2. The time now is 06:40.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.