• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Array assignment -- need help

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why does it print 3,1???
Can somebody explain in detail....
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Because you just swapped i1 and i2.
[ November 28, 2005: Message edited by: Ken Blair ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you seriously ask this question? What do you expect this code produce?
 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but why does the second println statement print 1,1???
 
Miguel Antonioli
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second println should NOT print 1, 1. It should, however, print 3,1.

Notice in your m2() method, you swap the value of i1 and i2 using i3 as your temporary array.
[ November 28, 2005: Message edited by: Miguel Antonioli ]
 
Jayashree Mohan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah.. even i thought the same as we are passing the object reference in the methods.. but when I run the program,,, i get the answer 1,1...
I am not able to understand why???
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jayashree Mohan:

I am not able to understand why???



In the first program, the method parameters to the "swapping" method have the same names as the member variables, but you use "this" to disambiguate the two. In the second program, there's only one parameter, and it has the same name as one member variable, but you don't use the class name to disambiguate, so while Test2.i2 gets reassigned, Test2.i1 does not. See?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code the parameter is said to "hide" the member because they have the same name. When a paramter hides a member you can refer to the member by prefixing it with the keyword "this". Consequently, in the above code while within m() "o" refers to the parameter (object 2) and the only way to access the member (object 1) is to refer to it by "this.o". If you didn't understand Ernest before reading this, try reading his reply again and it should make sense.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic