Java calling method and using ternary operator and assignment in parameters?

I was looking at the 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 does it work exactly? I know that the resulting .toLowerCase string is assigned to myString (yes, I know the bad practice, since you should not reassign the parameters of the method, in fact they should be final), but I'm not quite sure how the method always gets 2 parameters that it are needed.

I know how this works when myString is null, or at least I think I have, since ternary has myString, null, but I'm not quite sure why it will go there when myString is not zero?

+4
source share
6 answers

Brackets for salvation!

 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 that it assigns
+5
source

doSomething takes 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 .

This may be clearer as described below:

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

Its just a more complicated version:

 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
source
 myString = myString != null ? myString.toLowerCase(): myString 

This piece of code reassigns myString as myString.toLowerCase (), or does not reassign it. But the action of using the assignment operator returns the value that was assigned, so you essentially call this:

 //if myString != null doSomething(myString.toLowerCase(), null); //or if myString is null doSomething(myString /*which is null*/, null); 

It should also be noted that strings are immutable, and changing the value of myString to doAnotherThing (String) will not affect the string that was passed to the method.

+1
source

The code is confusing, but I'm not sure what the problem is. The result of the assignment is the assigned value.

it

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

coincides with

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

Assign a variable from the ternary operator as follows:

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

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

0
source

All Articles