+ 7

Please Help.explain this. why? how?

class Program { int x=2; int z=f(f(x)); int f(int x){ return x+x; } public static void main(String[] args) { Program obj = new Program() ; System.out.println(obj.z); } }

1st Apr 2019, 10:53 AM
Iftakher Hossain
Iftakher Hossain - avatar
3 Antworten
+ 9
f(2)=2+2=4 f(4)=4+4=8 Output is 8.
1st Apr 2019, 12:12 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
The output would be 8. When you creating a new object it sets x=2. And then it calls a function to evaluate z. But in first function call the parameter is the number, that a subfunction should return. z=f(f(x))=f(x+x)=f(2+2)=f(4)=4+4=8.
1st Apr 2019, 12:11 PM
Julia
+ 2
Thanks all.
1st Apr 2019, 1:17 PM
Iftakher Hossain
Iftakher Hossain - avatar