如何在Java中使用方法的返回值? [英] How to use return value from method in Java?

查看:360
本文介绍了如何在Java中使用方法的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将成员函数的返回值存储到变量中然后使用它来使用它.例如:

I want to use the return value from a member function by storing it into a variable and then using it. For example:

public int give_value(int x,int y) {
  int a=0,b=0,c;
  c=a+b;
  return c;
}

public int sum(int c){ 
  System.out.println("sum="+c); 
}              

public static void main(String[] args){
    obj1.give_value(5,6);
    obj2.sum(..??..);  //what to write here so that i can use value of return c 
                       //in obj2.sum
}

推荐答案

尝试

int value = obj1.give_value(5,6);
obj2.sum(value);

obj2.sum(obj1.give_value(5,6));

这篇关于如何在Java中使用方法的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆