Java调用方法并使用三元运算符并在参数中赋值?

发布于 2024-11-17 14:12:54 字数 511 浏览 3 评论 0原文

我正在审查一些代码,我遇到了这个:

 public static doSomething(String myString, String myString2) {
        //Stuff
 }

 public static doAnotherThing(String myString) {
      return doSomething(myString = myString != null ? myString.toLowerCase(): myString, null)
 }

这到底是如何工作的?,我知道 .toLowerCase 结果字符串被分配给 myString (是的,我知道不好的做法,因为你不应该重新分配方法参数,事实上它们应该是最终的),但我不太确定该方法如何总是接收它需要的 2 个参数。

我知道当 myString 为 null 时它是如何工作的,或者至少我认为我知道,因为三元组有 myString, null,但我不太确定为什么当 myString 不为 null 时它会去那里?

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.

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?.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

非要怀念 2024-11-24 14:12:54

括号来拯救!

doSomething(myString = ( ( myString != null ) ? myString.toLowerCase() : myString ), null)

要理解这一点,您需要了解两件事:

  • 三元运算符如何工作
  • 赋值运算符返回它正在分配的内容这一事实

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
痕至 2024-11-24 14:12:54

它只是一个更复杂的版本:

public static doAnotherThing(String myString) 
{
  myString = myString != null ? myString.toLowerCase(): myString;
  return doSomething(myString, null) 
}

或者甚至

public static doAnotherThing(String myString) 
{
  String s = myString;
  if (myString != null)
      s = myString.toLowerCase();
  return doSomething(s, null) 
}

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) 
}
花想c 2024-11-24 14:12:54

doSomething 接收两个参数,这两个参数都是字符串。在 doAnotherThing 中:

  • 传递给 doSomething 的第一个参数是:
    • null 如果 myStringnull
    • myString.toLowerCase() 否则。
  • 传递给 doSomething 的第二个参数始终为 null

像这样重写可能会更清楚:

public static doAnotherThing(String myString)
{
    if (myString == null) return doSomething(null, null);
    else return doSomething(myString.toLowerCase(), null);
}

doSomething receives two parameters, both of which are strings. In doAnotherThing:

  • The first parameter passed to doSomething is:
    • null if myString is null,
    • myString.toLowerCase() otherwise.
  • The second parameter passed to doSomething is always 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);
}
已下线请稍等 2024-11-24 14:12:54
myString = myString != null ? myString.toLowerCase(): myString

这段代码将 myString 重新分配为 myString.toLowerCase(),或者不重新分配它。但是使用赋值运算符的行为会返回已分配的值,因此您实际上是在调用它:

//if myString != null
doSomething(myString.toLowerCase(), null);

//or if myString is null
doSomething(myString /*which is null*/, null);

您还应该注意字符串是不可变的,并且更改 doAnotherThing(String) 中 myString 的值不会影响原来的字符串。传递到方法中。

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:

//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.

圈圈圆圆圈圈 2024-11-24 14:12:54

代码很混乱,但我不确定问题是什么。赋值的结果就是赋值的值。

 return doSomething(myString = myString != null ? myString.toLowerCase(): myString, null)

 if(myString != null) myString = myString.toLowerCase();
 return doSomething(myString, null)

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)
我很坚强 2024-11-24 14:12:54

从三元运算符分配到变量,如下所示:

minVal = (a < b) ? a : b;

更多示例:
http://alvinalexander.com/java/edu/pj/pj010018

Assign into variable from ternary operator like so:

minVal = (a < b) ? a : b;

More examples:
http://alvinalexander.com/java/edu/pj/pj010018

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文