- 1
Please explain why it prints 1
Java pass by value
6 Réponses
+ 2
Ilja Veselov it is a simple pass by value concept where the function creates a copy of the argument and doesn't alter the actual argument passed.
1) So this makes clear that the modification inside your function doesn't affect your actual value.
2) Now also "i" has a value 0. But since it is an instance variable and not a class variable, it cannot be accessed inside the main method.
So two options are eliminated.
All that remains is 1.
You can read about actual and formal arguments to have further clarity.
+ 2
I made a small example about the difference between pass by value and pass by reference:
https://code.sololearn.com/cMhUxkqNYsho/?ref=app
+ 1
Thanks!
+ 1
Thanks a lot
0
What happens when the following program is compiled and run. Select the one correct answer.
public class example
{
int i = 0;
public static void main(String args[])
{
int i = 1;
change_i(i);
System.out.println(i);
}
public static void change_i(int i)
{
i =2;
i *=2;
}
}
0
Sorry but no it's still prints 1...must be another reason