What does it mean to return a value?

后端 未结 3 1876
渐次进展
渐次进展 2020-12-03 12:56

I\'m fairly new to programming, and I\'m confused about what it means exactly to return a value. At first, I thought it meant to output what value is being returned, but whe

3条回答
  •  执念已碎
    2020-12-03 13:46

    In simple terms, it means to return the value to caller of the method...

    So, in your example, the method getX would return the value of x to the caller, allowing them access to it.

    class Class1{
    
        static int x = 3;
    
        public static int getX(){
            return x;
        }
    
        public static void main(String args[]){
            int myX = Class1.getX(); // return the value to the caller...
            System.out.println(myX); // print the result to the console...
        }
    }
    

提交回复
热议问题