argument question
posted 21 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).
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).
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 21 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
| Oh the stink of it! Smell my tiny ad! Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











