为什么Integer.parseInt(" 1")++不能在Java中运行? [英] Why doesn't Integer.parseInt("1")++ work in Java?

查看:150
本文介绍了为什么Integer.parseInt(" 1")++不能在Java中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行:

suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)+1);

在一个块中,后缀已被声明为空字符串( )。该块正在寻找重复的文件名并在任何重复项上添加一个数字,因此它们不再具有相同的名称。

in a block where suffix has already been declared as an empty String (""). The block is looking for duplicate file names and adding a number on to any duplicates so they don't have the same name any more.

上面的代码行编译好了,但如果我将其改为此,

The line of code above compiles fine, but if I change it to this,

suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)++);

我得到操作的参数无效++ / - 。由于 Integer.parseInt()返回和int,为什么我不能使用 ++ 运算符?

I get Invalid argument to operation ++/--. Since Integer.parseInt() returns and int, why can't I use the ++ operator?

推荐答案

++ 运算符应更新其参数的值,因此参数应该在内存中有一个固定的位置进行更新。因此,参数应该是变量*。在这种情况下,参数是 Integer.parseInt(后缀),没有固定的内存地址可供更新。

The ++ operator should update the value of its argument, so the argument should have a fixed position in memory to be updated. For this reason, the argument should be a variable*. In this case, the argument is Integer.parseInt(suffix), has no fixed memory address to be updated at all.

直观地, Integer.parseInt(后缀)++ 大致相当于 Integer.parseInt(suffix)= Integer.parseInt(suffix)+ 1 。但 Integer.parseInt(后缀)只是一个整数值,与内存中的固定位置无关,所以上面的代码几乎是相同的,让我们说, 32 = 32 + 1 。由于您无法为 32 (既不是 Integer.parseInt(后缀))分配新值,所以没有任何意义支持 ++ 运算符。

Intuitively, Integer.parseInt(suffix)++ is roughly equivalent to Integer.parseInt(suffix) = Integer.parseInt(suffix) + 1. But Integer.parseInt(suffix) is just an integer value, not associated to a fixed position in memory, so the code above is almost the same thing of, let us say, 32 = 32 + 1. Since you cannot assign a new value to 32 (neither to Integer.parseInt(suffix)) then there is no sense in supporting the ++ operator.

好消息是这不会导致任何问题!而不是 Integer.parseInt(后缀)++ ,写 Integer.parseInt(后缀)+1

The good news is that this does not cause any problems at all! Instead of Integer.parseInt(suffix)++, write Integer.parseInt(suffix)+1.

*或者,最常见的是, l-value 地址值

* Or, as it is most commonly called, an l-value, or an address value.

这篇关于为什么Integer.parseInt(" 1")++不能在Java中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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