• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pointers in Java?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy hits it.
Regarding object reference, see CampFire Story: Pass-By-Value Please.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic