Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pointers in Java?

 
Anil Jain
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy hits it.
Regarding object reference, see CampFire Story: Pass-By-Value Please.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic