• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 79240
377
  • 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: 23951
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: 79240
377
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic