• 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

Array

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am weak in array. Please do help me with this question


the output is 2.
I am not getting how?

thanks
sandhya
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandhi,

you need to understand two logic here..!!

when you wrote this:

int[] a = {1};


What is the meaning of this:
this means:
a ---> refers to an int array on heap ---> size of array is 1 ---> a[0] refers to 1

when you call method by passing a that means you are passing the reference to that array Object, if you pass a[0] you are passing the value 1.

Inside Method:

Now i is also referring to one array object.
i ---> refers to an int array on heap ---> size of array is 1 ---> i[0] refers to 1

In fact both a and i refers to same object.

you increment the i[0] by 1, so now..
i ---> refers to an int array on heap ---> size of array is 1 ---> i[0] refers to 2

Or we can say that:
a ---> refers to an int array on heap ---> size of array is 1 ---> a[0] refers to 2

As both were referring to same object.
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sunny. i got it now.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main(String[] args){
int[] a = {1};
Test5 t = new Test5();
t.increment(a);
System.out.println(a[a.length - 1]); // o/p is 2
}
void increment(int[] i){
System.out.println(i[i.length - 1]++); // o/p is 1
}
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think because of the post increment operator it first prints 1 and then increments by 1. since both a and i are referring to the same object, a[0] will be 2 and hence prints 2 the second time.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In simple words,

increments the value of the element in the array, by 1.

Gitesh
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic