To know the logic behind it
is there anybody who explain me this coding
public class FunctionCall {
public static void funct1 () {
System.out.println ("Inside funct1");
}
public static void main (String[] args) {
int val;
System.out.println ("Inside main");
funct1();
System.out.println ("About to call funct2");
val = funct2(8);
System.out.println ("funct2 returned a value of " + val);
System.out.println ("About to call funct2 again");
val = funct2(-3);
System.out.println ("funct2 returned a value of " + val);
}
public static int funct2 (int param) {
System.out.println ("Inside funct2 with par开发者_运维技巧am " + param);
return param * 2;
}
}
public class FunctionCall {//a public class
public static void funct1 () { /a static method which returns nothing void
System.out.println ("Inside funct1");
}
public static void main (String[] args) {//main method
int val;//int variable declaration
System.out.println ("Inside main");//printing on console
funct1();//calling static method
System.out.println ("About to call funct2");////printing on console
val = funct2(8);//calling static method which returns int and assigning it to val variable
System.out.println ("funct2 returned a value of " + val);//printing on console
System.out.println ("About to call funct2 again");//printing on console
val = funct2(-3);////calling static method which returns int and assigning it to val
System.out.println ("funct2 returned a value of " + val);//printing on console
}
public static int funct2 (int param) {
System.out.println ("Inside funct2 with param " + param);
return param * 2;
}
}
Of course, but where is the problem?
There are 2 static methods
public static void funct1 () {
System.out.println ("Inside funct1");
}
and
public static int funct2 (int param) {
System.out.println ("Inside funct2 with param " + param);
return param * 2;
}
The output on console will be (If I don't made a calculation mistake):
Inside main
Inside funct1
About to call funct2
Inside funct2 with param 8
funct2 returned a value of 16
About to call funct2 again
Inside funct2 with param -3
funct2 returned a value of -6
It starts with the main, then it enters the funct1 once; then it calles func2 with 8 and another time with -3
精彩评论