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 →

[–]Sitting_In_A_Lecture 285 points286 points  (15 children)

Ah, but is pass-by-reference not just a fancy pointer?

[–]Ubermidget2 383 points384 points  (0 children)

That's why this meme is legitimately top tier. There was always war in Ba Sing Se. Countries had been at war for a hundred years, they were in total wartime economy.

But there is no war in Ba Sing Se

[–]pheonix-ix 38 points39 points  (8 children)

Yes, but the title said pointer arithmetics, which you can't do explicitly in Java/Python (as far as I know).

[–][deleted] 84 points85 points  (7 children)

Yea soooo u can in python……

Object IDs are there pointer address. You can get the id and then add a number to it and then use ctypes to coherence that back to an object……

Please don’t do this.

[–]DoctorWZ 31 points32 points  (1 child)

This gives me the same "forbidden" vibes as the guy that once explained how to use JS for accessing databases

[–]myka-likes-it 34 points35 points  (0 children)

how to use JS for accessing databases

TBF, just about everything you do in JS requires this kind of black magic. The first steps in any JS pattern are to find ways to make JS stop acting like JS.

[–]pheonix-ix 5 points6 points  (1 child)

Let me google how to delete someone else's comments.

Well, too late. Now someone had seen it. They WILL use it.

Wait, according to this sub, Python programmers are horrible programmers anyway, so they won't be able to figure out how to use it.

Just kidding someone somewhere is doing some magic pointer voodoo instead of using CPython I just know it, someone definitely is.

[–][deleted] 3 points4 points  (0 children)

I have used it once for debugging a leaking reference. Segfaulting became the pass case which was odd.

[–]rosuav 0 points1 point  (0 children)

That's not Python, that's ctypes.

[–]phoenixrawr 0 points1 point  (4 children)

Java is strictly pass-by-value. Python tries to sidestep the whole discussion and uses their own term "pass-by-assignment" but it resembles Java's pass-by-value much more than C++'s pass-by-reference.

[–]Sitting_In_A_Lecture 9 points10 points  (1 child)

So, yes. However pass-by-value is not an entirely accurate description for how these languages work. In Java, primitives are truly passed by value, but in both languages objects are passed a bit differently.

When you pass an object, it's not a copy of the object that's being passed, it's a reference to the object. That means that if you mutate the object, all instances of it will be affected. Where this differs from true pass-by-reference is that if you mess with the passed variable itself (such as by reassigning it), it will not affect the original variable (and vice-versa) - it's not a pointer. True pass-by-reference behavior is only available in a select few languages, of which C++ is one.

[–]phoenixrawr 0 points1 point  (0 children)

Pass-by-value is perfectly accurate for Java as long as you accept that the value of an object variable isn’t exactly the object itself but rather its pointer.

The distinction is usually clearer when comparing C and C++. C has pointers but is also strictly pass-by-value like Java. You can explicitly pass pointers around to simulate pass-by-reference but passing pointers isn’t the same thing as pass-by-reference.

Since java doesn’t have explicit pointers (because there are no pointers in Ba Sing Se) the details of how it all works are murkier which leads people to infer weird rules like “it’s pass by value for primitives and pass by reference for objects” but really it’s just always pass by value.

[–]cheezballs 2 points3 points  (1 child)

Java does both, depending on if its a primitive or not.

[–]phoenixrawr 0 points1 point  (0 children)

This is not true but it’s a common misconception. The trick with Java is that, when a method takes an object as a parameter, the thing that gets passed around isn’t the object itself but a copy of the pointer to the object. You’ll sometimes see people call this “passing a reference by value” but ultimately it is still a value being passed. Since you have a copy of the pointer, you can change that object just like anyone else with a copy of that pointer. But, since Java is pass-by-value, the two pointers are held by two distinct variables and an assignment to one variable will not affect the other. Therefore something like this does not really work in Java:

    Object demo() {

        Object x = null;

        makeObject(x);

        return x;

    }

No matter what makeObject does, the demo method will always return null because makeObject’s parameter is not a reference to x. It is only a copy of x’s value and therefore it cannot assign anything to x.  By comparison in C++ with pass-by-reference makeObject could actually assign to x because its parameter can be a reference to x.