aspose file tools*
The moose likes Beginning Java and the fly likes Pointers in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pointers in Java?" Watch "Pointers in Java?" New topic
Author

Pointers in Java?

Anil Jain
Ranch Hand

Joined: Mar 10, 2002
Posts: 51
Hello Friends!!
Can someone tell me if we have pointers concept in Java like c/c++. Any way of knowing the memory address of variables etc..
Thanks,


Anil Jain<br />Sun Certified Java(2) Programmer
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well a reference in a variable is the address of an object, but you can not manipulate the address to come up with a different address, like you can in c++. You are not allowed to do calculations on references in java.


"JavaRanch, where the deer and the Certified play" - David O'Meara
sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
Hi Amit
When you write code like......
User firstTimeBuyer=new User()
the firstTimeBuyer is pointing to the User object just created.So,yes JVM does resolve it to a pointer.And calling the hashCode() method returns a unique integer for this object which I think is the memory address.
Hope this helps.
Regards
sanj
Anil Jain
Ranch Hand

Joined: Mar 10, 2002
Posts: 51
If one cannot get the memory location of variables in Java then isn't the language lacking something very seriously fundamentally??
Thanks!!
sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
Hi Amit
Your variables point to the memory locations(objects) but you can't manipulate the memory locations as such.
Regards
sanj
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Amit Daga:
If one cannot get the memory location of variables in Java then isn't the language lacking something very seriously fundamentally??
Thanks!!


Yes - it is missing one of the things that causes the most bugs in c++. :roll:
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
If one cannot get the memory location of variables in Java then isn't the language lacking something very seriously fundamentally??

No, infact this is helpful for the programmers as this reduces the errors that are typically encountered in C++ due to improper memory management.
The JRE itself manages the memory allocation and for garbage collection.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Originally posted by Amit Daga:
If one cannot get the memory location of variables in Java then isn't the language lacking something very seriously fundamentally??
Thanks!!

If you think about it, the exact location in memory of a variable is not genuinely useful unless you're writing some sort of RAM dumper or analyzer. If you print it to the console, it means nothing. Pointers come from the days of assembler and it's big brother C -- when the contents of the stack really were your buisness.
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Cindy hits it.
Regarding object reference, see CampFire Story: Pass-By-Value Please.


Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by sanj singh:
[...] calling the hashCode() method returns a unique integer for this object which I think is the memory address.

From the API docu:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pointers in Java?