+ 1
Why output is not 16?
I am using parameter passing. but I use return type for the program. but the output is not return value, it shows assigned value. y ? public class Program { public static void main(String[] args) { int x=4; square(x); System.out.println(x); } static int square(int x){ x=x*x; return x; } }
2 ответов
+ 7
You need to use x = square(x); instead of square(x); as this doesn't change the value of variable of data type int.
+ 4
in java primitive variables are passed by value (a copy is created and changes on that copy don't affect the original variable.
Objects are intead passed by referenve.