• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 17
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to execute the code ?
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can guess what a[2]=a[0]; is doing ?
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you start writing to a[2], what happens to the elements in a[0] ?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right Henry. I didn’t notice that. Well done noticing, and I am sorry about my mistake.
 
I'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic