java Java调用方法和使用三元运算符并在参数中赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6511103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java calling method and using ternary operator and assign in the parameters?
提问by Oscar Gomez
I was reviewing some code and I came across this:
我正在一些代码,我遇到了这个:
public static doSomething(String myString, String myString2) {
//Stuff
}
public static doAnotherThing(String myString) {
return doSomething(myString = myString != null ? myString.toLowerCase(): myString, null)
}
How is this working exactly?, I know the .toLowerCase resulting string is assigned to myString (yes I know bad practice since you are not supposed to reassign method parameters in fact they should be final), but I am not quite sure how does the method always receives the 2 parameters it needs.
这是如何工作的?,我知道 .toLowerCase 结果字符串被分配给 myString(是的,我知道不好的做法,因为你不应该重新分配方法参数,实际上它们应该是最终的),但我不太确定如何方法总是接收它需要的 2 个参数。
I know how it works when myString is null or at least I think I do, since the ternary has myString, null, but I am not quite sure why it would go there when myString is not null?.
我知道当 myString 为 null 时它是如何工作的,或者至少我认为我知道它是如何工作的,因为三元有 myString, null,但我不太确定为什么当 myString 不为 null 时它会去那里?。
回答by tskuzzy
Parenthesis to the rescue!
括号来拯救!
doSomething(myString = ( ( myString != null ) ? myString.toLowerCase() : myString ), null)
To understand this, you need to know two things:
要理解这一点,你需要知道两件事:
- How the ternary operator works
- The fact that the assignment operator returns the thing it is assigning
- 三元运算符的工作原理
- 赋值运算符返回它正在赋值的东西的事实
回答by Matt Ball
doSomething
receives two parameters, both of which are strings. In doAnotherThing
:
doSomething
接收两个参数,都是字符串。在doAnotherThing
:
- The first parameter passed to
doSomething
is:null
ifmyString
isnull
,myString.toLowerCase()
otherwise.
- The second parameter passed to
doSomething
is alwaysnull
.
- 传递给的第一个参数
doSomething
是:null
如果myString
是null
,myString.toLowerCase()
否则。
- 传递给的第二个参数
doSomething
始终是null
。
It might be clearer rewritten like this:
改写成这样可能更清楚:
public static doAnotherThing(String myString)
{
if (myString == null) return doSomething(null, null);
else return doSomething(myString.toLowerCase(), null);
}
回答by John Gardner
Its just a more complicated version of:
它只是一个更复杂的版本:
public static doAnotherThing(String myString)
{
myString = myString != null ? myString.toLowerCase(): myString;
return doSomething(myString, null)
}
or even
甚至
public static doAnotherThing(String myString)
{
String s = myString;
if (myString != null)
s = myString.toLowerCase();
return doSomething(s, null)
}
回答by nicholas.hauschild
myString = myString != null ? myString.toLowerCase(): myString
This piece of code reassigns myString to be either myString.toLowerCase(), or it doesn't reassign it. But the act of using the assignment operator returns the value that was assigned, thus you are essentially calling this:
这段代码将 myString 重新分配为 myString.toLowerCase(),或者不重新分配它。但是使用赋值运算符的行为会返回分配的值,因此您实际上是在调用它:
//if myString != null
doSomething(myString.toLowerCase(), null);
//or if myString is null
doSomething(myString /*which is null*/, null);
You should also note that Strings are immutable, and that changing the value of myString in doAnotherThing(String) will not affect the String that was passed into the method.
您还应该注意字符串是不可变的,并且在 doAnotherThing(String) 中更改 myString 的值不会影响传递给方法的字符串。
回答by Peter Lawrey
The code is confusing, but I am not sure what the problem is. The result of an assignment is the value assigned.
代码令人困惑,但我不确定问题是什么。赋值的结果就是赋值的值。
This
这
return doSomething(myString = myString != null ? myString.toLowerCase(): myString, null)
is the same as
是相同的
if(myString != null) myString = myString.toLowerCase();
return doSomething(myString, null)
回答by sivi
Assign into variable from ternary operator like so:
从三元运算符赋值给变量,如下所示:
minVal = (a < b) ? a : b;
More examples: http://alvinalexander.com/java/edu/pj/pj010018