Java增量运算符查询(++ i和i ++) [英] Java Incremental operator query (++i and i++)

查看:92
本文介绍了Java增量运算符查询(++ i和i ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public class Book {
    private static int sample1(int i) {
        return i++;
    }
    private static int sample2(int j) {
        return ++j;
    }

    public static void main(String[] arguments){ 
        int i = 0;
        int j = 0;
        System.out.println(sample1(i++)); //0
        System.out.println(sample1(++i)); //1
        System.out.println(sample2(j++));//1
        System.out.println(sample2(++j));//2
        System.out.println(i);//2
        System.out.println(j);//2
    }
}

我的预期输出在注释中.实际输出如下:

My expected output is in comments. The actual output is below:

0
2
1
3
2
2

我对函数调用和增量运算符感到困惑.有人可以请您解释一下实际结果吗?

I'm getting confused with the function calls and incemental operator. Can someone kindly explain the actual result?

推荐答案

首先,您需要了解x++++X;

First of all you need to know the difference between x++ and ++X;

对于x++:

首先将使用当前值,然后将其递增. 这意味着您将获得该操作的当前值x,如果您 下次使用x将获得递增的值;

First the current value will be used and it will be incremented next. That means you will get the present value of x for the operation and if you use x next time will get the incremented value;

对于++x:

首先,当前值将增加,然后将使用它(增加的值),这意味着您将获得增加的值 在此操作中以及此操作之后的其他操作.

First the current value will be incremented and it will be used (the incremented value) next, that means you will get the incremented value at this operation and for other after this operation.

现在让我们分割代码并分别进行讨论

Now lets split the code and discuss them separately

方法:sample1():

method: sample1() :

private static int sample1(int i) {
    return i++;
}

此方法将获取一个int值,然后先返回它,然后尝试递增,但是返回变量后i将超出范围,因此它将永远不会 完全增加. exp in: 10-> out 10

This method will take a int and return it first and then try to increment but as after returning the variable i will go out of scope so it will never be incremented at all. exp in: 10-> out 10

方法:sample2():

method: sample2() :

private static int sample2(int j) {
    return ++j;
}

此方法将获取一个int并先对其进行递增,然后将其返回. exp in: 10-> out 11

This method will take a int and increment it first and then return it. exp in: 10-> out 11

在两种情况下,仅变量将在本地更改,这意味着,如果您从main方法调用,则main方法的变量将不受更改的影响 (因为sample1()和sample2()正在复制变量)

In both case only the variables will change locally, that means if you call from main method the variables of main method will remain unaffected by the change (as the sample1() and sample2() are making copy of the variables)

现在是主要方法的代码

System.out.println(sample1(i++)); // it's giving sample1() `i=0` then making `i=1` 
                                  //  so  sample1() will return 0 too;

System.out.println(sample1(++i)); // it's making `i=2` and then giving sample1() `i=2`
                                  // so sample1() will return 2;

System.out.println(sample2(j++)); // it's giving sample2() `j=0` then making `j=1` 
                                  // so sample2() will return 1;

System.out.println(sample2(++j)); // it's making `j=2` giving sample2() `j=2` then  
                                  // so sample2() will return 3;

这篇关于Java增量运算符查询(++ i和i ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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