开发者

Java calling method and using ternary operator and assign in the parameters?

I was reviewing some code and I came across this:

开发者_JAVA技巧
 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?.


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


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


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


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.


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)


Assign into variable from ternary operator like so:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜