Java calling method and using ternary operator and

2019-06-24 22:07发布

问题:

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

回答1:

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


回答2:

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) 
}


回答3:

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);
}


回答4:

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.



回答5:

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)


回答6:

Assign into variable from ternary operator like so:

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

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