• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays in methods value  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason why when I pass an array into a method as an arquement after the method is done using it that it then changes the value of the array I passed in even though you do not specify a return type back to the main method.

BUT when you do this with a primitive type such as an integer it does not change the value here is the code to show you an example of what I mean.


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't allowed to change the "thing in memory" that the variable points to directly. For an int, that's the value. For an array, it's a reference to a bunch of array elements.

This example would be equivalent to the int one:


Changing the array to point to a different array would not affect the caller. Just like changing the int to a different number doesn't change the caller.

By contrast, in your example, the array is still the same array. Just with different values for the elements.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You aren't allowed to change the "thing in memory" that the variable points to directly. For an int, that's the value. For an array, it's a reference to a bunch of array elements.

This example would be equivalent to the int one:


Changing the array to point to a different array would not affect the caller. Just like changing the int to a different number doesn't change the caller.

By contrast, in your example, the array is still the same array. Just with different values for the elements.



kind of follow you still a little confused I know the array name is just a reference to 7 different doubles in this case in memory but I also declared another array in the methods paramaters so now would my array other which I passed in point to the array or the reference of tempatureIn in the method? if so I think I understand :P
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, there are two double arrays: tempature and other. The former is not actually used anywhere. You could delete it and the code would do the same thing. The other is passed to both methods. Where is modified, but still the same array.

In my example, I actually create a new array and change the reference in the method. The caller doesn't know about this change since it still points to the original array.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand your problem there are two simple rules to keep in mind with regards to passing arguments to methods...
  • Primitives are passed by value...
  • Objects are passed by reference...


  • What these rules mean is that:

    Passing a primitive as an argument to a method simply binds the value of the argument to the method's parameter name... This creates the effect of an independent entity with the same value within the scope of the method...

    Passing an object as an argument to a method still simply binds the value of the argument to the method's parameter name... This also creates the effect of an independent entity with the same value within the scope of the method... The difference within this context is that the value is the address of the physical object within memory so any operations performed using this address will affect all references with the same address as their value...

    Since an array is an object which brings it into the realm of a reference type the second rule is in effect...
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rico Felix wrote:Objects are passed by reference...
    I think there is nothing wrong with this statement.
    But I always like to use this as...
    Object references(pointers) are passed by value
    The reason of my liking for the above statement is...There cannot be a direct object with which we can deal. We always deal with the pointer(I prefer the term pointer to reference) to the object.
    So when method execution comes in picture we can say that
    Object reference is passed
    Although I donot take it wrong when people say Object is passed
     
    Adam Chalkley
    Ranch Hand
    Posts: 518
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:In your example, there are two double arrays: tempature and other. The former is not actually used anywhere. You could delete it and the code would do the same thing. The other is passed to both methods. Where is modified, but still the same array.

    In my example, I actually create a new array and change the reference in the method. The caller doesn't know about this change since it still points to the original array.



    but how come even when I don't initialize the array tempatureIn in my method I do not get an error just the java hashcode unlike when I create the array and not initialize it inside a method and not in the parameters I get an intialization error like for any reference types for example

    >

    haven't intialized tempatureIn and assigned it space in memory but I still could print out the hashcode without initaizing it but when I do it out of the parameters in main for example I get an error



    get an error.


    Also would I be correct now I'll try to explain it the way I see it now

    so other is passed in as the arquement to the method Enter
    so now TempatureIns value its reference now also points to other's reference(where in memory the values are located) in a rather simplified way
    so now Others values not reference value is overwritten by the method
    am I correct in a way?

    Thanks for the help I'm getting there I think let me know if I'm right haha =)
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rico Felix wrote: . . .
  • Primitives are passed by value...
  • Objects are passed by reference...

  • . . .
    That is total nonsense, even though you see it in many places.

    All arguments are passed by value in Java®. It does not support pass by reference. Confusion arises because many objects are mutable and when you pass an argument, you are creating an alias. You do not have two objects, but two references to the same object.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tapas Chand wrote:
    Rico Felix wrote:Objects are passed by reference...
    I think there is nothing wrong with this statement.
    There is a lot wrong with that statement.

    Object references(pointers) are passed by value . . .
    That bit is correct, but the rest of your post was incorrect. Do not prefer pointer to reference. You cannot manipulate the pointer behind the reference.
     
    Rico Felix
    Ranch Hand
    Posts: 411
    5
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Seeing that the statement made about objects being passed by reference is unacceptable I would like to also clarify this once and for all...

    Given the following:

    What is obj called?

    Given the following:

    What is obj in the method parameter called?
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know what you mean, but this part is a tricky one, and many literature could give you a misleading information. Maybe not that misleading, but not fully explained in deep details. And this confusion could take a place by using terminology interchangeably in different programming languages.

    In your first example, "obj" is a reference variable, which holds the information about the location in memory, where the object is stored in memory.
    In the method parameter, "obj1" (i'll call it like this for simplicity) is a copy of "obj" reference variable (but "obj1" is not the reference variable which refers to "obj" variable as it would be in truly pass-by-reference languages, but it is a copy of it, which only refers to the same object), and copied reference variable is considered as a value in Java.

    To visualise this, example might would help:
    Output:
    mercedes max speed: 220
    mercedesUpgrade max speed: 270

    So, from the example you see, the "mercedes" object were not changed, as it were a copy (value) of original reference variable. And this is actually the main difference between "pass by value" and "pass by reference" terminology. If Java pass type were considered as latter one, both "mercedes" and "mercedesUpgrade" max speed would be the same.

    [edit: added some] Nevertheless, if you pass the variable, which refers to an array, and you do the sort, array content gets sorted. Difference is, that you're not constructing a new Array object, so both reference variables (original, and copied value of it) still refers to the same object. And this is probably what mostly makes you believe about your theory about "pass by reference" type.
    Example:
    Output:
    [1, 2, 3, 4]
     
    Tapas Chand
    Ranch Hand
    Posts: 614
    9
    BSD Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:...Do not prefer pointer to reference...

    Thank you Campbell, I will take care in future.
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:. . . Maybe not that misleading, but not fully explained in deep details. And this confusion could take a place by using terminology interchangeably in different programming languages.
    . . .
    Yes, it is that misleading. And it is often caused by using the terminology wrongly.

    I would say that in
    public void function(Object obj)
    “obj” is a parameter into which a copy of a reference will be put. Inside the method, any use of obj will use that same reference unless you say
    obj = ...;
    inside that method. Remember that any
    obj = ...;
    inside that method is independent of any obj = …; anywhere else. That is what pass by value means.

    What people do not understand is that they are often passing mutable reference types. If you change the state of the object in one reference, then the state of that object changes. If you have two references to one car object, then it will speed up from from 220 to 270 and that new speed is visible everywhere. If you have two references to one array and sort it in one place, the sorting will be visible in the other places. For the purposes of this discussion, consider an array simply to be a mutable reference type.

    I hope those speeds, Liutauras, are in kph; if you go that fast in mph, this sort of thing happens.
    [edit 2×]Spelling errors corrected.
     
    Rico Felix
    Ranch Hand
    Posts: 411
    5
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting enough... Thank you guys for your time and energy in correcting my seemingly misuse of terminology although I fully understood the semantics...

    All that's being copied is a series of bit patterns where for primitives the bit patterns represent the value itself in contrast to reference types where the bit patterns is simply the address in memory where the object resides...

    It really does make sense to state that all arguments are passed by value...
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you pass a copy of the bit pattern representing the location of the object.

    Actually, locations may be maintained as handles, i.e. pointers to pointers, rather than straight pointers, so you may actually pass the handle instead.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!