1

I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?

1ac0
  • 2,875
  • 3
  • 33
  • 47
curiousity
  • 4,703
  • 8
  • 39
  • 59
  • 3
    Use `Boolean`, not `boolean`. – Marko Topolnik Feb 10 '14 at 15:13
  • 1
    It's hard to say without more information, but if neither `true` nor `false` apply after your computation, it might make sense to `throw an Exception` rather than return null. – C.B. Feb 10 '14 at 15:15
  • OK, you originally wrote this method to return a `boolean` because you thought that two values would be enough. Now you need a third. Why are you now sure that three will always be enough? Probably best to define an `enum` so you can extend it later if you need to, or rethink your logic. – Ian McLaird Feb 10 '14 at 15:29
  • I have tried Boolean, but I couldn't pass received null as input parameter for other function. Only enum or counter, eh? – curiousity Feb 10 '14 at 15:39
  • 2
    There is nothing 'unclear' about 'three-valued logic'. Come off it guys. Voting to reopen. – user207421 Nov 27 '14 at 21:32

2 Answers2

2

Please use an enumeration with three values defined. Hacking things together is no solution.

Enigma
  • 1,699
  • 10
  • 14
  • I know about enumerators but I suppose there are some ternary-logic( why misusing me? – curiousity Feb 10 '14 at 15:14
  • The problem with returning null is, that no-one knows what it means. You should write documentation, stating that null is a valid return value, or expect applications to throw exceptions when using your function. – Enigma Feb 10 '14 at 15:16
  • I think you mean "enumeration". Enumerators are used for looping over a collection. – Josh Feb 10 '14 at 15:17
  • Or, to use the language keyword, define an `enum`. – Ian McLaird Feb 10 '14 at 15:26
-1

Similar question has been asked, it should help.

You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.

Generaly, don't use null values as state indicators.

Community
  • 1
  • 1
1ac0
  • 2,875
  • 3
  • 33
  • 47