Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java arrrays

 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you say how this class prints output as 1.when i=j is that 'i 'getting the value of j?




 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually when you assign a new array object to the i in the change i method you create two different arrays.
Try this code


This will explain it better.
Edited :To give code example
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep that in my mind java always use pass by value.But in object passing the idea may be little bit ambiguous.Because what you pass is the bit pattern of the reference variable . So when you pass the bit pattern ,the reference in the method will point to the same object .So if you update one other will get changed.But if you try to say something like i=new int[3]; then you create a completely different object.
 
Mahmoud Hossam
Ranch Hand
Posts: 74
Linux Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejaka samarakoon wrote:Can you say how this class prints output as 1.when i=j is that 'i 'getting the value of j?






In this code,the method change_i can change the state of int j[],but it can't make it refer to another object

this was in K&B's book in chapter 3
 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone have k&B book to download
 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'i' reference variable in main method points to an array object containing an elemnt '1'....
when this is passed to change_i method then there is another reference var. 'i' in that method(change_i method) pointing to the same object on the heap.....

So, that means two array reference variables are of the same name 'i' pointing to the same object,,,,?

And this is happening because both these variables are declared in different methods, so there is no name collision or ambiguity in names?

Is my interpretation right?

Waiting for enlightenment........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic