-2

Java doesn't support pointers; are there any alternatives?

Note that I'm not referring to function pointers, but to pointers!

Ry-
  • 218,210
  • 55
  • 464
  • 476
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 2
    For what do you need pointers in Java? – micha Dec 23 '12 at 15:38
  • 1
    Why do you need them? Context, please! – Ry- Dec 23 '12 at 15:38
  • 9
    What *exactly* are you trying to achieve? In many ways you can view references as being similar to pointers - but we don't know whether that will be sufficient without knowing what you're trying to do. – Jon Skeet Dec 23 '12 at 15:39
  • I swear if Gosling had simply made up a new word rather than using "reference" the internet would be a better place. – Brian Roach Dec 23 '12 at 15:48
  • 3
    @BrianRoach: I've always wondered why they didn't call them pointers. That's what they are. I guess it was just a marketing choice to make it clear that pointer *arithmetics* and direct memory access were impossible. BTW, Java has a NullPointerException. – JB Nizet Dec 23 '12 at 15:51
  • @JBNizet - Agreed. They didn't want to use the scary "pointer" word. And thus two decades of "No, it's pass by value and that isn't a reference" has ensued. – Brian Roach Dec 23 '12 at 15:55

5 Answers5

8

Well, pointers exist in C++ to allow a level of indirection when passing objects around so that an object can be modified or accessed in different areas of code. This is what happens by default in Java, but it approaches it in a different way. In Java, everything you have is a reference. The name you use to refer to an object is a reference to that object. When you call a function, that reference is copied into the function (passed by value). You can think of the variable names in Java as being similar to (or behaving like) pointers in C++.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

Java does not have pointers. It has references almost exactly as C++ has.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 5
    Well, if you think about it, Java references are closer to C++ pointers than to C++ references. – Luc Touraille Dec 23 '12 at 15:39
  • 3
    Except that ... they aren't actually references and don't act like references at all. They're pointers except you can't do anything but dereference them. – Brian Roach Dec 23 '12 at 15:41
  • @BrianRoach: There is one other thing you can do with Java references but not with C++ references: You can change the "target" i.e. you can assign to the reference. – A.H. Dec 23 '12 at 15:48
  • @A.H.- What I mean is, you don't have access to the raw address so can't operate on it (pointer arithmetic) – Brian Roach Dec 23 '12 at 15:51
0

Can't say alternative, but you can use references.

Edit:

More precisely, all Java invocations are pass-by-value. However, when you pass an object you are passing a reference by value. An ordinary Java parameter is more similar to a C++ reference than to C++ pass-by-value or pass-by-pointer.

Reference : Is Java “pass-by-reference”?

Community
  • 1
  • 1
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

There are no pointers in Java. However Java uses references nearly everywhere which makes pointers close to obsolete.

micha
  • 47,774
  • 16
  • 73
  • 80
0

Java references are very close to pointers.

Object x;

of java is close to a

void *x;

in C, or C++;

AlexWien
  • 28,470
  • 6
  • 53
  • 83