3 ms·
> They actually don't have pointers. They do. Everywhere. Every Java variable of Object type is a pointer. (Also, they're all nullable....ugh.) If Java had r
by paulddraper 10y ago
> They actually don't have pointers.
They do. Everywhere.
Every Java variable of Object type is a pointer. (Also, they're all nullable....ugh.)
If Java had real references (see original comment by jasode) you'd be able to write something like C#'s int.TryParse ( https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).... ).
- coldtea 10y ago>Every Java variable of Object type is a pointer. No, it's a reference. It just has a pointer underneath. >If Java had real references (see original comment by jasode) you'd be able to write something like C#'s int.TryParse If Java had one specific type of references. The common use of the term though is still what Java has.
- paulddraper 10y ago> The common use of the term though is still what Java has. The way you are defining "reference" makes it immaterially different from "pointer". Can a pointer be null? Yes, in C. Can a "reference" be null? Yes, in Java. Can a pointer be used in arithmetic operations? Yes, in C. Can a pointer not be used in arithmetic operations? Yes, in Go. If this is really the common definition, it isn't a useful one.
- coldtea 10y agoWhether a thing can be null or not is orthogonal to whether it is a pointer or a reference. The main difference in this definition is on whether you need to dereference to access the value pointed at (like you do in C) or not.
- paulddraper 10y agoNot sure I understand you. A concrete PHP example: function noop($a, $b) { $tmp = $a; $a = $b; $b = $tmp; } function swap(&$c, &$d) { $tmp = $c; $c = $d; $d = $tmp; } $first = [1, 2]; $second = [3, 4]; noop($first, $second); swap($first, $second); $a and $b work much differently than $c and $d. I would call the first two pointers (to [1,2] and [3,4]), and the latter two references (to $first and $second). What would you call them? (Note: the latter function is impossible to write in Java.)
- coldtea 10y ago>I would call the first two pointers (to [1,2] and [3,4]), and the latter two references (to $first and $second). What would you call them? In the first case, $a and $b pass the arrays by copying their values. The second example uses references. Java can't do the change visible externally because it passes its references by value -- but that's not a necessity for all languages with references. As the PHP manual itself says: "References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on".