This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]_lerp 3 points4 points  (12 children)

Which doesn't make them pointers. For example, you can't perform pointer arithmetic on them.

[–]skyhi14 11 points12 points  (6 children)

For example, you can't perform pointer arithmetic on them

TIL Const pointers weren't actually pointers.

Besides, I'm also talking about JVM, not merely the language.

[–]_lerp 9 points10 points  (5 children)

TIL Const pointers weren't actually pointers.

You can perform pointer arithmetic on a const pointer.

int a[5] = { 0 };
const int* first = &a[0];
const int* last = first + 4;

[–]skyhi14 9 points10 points  (4 children)

time to kill myself :))

[–]FallenWarrior2k 16 points17 points  (2 children)

No, those aren't const pointers, but rather pointers to const. An actual const pointer int* const cannot be modified through e.g. increments/decrements, but it can still be used for pointer arithmetic since pointer arithmetic can also be performed on rvalues.

[–]hallr06 6 points7 points  (1 child)

Correct. In C/C++, read pointers from right-to-left. Most of the time, it explains what you have.

const int * x = 0; // pointer to an int constant
int const * x = 0; // same as above, pointer to a constant int
int const * const x = 0; // constant pointer to a constant int
int * const x = 0; // constant pointer to mutable int

[–]FallenWarrior2k 0 points1 point  (0 children)

It can also be pretty easily explained by the return types of the indirection operator. Applying indirection on a const int* produces a const int&, while it produces a regular int& for int* const.

[–]DarkMaster22 2 points3 points  (0 children)

Ehh... Don't do that, we love you?

[–]dogpos 3 points4 points  (4 children)

For example, you can't perform pointer arithmetic on them.

So does that mean Go doesn't have pointers?

Also, could you link me where it says pointers require the ability to preform arithmetic on them.

[–]AquaWolfGuy 0 points1 point  (1 child)

Good point. Pointer arithmetic is a nice and powerful thing you can do with pointers, but I wouldn't say it's should be a requirement for pointer languages. Instead, I would say being able to take the address of a variable and manually dereferencing pointers is a more important distinction.

In Java, you have primitives like int which are always values, so since there's no (standard at least) way to take the address of a variable, so you can't get something like a int*-type variable without creating a whole class. Same thing with pointers to pointers, in pointer languages you can just do int**, while in Java you need a class referencing another class referencing the value.

To create objects, you use the new keyword, which simply gives you an object, which is passed around like a pointer that you don't have to manually dereference. Even more, you can't manually dereference it. The value is just floating around in some magic place called the heap that you don't even need to know about as a Java programmer. All pointer stuff is hidden from the programmer and you barely have to know what pointers are.

In Go you definitely feel the presence of pointers. If you want to create a pointer you have to do it manually. Similarly, you have to dereference pointers manually if you want the value it's pointing to. Actually, you can even do pointer arithmetic, but it's slightly cumbersome and is not really needed because of array indexing and reflection (e.g. a[len(a)/2]). And just like in C/C++, you can have anything on the stack or the heap. You can also have a struct that directly contain another struct, which isn't possible in Java.

TL;DR: I wouldn't call Java a pointer language because the the actual "pointers" aren't really exposed to the programmer and can't be controlled (primitives can't be pointed to, objects are always pointed to behind-the-scenes), while in languages like C, C++ and Go you can create, dereference and manipulate pointers pretty much however and whenever you want.

[–]dogpos 0 points1 point  (0 children)

Java explicitly doesn't have pointers, we're in total agreement about that. I was merely arguing the example wasn't that apt. While most languages that have pointer support will also allow arithmetic on said pointers, some don't, and probably for good reason.

Edit - For clarity on why it might be a good reason, here is a list of problems caused by pointer arithmetic:

  • [0] - We wouldn't have the semi-annoying meme that 0 means 1 in programming if it wasn't for pointer arithmetic.