Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes what will be the output of this two dimentional array and why Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what will be the output of this two dimentional array and why" Watch "what will be the output of this two dimentional array and why" New topic
Author

what will be the output of this two dimentional array and why

ravi suthar
Greenhorn

Joined: Apr 22, 2012
Posts: 17

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you try to execute the code ?


[My Blog]
All roads lead to JavaRanch
ravi suthar
Greenhorn

Joined: Apr 22, 2012
Posts: 17

Christophe Verré wrote:Did you try to execute the code ?


i am expecting the output will print 3
but the actual output is 5
i am confused why it is so
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can guess what a[2]=a[0]; is doing ?
ravi suthar
Greenhorn

Joined: Apr 22, 2012
Posts: 17

Christophe Verré wrote:Can guess what a[2]=a[0]; is doing ?


a[2] is start referring to a[0]
both a[2] and a[0] have the same reference
and there indexing would be 0 1 2 3 4
so the sum would be 0+1+2=3 ???
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

When you start writing to a[2], what happens to the elements in a[0] ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.
ravi suthar
Greenhorn

Joined: Apr 22, 2012
Posts: 17

ok, i got the answer
when i=0 value of a[0][3] will be 3
and when i=2 value of a[2][3] will be 5
both a[2] and a[0] reffered at same.
so value 3 is replaced by 5
it means every value of a[0] is replaced by a[2]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

Campbell Ritchie wrote:You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.



I think the OP used the terms "refering" and "reference" in regards to an int[] (elements of int[][]) -- and in that context, "reference" is correct.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Yes, you are right Henry. I didn’t notice that. Well done noticing, and I am sorry about my mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what will be the output of this two dimentional array and why
 
Similar Threads
Digits in Array
Rule Round Up #30 - What?? for loop declarations
for() identifiers question
jsp tags inside javacript creating error
for loop