+ 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.

28th Sep 2021, 8:29 AM
Yahel
Yahel - avatar
25 Respostas
+ 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.
28th Sep 2021, 10:57 PM
Sonic
Sonic - avatar
+ 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.
29th Sep 2021, 5:50 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 2
you are correct.
28th Sep 2021, 8:36 AM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 2
yes, but in an job interview rather say by value :) (for java)
28th Sep 2021, 2:24 PM
zemiak
+ 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.
28th Sep 2021, 4:00 PM
Mehran
Mehran - avatar
+ 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?
28th Sep 2021, 6:30 PM
Mehran
Mehran - avatar
+ 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
28th Sep 2021, 9:11 PM
Mehran
Mehran - avatar
+ 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!! 🙄
28th Sep 2021, 9:30 PM
Mehran
Mehran - avatar
+ 1
Martin Taylor, right. Thanks :)
28th Sep 2021, 12:10 PM
Yahel
Yahel - avatar
+ 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)
28th Sep 2021, 2:58 PM
Yahel
Yahel - avatar
+ 1
maybe i wrote something wrong in English?!
28th Sep 2021, 6:39 PM
Mehran
Mehran - avatar
+ 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; } }
28th Sep 2021, 8:03 PM
zemiak
+ 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.
28th Sep 2021, 9:05 PM
Mehran
Mehran - avatar
+ 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.
28th Sep 2021, 9:09 PM
Mehran
Mehran - avatar
0
Martin Taylor, I just rechecked and yeah, java also takes object's reference to a function. I made a class object and passed it to a void function that alters the attributes of the object and checked if it did... Both in Python and Java - it alters the data, meaning that objects are passed by reference (but primitives by value)
28th Sep 2021, 12:02 PM
Yahel
Yahel - avatar
0
Mehran, zemiak & Martin Taylor In conclusion, 1. both in C++ Pointers and Java Objects(hashcodes), they are copied to the function's parameter and can be dereferenced. So far tell me if I'm correct. 2. I also believe that python object work the same as the latter. 3. If the pointers/hashcodes are copied - we basically call it "by reference", and when the whole object/primitive in being copied to another entity - we call it "by value". Am I right? 4. Is there more I should know?
28th Sep 2021, 9:54 PM
Yahel
Yahel - avatar
0
values
29th Sep 2021, 11:52 PM
Cookie Fans club
Cookie Fans club - avatar
0
what is next after hello world
4th Oct 2021, 8:45 AM
Nogomi Adam
Nogomi Adam - avatar