Results 1 to 14 of 14
  1. #1
    tapies is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default A variable pointer

    Hi,

    could you please advise me how can I get a variable pointer (address) to see where it points to?

    Thanks a lot,
    T.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    It's not possible in Java, and it's foolish think to try. There is no point to deal with variable address in Java.

  3. #3
    tapies is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Yes, I see. I just wanted to use that to check e.g. whether two different variables pointed to the same or different memory spot.

    Thank you Eranga.
    T.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    whether two different variables pointed to the same
    Use the == operator with the two variables

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by tapies View Post
    Yes, I see. I just wanted to use that to check e.g. whether two different variables pointed to the same or different memory spot.

    Thank you Eranga.
    T.
    You mean pointed to the same memory address? Say you have two int variables as follows,

    Java Code:
    int i = 10;
    int j = 10;
    So you want to know that bot i and j use the same memory address, right?

    The answer is no. Whenever the new variable is declared it allocated a memory space as well as address.

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    It's not possible in Java, and it's foolish think to try. There is no point to deal with variable address in Java.
    More, Java was carefully designed so you could not do this. It was a bug farm in C, and the Java designers did not want to maintain the bugs.

    While you can do things like use == and .equals() to get an idea, none of them are guaranteed to give you the information. Its a secret and supposed to stay that way.

  7. #7
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    If you wanted a pointer like variable manipulation. You can created a class that makes and stores variables in a table and then you can "address" then use an index acting as an address. Lol, just trying to make a useful reply.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    However my thoughts on this is, very bad idea. Dealing with the memory address in Java is not required in any way, link in C/C++

  9. #9
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by ChazZeromus View Post
    If you wanted a pointer like variable manipulation. You can created a class that makes and stores variables in a table and then you can "address" then use an index acting as an address. Lol, just trying to make a useful reply.
    Your reply is technically correct, but a really bad idea. This is not the Java Way. If you want to write low level code, just use C.

    I would argue with your claim that it is a useful reply.

  10. #10
    tapies is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I see, that is not the Java Way.
    Thanks a lot to all of you.
    F.

  11. #11
    ChazZeromus is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    *Shrug*
    Yeah, well it's the closest thing to addresses I could think of :p

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by ChazZeromus View Post
    *Shrug*
    Yeah, well it's the closest thing to addresses I could think of :p
    The way you are talking is correct as fishtoprecords says in his last post. And also he said that it's a bad idea. In simple word the reason is, Java is a high level language, so we never worried about in memory management.

    Say you have define an array as follows in C++ and Java

    Java Code:
    int[] temp = new int[12];
    Then you are using it and do some processing. After that what are you doing? In Java, nothing. But in C++, you have to clear the memory, simply delete the array.

    Java Code:
    delete[] temp;
    I hope you are much clever than me on C++. If so I no need to talk about what happen if I forget to delete the array.

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Java Code:
    delete[] temp;
    what happen if I forget to delete the array.
    What happens is, of course, that you get a nearly impossible to find memory leak. And you not only have to remember to write the code to do the delete, but you have to make sure that its executed on every path in your code, including any and all exceptions.

    Don't forget to trap all exceptions that you never heard of, because some other developer changed code way down in the call tree in ways you never expected.

    There are real engineering reasons why Java has garbage collection: it makes the computer do what programmers and other humans do poorly.

    Back in the mid-1990s, folks had theological arguments that garbage collection was too slow. And it can be too slow for realtime work. So the solution is to not use Java for real time work.

    The theological arguments ignored the cost and slowness of finding memory leaks in complex systems.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  3. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM
  4. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 04:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •