java中数据类型作为参数和作为返回值的区别

java中数据类型作为参数和作为返回值的区别

基本数据类型,传(返回)一个相应类型的变量(或者直接返回一个值)即可
引用数据类型传一个对象名(或者直接 new一个对象返回)

public class Test {
    /**
     * 返回一个基本数据类型
     * @return
     */
    public int returnBase1() {
        int a = 1;
        return a;
    }

    /**
     * 返回一个基本数据类型
     * @return
     */
    public int returnBase2() {
        return 1;
}

    /**
     * 返回一个引用数据类型
     * @return
     */
    public BaseType returnBean1() {
        BaseType baseType = new BaseType();
        return baseType;
    }

    /**
     * 返回一个引用数据类型
     * @return
     */
    public BaseType returnBean2() {
        return new BaseType();
    }
}

猜你喜欢

转载自www.cnblogs.com/anke-z/p/12356476.html