Java: Not a statement

后端 未结 2 1326
梦毁少年i
梦毁少年i 2020-12-06 11:10

I suppose this is more a question about language theory than anything else. Why is the first statement in main legal, when the second is not? Don\'t they evaluate to be the

2条回答
  •  一生所求
    2020-12-06 11:49

    In the first statement you are actually calling a function and second statement doesn't give any value. Incase you want to process the return value, you need to call a variable for return type

    Eg:

    public class Main {
    
        public static void main(String[] args) {
            int n = foo();
        
             //do whatever you want with return 
        }
    
        public static int foo() {
            return 0;
        }
    }
    

提交回复
热议问题