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

Oscar Gomez :

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

tskuzzy :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

JAVA calling a method using a ternary operator

From Dev

Using ternary operator in build method

From Dev

Assign null to decimal using ternary operator

From Dev

Assign value in ternary operator

From Java

Java: Ternary with no return. (For method calling)

From Dev

Ethics and readability of using ternary operator for method calls

From Dev

Is there a way to use a ternary operator - or similar method - for picking the variable to assign to?

From Java

Passing method argument through ternary operator in java

From Dev

The ternary operator in Java Linked list Search method

From Dev

Why can't I assign lambda to the variable using ternary operator

From Dev

How can I assign a Func<> using the conditional ternary operator?

From Dev

Angular 2 assign a click function using a ternary operator

From Java

Strange behavior using Java ternary operator on Android

From Java

Weird behaviour when using Java ternary operator

From Dev

Android Databinding : ConcurrentModificationException while using ternary operator in method parameter

From Java

Why is the Ternary operator not working inside a method argument in java

From Java

Why doesn't this method work? Java ternary operator

From Dev

OR in ternary operator, using &&

From Dev

Using ternary operator in echo?

From Dev

Using OR in ternary operator

From Java

Java ternary operator not working?

From Java

java ternary operator

From Dev

Ternary operator syntax (Java)

From Dev

Java Ternary operator syntax

From Java

Ternary operator confusion in Java

From Dev

Java ternary operator and NullPointerException

From Dev

Ternary Operator in Java

From Dev

Is there a ternary assignment operator in Java?

From Dev

Is the ternary operator ?: defined as a method in Ruby?

Related Related

  1. 1

    JAVA calling a method using a ternary operator

  2. 2

    Using ternary operator in build method

  3. 3

    Assign null to decimal using ternary operator

  4. 4

    Assign value in ternary operator

  5. 5

    Java: Ternary with no return. (For method calling)

  6. 6

    Ethics and readability of using ternary operator for method calls

  7. 7

    Is there a way to use a ternary operator - or similar method - for picking the variable to assign to?

  8. 8

    Passing method argument through ternary operator in java

  9. 9

    The ternary operator in Java Linked list Search method

  10. 10

    Why can't I assign lambda to the variable using ternary operator

  11. 11

    How can I assign a Func<> using the conditional ternary operator?

  12. 12

    Angular 2 assign a click function using a ternary operator

  13. 13

    Strange behavior using Java ternary operator on Android

  14. 14

    Weird behaviour when using Java ternary operator

  15. 15

    Android Databinding : ConcurrentModificationException while using ternary operator in method parameter

  16. 16

    Why is the Ternary operator not working inside a method argument in java

  17. 17

    Why doesn't this method work? Java ternary operator

  18. 18

    OR in ternary operator, using &&

  19. 19

    Using ternary operator in echo?

  20. 20

    Using OR in ternary operator

  21. 21

    Java ternary operator not working?

  22. 22

    java ternary operator

  23. 23

    Ternary operator syntax (Java)

  24. 24

    Java Ternary operator syntax

  25. 25

    Ternary operator confusion in Java

  26. 26

    Java ternary operator and NullPointerException

  27. 27

    Ternary Operator in Java

  28. 28

    Is there a ternary assignment operator in Java?

  29. 29

    Is the ternary operator ?: defined as a method in Ruby?

HotTag

Archive