0

I tried various methods to get the address of a Java base variable, but I haven't found a way (i mean in the java category)

ok acturally there is a question :
the annotation said : after the method 'method' is called only print a = 100 ,b= 200,Please supplement 'method
the annotation said : after the method 'method' is called  only print a = 100 ,b= 200,Please supplement 'method'

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
  • 2
    Yes. It is really impossible to get the address of a variable. You cannot get the address of a primitive. You cannot get the address of a reference variable. What would you do with the address if you could get it? – Elliott Frisch Nov 05 '19 at 04:33
  • Java is [pass-by-value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), which means that `method` only gets passed the *values* of the variables `a` and `b` and has no access to the variables themselves. Therefore, what the question is asking is not possible. – Jesper Nov 05 '19 at 09:19

1 Answers1

1

Yes, you cannot do this.

If you want to use pointers, you have 2 options:

  • Explore the class sun.misc.Unsafe . This is a class containing unsafe operations for allocating memory etc. The following link explains how to use it, since the developers deliberately made it hard to accidentally use: https://www.baeldung.com/java-unsafe

  • Don't use Java. Consider trying C++ or Rust or something designed for this if you want to use this feature.

cameron1024
  • 9,083
  • 2
  • 16
  • 36