0

As a undergraduate in CS, I started with C, where pointer is an important data type. Thereafter I touched on Java, JavaScript, PHP, Python. None of them have pointer per se.

So why? Do they have some equivalents in their languages that perform like pointer, or is the functionality of pointer not important anymore?

I roughly have some idea but I'd love to hear from more experienced programmers regarding this.

Boyang
  • 2,520
  • 5
  • 31
  • 49
  • 1
    This might be a bit helpful: [How is a Java reference different from a C pointer?](http://programmers.stackexchange.com/q/141834/4978) –  Aug 15 '13 at 16:14
  • Also see [Why doesn't java have pointers?](http://stackoverflow.com/questions/8080617/why-doesnt-java-have-pointers) – Shafik Yaghmour Aug 15 '13 at 16:52

3 Answers3

5

So why?

In general, pointers are considered too dangerous, so modern languages try to avoid their direct use.

Do they have some equivalents in their languages that perform like pointer, or is the functionality of pointer not important anymore?

The functionality is VERY important. But to make them less dangerous, the pointer has been abstracted into less virulent types, such as references.

Basically, this boils down to stronger typing, and the lack of pointer arithmetic.

Jiminion
  • 5,080
  • 1
  • 31
  • 54
  • So the references in those languages are actually pointers, just without arithmatic? – Boyang Aug 15 '13 at 16:46
  • @CharlesW. Yes, if you dig down deep enough, they are just pointers. (Think about the limited way a computer can actually access data via basic operations.) But everything is heavily abstracted and layered such that you can't do any of the nasty (and fun!) stuff you can do with C pointers. C++ still has pointers, but that's mostly because it has to remain compatible with C. – Jiminion Aug 15 '13 at 16:54
1

Most object oriented language implement object as a C pointer but they are abstract and you can not change the value of it. So when you use this/self it is indeed a pointer in most of the cases (some language could have an object table for faster garbage reclamation and other optimization).

For example in Java when you do something like:

Object t = new A();
t = new B();

You are swapping pointer behind the scene. Some language have reintroduce the notion of pointer like Go but you can not do arithmetic with it.

So in a way: no pointer have not disappear from languages they just have been implemented in a better way. The way C have designe pointer is not good at all but it serves its purpose. It is not good for many reason, one is: it break invariant of you program. I do not mean to avoid it, of course they are crucial for C.

kindall
  • 178,883
  • 35
  • 278
  • 309
mathk
  • 7,973
  • 6
  • 45
  • 74
0

In Java:

  • Instead of having a pointer to a struct that you allocate with malloc, you have a reference to an instance of a class that you instantiate with "new". (In Java, you cannot allocate memory for objects on the heap directly as you can in C/C++)
  • Primitives have no pointers, BUT there are libraries built into the main library for wrapping int,double, etc. in objects (Integer, Double).
James Kingsbery
  • 7,298
  • 2
  • 38
  • 67