- 1

Please explain why it prints 1

Java pass by value

1st Dec 2019, 9:46 AM
Ilja Veselov
Ilja Veselov - avatar
6 Respuestas
+ 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.
1st Dec 2019, 10:03 AM
Avinesh
Avinesh - avatar
+ 2
I made a small example about the difference between pass by value and pass by reference: https://code.sololearn.com/cMhUxkqNYsho/?ref=app
1st Dec 2019, 10:17 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thanks!
1st Dec 2019, 10:26 AM
Ilja Veselov
Ilja Veselov - avatar
+ 1
Thanks a lot
1st Dec 2019, 11:16 AM
Ilja Veselov
Ilja Veselov - avatar
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; } }
1st Dec 2019, 9:47 AM
Ilja Veselov
Ilja Veselov - avatar
0
Sorry but no it's still prints 1...must be another reason
1st Dec 2019, 10:02 AM
Ilja Veselov
Ilja Veselov - avatar