java中的前++和后++的疑惑?

public void function1() {
		
		int x = 10;
		int y = x++ + 5;
		System.out.println("x=" + x + "------y=" + y);
		
		int m = 10;
		int n = ++m + 5;
		System.out.println("m=" + m + "------n=" + n);
		
		int a = 10;
		int b = ++a + ++a;
		System.out.println("a=" + a + "------b=" + b);
		
		int c = 10;
		int d = ++c + c++;
		System.out.println("c=" + c + "------d=" + d);
		
		int g = 10;
		int h = g++ + ++g;
		System.out.println("g=" + g + "------h=" + h);
		
		int e = 10;
		int f = e++ + e++;
//		int f = e++ + 0 + e++;
		System.out.println("e=" + e + "------f=" + f);
	}

还有另一个问题,如下图:

	public void function2() {
		int r = 0;
		int result = ++r + r++ + ++r + r;
		//result的值为多少
		System.out.println("result=" + result + "----r=" + r);
	}

源代码

package javase3;

import java.util.Calendar;
import java.util.Date;

//java笔试题
public class Test {
	
	public void testFunction(String str) {
		System.out.println("String");
	}
	
	public void testFunction(Object obj) {
		System.out.println("Object");
	}
	
	public static void main(String[] args) {
		Test test = new Test();
		test.testFunction(null);
		test.testFunction(new Test());
		test.testFunction("江西省赣州市于都县");
		
		test.function1();
		
		test.function2();
		
		test.function3();
		
		test.function4();
	}
	
	public void function1() {
		
		int x = 10;
		int y = x++ + 5;
		System.out.println("x=" + x + "------y=" + y);
		
		int m = 10;
		int n = ++m + 5;
		System.out.println("m=" + m + "------n=" + n);
		
		int a = 10;
		int b = ++a + ++a;
		System.out.println("a=" + a + "------b=" + b);
		
		int c = 10;
		int d = ++c + c++;
		System.out.println("c=" + c + "------d=" + d);
		
		int g = 10;
		int h = g++ + ++g;
		System.out.println("g=" + g + "------h=" + h);
		
		int e = 10;
		int f = e++ + e++;
//		int f = e++ + 0 + e++;
		System.out.println("e=" + e + "------f=" + f);
	}
	
	public void function2() {
		int r = 0;
		int result = ++r + r++ + ++r + r;
		//result的值为多少
		System.out.println("result=" + result + "----r=" + r);
	}
	
	public void function3() {
		int i1 = 20, i2 = 5;
		System.out.println("i1 + i2 = " + i1 + i2);
		System.out.println("i1 - i2 = " + (i1 - i2));
//		System.out.println("i1 - i2 = " + i1 - i2); //语法报错,连编译都通不过
		System.out.println("i1 * i2 = " + i1 * i2);
		System.out.println("i1 / i2 = " + i1 / i2);
	}
	
	//java规定,月份从0到11
	public void function4() {
		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.get(Calendar.MONTH));
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
		
//不要用Date()里面的 getMonth(),getDate()等方法,因为sun已经不提倡使用这些方法了,随时可能在以后的版本里取消
		Date date = new Date();
		System.out.println(date.getMonth() + "-" + date.getDate());
	}

}

运行结果如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值