在Java中,将一个值分配给println()函数内的变量是否有效?

huangapple go评论70阅读模式
英文:

Is it valid to assign a value to a variable inside println() function in Java?

问题

以下是翻译好的部分:

我是一个对Java和编程都很新的人,我正在尝试这段代码:

public class Main {
    public static void main(String[] args){
        int x = 10;
        System.out.println(x = 1);
    }
}

它编译通过并打印出了 "1",但是我的IntelliJ提示第一个 x 变量从未被访问过,第二个 x 是多余的。一旦我移除了第一个 x 变量:

public class Main {
    public static void main(String[] args){
        System.out.println(x = 1);
    }
}

这显然是不正确的,但根据我的IDE,int x = 10; 从未被使用过。看起来这个语句的唯一用途是提供了一个 x 的声明。我是对的吗?在 System.out.println() 中赋值给变量的操作有可能吗?

英文:

I am a new guy to Java and programming and I am trying this piece of code:

public class Main {
    public static void main(String[] args){
        int x = 10;
        System.out.println(x = 1);
    }
}

It did compile and print out "1", but my IntelliJ said that the first x variable is never been accessed and the second x is redundant. Once I remove the first x variable:

public class Main {
    public static void main(String[] args){
        System.out.println(x = 1);
    }
}

This is surely incorrect but according to my IDE the int x = 10; was never been used. It seems that the only usage of this statement is to provide a declaration of x*. Am I right? Is it ever possible to assign a value to a variable in System.out.println()?

答案1

得分: 2

首先:不要这样做。在打印语句中修改变量违反了最小惊讶原则

你的第一个例子确实是合法的Java代码。问题在于你搞混了Intellij用于生成关于未使用变量警告的代码。这并不完全令人惊讶...考虑到你的代码在做的事情是没有任何程序员应该考虑的(在我看来!)。这是一个不常见的Intellij bug。

(或者也许不是一个bug。毕竟,变量在赋值后从未被引用过。尽管赋值给变量的值被使用了。无论哪种方式,你做了一些奇怪的事情(在我看来),然后收到了一个警告。解决方法:不要这样做。)

第二个例子确实不是合法的Java代码。x变量必须要声明。


> 这个语句似乎唯一的用途就是声明 x。我理解对了吗?

没错。

> 有可能在 System.out.println() 中赋值给变量吗?

是的。这就是第一个例子所做的。你看到的是一个警告,而不是一个错误。Intellij的Java编译器实际上在生成字节码...这些字节码会运行并且表现出人们所期望的行为。(当然除了在第一个例子中看到在打印调用中出现赋值表达式的完全意外情况!)

英文:

First of all: Don't do this. Modifying a variable in a print statement violates the principle of least surprise.

You first example is indeed legal Java code. The issue is that you have managed to confuse Intellij's code for generating warnings about unused variables. Not entirely surprising ... given that what you code is doing is something that no programmer ought to contemplate doing (IMO!). This is an obscure Intellij bug.

<sup>(Or maybe not a bug. After all, the variable is never referred to after the assignment. Even though the value that was written into the variable is being used. Either way, you did something bizarre (IMO) and got a warning. Solution: don't do it.)</sup>

The second example is indeed not legal Java code. The x variable must be declared.


> It seems that the only usage of this statement is to provide a declaration of x. Am I right?

Correct.

> Is is ever possible to assign a value to a variable in System.out.println()?

Yes. That's what the first example does. What you are seeing is Warning, not an Error. Intellij's Java compiler is actually generating bytecodes ... which run and behave as one would expect them to. (Modulo the total unexpectedness of seeing an assignment expression in a print call in the first place!)

答案2

得分: 1

以下是翻译好的部分:

有多种方法可以调试这个问题。
我建议在分配新值之前打印出 x 的值。这是你可以使用现有值 x 的方式。在使用后,你可以给 x 分配新的值。

int x = 10;
System.out.println("分配新值之前的 x 值: " + x + "  分配新值后的 x 值: " + (x = 1));

输出:

分配新值之前的 x 值: 10 分配新值后的 x 值: 1。

英文:

There are multiple ways to debug this.
I would recommend to print value of x before assigning new values. This is how you can use the existing value of x. After using it you can assign new value to x.

int x = 10;
System.out.println(&quot;x before assigning new Value :  &quot;+x+&quot;  And X after assigning new Value : &quot;+(x = 1));

Output :

x before assigning new Value :  10  And X after assigning new Value : 1.

答案3

得分: 1

你的第一个示例打印出 1,是因为赋值操作 x = 1 实际上会返回值 1。IntelliJ 认为你没有访问到 x 的值,因为实际上你只是访问了 x = 1 的返回值。

是的,我认为写 System.out.println(x = 1) 会将值 1 赋给 x。然而,正如其他答案所述,不建议这样做。

英文:

Your first example printed 1 because the assignment operation x = 1 actually returns the value of 1. IntelliJ thinks that you didn't access the value of x because you didn't; you only accessed the return value of x = 1.

Yes, I think writing System.out.println(x = 1) will assign the value 1 to x. However, as other answers have stated, don't do this.

答案4

得分: 0

我还没有在IntelliJ中检查过您的第一个示例是否有警告,但是您的第二个示例无法编译,也不应该编译,因为x从未声明过。

您的第一个示例确实"访问"了x,如果您将代码修改为:

public class Main {
    public static void main(String[] args){
        int x = 10;
        System.out.println(x = 1);
        System.out.println(x);
    }
}

IntelliJ可能会警告您该结构是"无用的",因为x的初始值从未被使用过,而"正确"的等效代码应为:

public class Main {
    public static void main(String[] args){
        System.out.println(10);
    }
}

至于是否应该在println语句内赋值给变量,这是一个观点问题,但我认为一般来说,Java程序员会认为不应该这样做。这是"合法的",即可以编译,但它是一个意外的结构,因此在阅读代码的人可能会轻易忽略它。

英文:

I haven't checked your first example for warnings in IntelliJ, but your second example does not compile, nor should it, because x is never declared.

Your first example does indeed "access" x, as you will see if you modify your code to:

public class Main {
    public static void main(String[] args){
        int x = 10;
        System.out.println(x = 1);
        System.out.println(x);
    }
}

IntelliJ is probably warning you that your construct is "useless", since the initial value of x is never used, and the "correct" equivalent code would be:

public class Main {
    public static void main(String[] args){
        System.out.println(10);
    }
}

As far as whether you should assign to a variable inside a println statement, this is an opinion, but I think that Java programmers in general would agree that you should not. It's "legal", i.e. will compile, but it is an unexpected construct, so might easily be missed by someone reading the code.

huangapple
  • 本文由 发表于 2020年9月4日 11:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63734606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定