- 1
The Answer should be 16 as we are using the actual parameters
9 odpowiedzi
+ 9
the problem in question:
What is the output of this code?
public static void main(String[ ] args) {
int x = 4;
square(x);
System.out.println(x);
}
static void square(int x) {
x = x*x;
}
The answer is 4 as the value of x is only changed in the local scope for the 'square' method. Notice that it does not print the value of square(x) and the value of x in the main method is not changed to x=square(x), you're just printing out the originally declared value of x,4.
+ 1
I spent a few days not understanding this, but I think I figured it out. My explanation is for num + 1, but it will work for x*x as well.
First off, we have x = 5. No problem, easy to understand that.
Then we call the method: addOneTo(x);
We expect that x, which has the value of 5, will be associated with num in the method set-up, and that one will be added to it, producing 6. We expect that x now equals 6.
Then we print out x, and are surprised that it is still 5 and not 6.
-----
Several people tried changing the term void in the method, but that is not the reason it prints 5. If you simply change void, you have to do other things to make it work and print 6. Void is not the problem.
I even tried commenting out the original x, and passed 5 into the called method. It worked, but gives the impression that we could not associate x with num. Later lessons demonstrated that we could, so I came back to this to figure it out.
The lesson states that the method "takes the value of its parameter which is why the original variable is not affected", but this is not exactly true. Yes, the method does take the value of the parameter num, in which x is passed through, x is associated with num, and 1 is added to it, and the called method is sitting there with a value of 6, just as we would expect it to. So the original variable IS affected. The called method is not the problem, and calling x is not the problem.
The print line is the problem. The printline is not part of the method, has no clue about num, so IT is the one reading the original value of x, and printing the output of 5. Not the called method.
If you leave everything else as it is (because everything else is not the problem), and move the print line into the method, with the parameter of num, everything works... and the called method with x gives the correct output of 6, which it was doing all along.
0
I was going through the tutorial, and found a similar code:
public class MyClass {
public static void main(String[ ] args) {
int x = 5;
addOneTo(x);
System.out.println(x);
}
static void addOneTo(int num) {
num = num + 1;
}
}
// Outputs "5"
// Above is the standard example shown in the tutorial.
// Below is the modification in order to output 6:
public class MyClass {
public static void main(String[ ] args) {
int x = 5;
int y;
// addOneTo(x);
System.out.println(addOneTo(x));
}
static int addOneTo(int num) {
num = num + 1;
return num;
}
}
0
The answer will be 4.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
0
the answer will be 4
0
The answer will be 4
0
the answer is 4
- 1
To what are you referring?
- 1
link please