This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

argument question

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


What is the result of attempting to compile and run the program?

a. Prints: 1,1
b. Prints: 1,3
c. Prints: 3,1
d. Prints: 3,3
e. Run-time error
f. Compile-time error
g. None of the above

answer: c
======================

I thought mehtod m1 could not change the local variables values,
But the answer is C.
I'm confused. Any ideas?
Thank you.
 
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When u are passing a reference to the object,in this case it is an array reference,in a method,the reference will remain the same.ie,it will still point to the same object even after the method invocation.But the member variables in the object will change.

For e.g.

class ReferenceTest {
public static void main (String [] args) {
Dimension d = new Dimension(5,10);
ReferenceTest rt = new ReferenceTest();
System.out.println("Before modify() d.height = " + d.height);
rt.modify(d);
System.out.println("After modify() d.height = " + d.height);
}
void modify(Dimension dim) {
dim.height = dim.height + 1;
System.out.println("dim = " + dim.height);
}
}

You can see that the height of the object d has actually changed after calling the method modify(dim).
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the arrays are basically objects and you do pass by reference ie you pass the memory address and swap .Hence ans is c
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in main, you have references to 2 arrays. think of them as notes saying 'the blue box' and 'the red box'.

now, when you pass them in to your function, you make a photocopy of each note. you now use those notes and say "go to 'the blue box' and change what's inside it". and then "go to 'the red box' and change what's inside it".

when you get back to main, you say "show me what is in 'the blue box'" etc.

So, what is in 'the blue box' has indeed changed.

IF, however, after you passed in those photocopies, you then in m1 say

you have just erased what's on the paper, and now have it pointing to some BRAND NEW BOX. if you then say i1[0] = 8, you will not change what's in the box referred to by the note out in your main() routine.
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic