Asked • 03/14/19

How to create a method to return 1 or 0 without using conditions?

I was asked a question in an interview to return 1 if provided 0 and return 0 if provided 1 without using conditions i.e if, ternary etc Just to give you and idea below code without if's: public int testMethod(int value){ if(value==0) return 1; if(value==1) return 0; return 0; } [`Java Fiddle`](http://www.tutorialspoint.com/compile_java8_online.php?PID=0Bw_CjBb95KQMbFJMXzlmZi1KZUk) **UPDATE:** Though @Usagi's Answer may seem the best fit with respect to the code I wrote .. but re-considering the question I re-analyzed the answers .. and @Sergio's answer seems the simplest and best fit ..

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.