java中前++、后++和return的执行顺序

文章详细描述了一个Java类Test中的五个公共方法,展示了如何在函数中操作变量(如自增),以及每个函数的返回值行为。通过main方法调用这些函数并输出结果,以说明程序执行过程。

直接po代码案例

运行结果如下:

完整的代码如下:

package test;

public class Test {

	int a = 5;
	int b = 10;
	int c = 200;
	int d = 500;
	int e = 800;

	public int functionA() {
		System.out.println("操作变量a之前,a = " + a);
		a++;
		System.out.println("操作变量a之后,a = " + a);
		return a;
	}

	public int functionB() {
		System.out.println("操作变量b之前,b = " + b);
		++b;
		System.out.println("操作变量b之后,b = " + b);
		return b;
	}

	public int functionC() {
		System.out.println("操作变量c之前,c = " + c);
		return c++; // 后++(先返回c,然后c再自加)
	}

	public int functionD() {
		System.out.println("操作变量d之前,d = " + d);
		return ++d; // 前++(d先自加,然后再把d返回)
	}

	public int functionE() {
		System.out.println("操作变量e之前,e = " + e);
		e = e + 66;
		System.out.println("操作变量e之后,e = " + e);
		return e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test = new Test();

		System.out.println("functionA函数的返回值是" + test.functionA() + ",调用完函数后a = " + test.a);
		System.out.println("-----------\n");
		System.out.println("functionB函数的返回值是" + test.functionB() + ",调用完函数后b = " + test.b);
		System.out.println("-----------\n");
		System.out.println("functionC函数的返回值是" + test.functionC() + ",调用完函数后c = " + test.c);
		System.out.println("-----------\n");
		System.out.println("functionD函数的返回值是" + test.functionD() + ",调用完函数后d = " + test.d);
		System.out.println("-----------\n");
		System.out.println("functionE函数的返回值是" + test.functionE() + ",调用完函数后e = " + test.e);

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值