Passed by Value or Reference? (Java, C++ & Python) | Sololearn: Learn to code for FREE!

+3

Passed by Value or Reference? (Java, C++ & Python)

Please Correct me if I'm wrong: - Python Objects are passed by reference (lists, dictionaries, class instances etc), and primitives by Value. - Java Objects are passed by Value, being copied to a new Object... (Java Object = everything except of primitive datatypes.. and they are also passed by Value) - You control whether your C++ Object/primitive will be passed by Value or by Reference.

9/28/2021 8:29:37 AM

Yahel

25 Answers

New Answer

+7

Mehran, what you get when you call the toString() method on a Java object will depend on if it has been overridden or not. The default behaviour of toString() is to print the class name, followed by @, then an unsigned hexadecimal representation of the hash code of the object. Object o = new Object(); System.out.println(o); // [email protected] Integer i = 42; System.out.println(i); // 42 o = i; System.out.println(o); // 42 Note that I did not explicitly have to call the toString() method; it is invoked automatically. Typing it in explicitly gives the same results. The problem of not defining words is that we need words to explain things to each other. No matter what you call it, a reference, a pointer, or a hash code the fact remains that passing an object to a method means that we can alter the original data from within the method, which is effectively passing a reference. As Zemiak has pointed out, if you are in a Job interview just say it passes by value but when programming remember that it is effectively passing by reference and that any changes that you make to the object inside the method are changing the original object. Which is pass by reference or by pointer in other languages.

+6

While technically Java is pass by value it is deemed to be "intuitively pass by reference". Objects are not copied to methods. Objects are known as reference data types and it is the reference that is copied, not the data itself. Effectively making it pass by reference. If it were pass by value altering a copy of an object within a method would have no effect on the version outside of the method unless we returned the object from the method and copied it back to the original object. This clearly is not the case. Personally I think Java is just playing silly buggers with semantics saying that passing a copy of the reference value means pass by value. https://www.cs.virginia.edu/~jh2jf/courses/cs2110/java-pass-by-value.html

+4

Even in C/C++ pointers can be passed by value, effectively passing the objects that they point to by reference. C++ also has the concept of a reference to an object whereby you can avoid the explicit use of pointers.

+3

Martin Taylor "There aren't many of us old dinosaur left." I'm glad that you are still alive and helping noob programmers like me. 😥😓 I'm really glad.

+2

you are correct.

+2

Yahel, that's exactly what I mean. Saying Java passes objects by value just because it passes the value of the reference is absurd. It is easy to demonstrate, as you have, that the object is effectively passed by reference.

+2

yes, but in an job interview rather say by value :) (for java)

+2

There is disagreement over the meaning of "pass by refrence". and whenever you print java objects you are printing hash code of the object. program calls toString method then toString returns hash code i believe hashcodes are stored in constant pool. and in java/javascript when you pass objects to a function argument or simply assign it to a variable. you just simply assign its address(from heap) to the variable.(on stack). don't try to define words. and whenever you try to accessing its fields, yo just simply are dereferencing from it: C style : *(ptr).feild=9; Fields mean attributes of the object. not including methods Methods have their own story.

+2

Martin Taylor thanks for replying. I absolutely agree. and i meant default behavior of toString method. actually i explained hash code in resulted String and also i meant that what we are explaining to is a futile thing to do, while we know how it works. I don't think there's anything wrong in my first message. Have you found a wrong in it? can you say what exactly wrong in my words?

+2

Mehran, No you didn't say anything wrong in English. What you said was correct, though you failed to emphasise that it is the default behaviour of toString() that returns the hash code. This whole pass by reference pass by pointer definition in Java is just silly. Whatever Sun Microsystems or Oracle want to call it it is effectively pass by reference for objects. They are even called reference data types. It's like saying C always passes by value, which we could argue it does technically. When you pass the pointer of a data structure to a function it is a copy of the address that is passed. The value of the address. It just gets dereferenced. Java is the same. It passes the value of the hash code of the object. It's just that that hash code can be dereferenced to alter the original object. Whereas if a primitive variable is passed to a method only the value is sent. Altering the value of a primitive data type in a method does not effect the original value outside of the method. Oracle can call it pass by Banana Milkshake as far as I care. So long as the programmer understands what happens it doesn't really matter. It's all just word salad.

+2

I think Martin Taylor can't see more than 10 messages in the browsers. so i change some upvotes. message orders is by vote in browsers

+2

zemiak i'm happy usually.😊 Do you feel like I'm angry?! i don't use browser. if he set order to by date, last message are at the end of page. so he can't see them 🤔 i don't know why some people use browser rather than app!! 🙄

+2

@Mehran, it's only the first 20 messages that I can see using a browser and it sorts by votes. So I'm transmitting in the blind here. The sort by date option only works if there are less than 20 replies, it shows nothing otherwise, and yes you are correct; sort by date shows replies in chronological order with the oldest at the top and the newest at the bottom. The reason I use a browser from a laptop is because I'm old, very old, and my eyesight is not what it used to be. Staring at a tiny screen and pecking away at a tiny on screen keyboard is not an enjoyable experience for me. I'm so old the first computer terminal I used didn't even have a screen. It was a teletype terminal... https://en.m.wikipedia.org/wiki/Teletype_Model_33 though my fading memory can't guarantee that it was that exect model, I was just a young lad at school way back then. Yes, I've been using computers for that long. There aren't many of us old dinosaurs left.

+1

Martin Taylor, right. Thanks :)

+1

Martin Taylor & zemiak, but doesn't java pass the objects just like python or a C++ reference? It's like passing a pointer, isn't it? If you print a java object - you get the memory address (pointer)

+1

Here is another example... public class Program { public static void main(String[] args) { Object o = new Object(); System.out.println(o); print(o); } private static void print(Object o) { System.out.println(o); } } The same hash code is printed in either case. Since a hash code is unique that implies that it is the same object, or at least the same hash code which is the same thing.

+1

maybe i wrote something wrong in English?!

+1

+ immutable objects can't be changed by method this way public class Program { public static void main(String[] args) { Integer immutable = new Integer(100); IntNum mutable = new IntNum( 100); change( immutable, mutable ); System.out.println(immutable); // 100 System.out.println(mutable); // 101 } static void change(Integer immutable, IntNum mutable) { immutable++; mutable.value++; } } class IntNum { int value; IntNum(int n) { value = n; } public String toString() { return ""+value; } }

+1

Martin Taylor i believe hashcodes are just a unique numbers in constant pool. but you are saying "derefrencing from hash code"! hashcodes are mainly used for hash mechanism even we can override hashcode method to have two object with same hash code! but addresses are unique. Did you just want to give an example? Yeah. java is same as C and hides details such as derefrencing from programer.👌 we only call "pass" when we pass an argument to a function.. but this mechanism exists for any variable in Java/Javascript which points to an object. example: Obj a=new Obj() ; value of a is the refrence/address of the object Obj b =a;//copies the value of a into b. // b is same as parameter and a is same as argument it's exactly what happens whenever we pass an Refrence Variable to an method. that's because i'm saying naming this mechanism "pass by refrence" isn't good. It's like to saying it's only happening to arguments/ parameters.. but this happens for every assignment.

+1

zemiak Although this is a wrapper class but this happens for strings too. that's because you didn't any derefrencing from variable. it's like assigning a new value to a pointer in C.