4 ms·
And Java just added to the problem. For PR reasons, they didn't want to have "pointers" (arithmetic, etc. ew). So Java has "references". But what happens when
by paulddraper 10y ago
And Java just added to the problem.
For PR reasons, they didn't want to have "pointers" (arithmetic, etc. ew). So Java has "references".
But what happens when you call a method on a null reference? Not NullReferenceException, but NullPointerException.
- coldtea 10y ago>For PR reasons, they didn't want to have "pointers" (arithmetic, etc. ew). So Java has "references" Well, it's just a rename for PR reasons. They actually don't have pointers. >But what happens when you call a method on a null reference? Not NullReferenceException, but NullPointerException. That's because it's the underlying pointer access that causes the exception. Doesn't mean the reference is itself a pointer -- it just abstracts over one.
- 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".
- wyuenho 10y agoThey also insists Java is pass-by-value whereas technically it's pass-by-value for primitive unboxed types and call-by-sharing for others. The reason of which escapes me.
- twic 10y ago"Call-by-sharing" is just a different, and IMHO unnecessary and unhelpful, name for pass-by-value when the values happen to be pointers. To put it another way, the inconsistency in Java isn't that primitives and objects are passed differently, it's that they're stored differently. Given how they're stored, they're passed the same way.
- paulddraper 10y agoSince primitives are immutable have value-equality, it doesn't matter. If your cognitive model prefers to think of everything as pass pointer by value, you think that.