+ 9
What is passed to the constructor? (Solved)
Hi, When passing a string value or primitive type to a constructor i understand a refrence is not needed as you can just pass the value directly over but when passing an array its diffrent as you need to create an object of the array and pass the reference to the constructor for some reason i dont seen to understand the concept of this as to me its not passed by value but by the refrence đ€ so im wondering when the class constructor gets the argument it would look somthing like program(String [] arr = reference){}
10 Answers
+ 3
Asiya Rehman yes, so what is the problem? maybe this helps you: http://www.javadude.com/articles/passbyvalue.htm
+ 18
Max but when we pass by reference, dosent it mean address of ( first location of ) array is passed not the values it self.
+ 18
So why D_Stark says they are passed by value and we dont need to initialise it by using .operator. I'm trying to understand that.
+ 8
Asiya Rehman after lots of studying i figured out my misunderstanding when you create a String for example String str = "Java"; variable str dosent actually contain the String "Java" but a reference to it so the same goes when a new string array is created the variable holds the reference to the objects(i guess there like pointers) which are then passed by value (always) i didnt actually know this untill today only primitive values are stored to variables i hope that helps and explains the reason why you dont have to use the dot to access the objects in arrays đ
+ 6
Max yes exactly which would mean that the constructor array variable holds the reference and [] are used to point to the objects if thats right the argument was passed by reference but apparently everything is passed by value this is the part i didnt get.
+ 6
Max thanks do you have a source about this i could read?
+ 2
what do you mean by "there is nothing to point to?" it points to the argument
+ 2
Asiya Rehman the argument for everything is pass by value is basically that if you create a new object in your program it is already stored in the variable as a reference to the actual object on the heap and if you pass it to a function it copies the reference. but to the programmer it looks like the object is stored in the variable, so when passing to a function it looks like pass by reference. iâm not sure what this has to do with initialization and constructors. so in c++ terms. object o = new object();
is equivalent in c++ to object* o = new object();
but using a fancy smartpointer garbage collector thingy
but this fact is hidden from the programmer, so he thinks he is using pass by reference even though he is passing the reference using pass by value
+ 2
D_Stark the article i posted explains it
+ 1
no, the pass by reference is implicit. the String[] arr is just the type of the reference. it doesnât mean that the reference is stored in the array, it means that arr is a reference to a String[]. when you create a new array with String[] array = new String[]; somewhere in your programm array also only stores the reference, so whats happening under the hood is that you are just passing the reference using pass by value