aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java arrrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java arrrays" Watch "Java arrrays" New topic
Author

Java arrrays

thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56
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

Joined: Aug 08, 2009
Posts: 47
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


[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
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

Joined: Feb 20, 2010
Posts: 74

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

Joined: Jul 20, 2010
Posts: 56
can anyone have k&B book to download
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
'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........


Waiting for enlightenment....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java arrrays
 
Similar Threads
doubt in arrays
Passing Array References, javaprepare Q.30
Array - question
Problem with passing variables to a function.
help!!