• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Array Behaviour

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



i1 -------------> 1 - Array Object

i2 -------------> 3 - Array Object

Passing array reference i1 by copy of value to m1. So inside the method m1,

i3 --------------> 1 <-------------------- i2
Array Object of i1

i1 ----------------> 3
Array Object of i2

Inside the method m1, we are not changing any array contents..just changing the references and once the m1 method exits all the refereneces of i3, i2 (both points to i1's array object) and i1 (points to i2' array object) are eligible for gc.

but still i1 and i2 points to 1 and 3 array object only..

but how the output is coming as 1,1 instead of 1,3?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
you wrote

but still i1 and i2 points to 1 and 3 array object only..



No, there's
i2 = i3;

in the code where the class variable i2 is redirected to the object of i1.


Perhaps change the(int i1) in the method declaration to (int xy).
Or compare to this code, it has the same structure as class GFC306, only with ints instead of arrays, and without confusing naming:

prints one twice.



Yours,
Bu.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Burkhard,

I thought Java passed a copy of the reference to the method argument? I can see why yours works because you were using primitives.

Shouldn't the original answer to Micheal's question be 3,1?

i1 = 1
i2 = 3

Then the method call...

i3 = 1
i1 = 3
i2 = 1

Or am I really confused?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Greg,

perhaps I should have used objects to avoid confusion. Works the same:

prints
java.awt.Point[x=1,y=1]
java.awt.Point[x=1,y=1]

The method, step by step:
static void method(Point parameter) {
a copy of the reference (i.e. a copy of the byte pattern telling where the object can be found) is passed to the method.
As the methods is called from main with (a), parameter points to the point object [1,1].


Point local = parameter;
A new reference (I called it local now) is created. It points to the object where parameter points to. Same [1,1].
If "parameter" would be redirected (or re-referenced as these Java Gurus call it) to some other object, "local" would still point to [1,1].


parameter = b;
The parameter (not a!) is redirected to the object that b points to, the [2,2] object.

b = local;
Here's the knack. The class variable b, that originally pointed to [2,2] is now redirected to the object local points to, i.e. the [1,1] object. It will still point to [1,1] even if the method is over and local will be GC-ed.


By the way, the middle part is unnecessary, here's the original again:


Is just the same as

meaning "static variable i2 is where parameter points to.".
And this is the i1 (class variable) from the call in main.


Yours,
Bu.
[ December 05, 2006: Message edited by: Burkhard Hassel ]
 
Squanch that. And squanch this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic