Java calling method and using ternary operator and

2019-06-24 22:17发布

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

6条回答
We Are One
2楼-- · 2019-06-24 22:22
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.

查看更多
Fickle 薄情
3楼-- · 2019-06-24 22:25

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
查看更多
祖国的老花朵
4楼-- · 2019-06-24 22:27

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)
查看更多
霸刀☆藐视天下
5楼-- · 2019-06-24 22:37

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) 
}
查看更多
霸刀☆藐视天下
6楼-- · 2019-06-24 22:45

Assign into variable from ternary operator like so:

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

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

查看更多
Melony?
7楼-- · 2019-06-24 22:47

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);
}
查看更多
登录 后发表回答