-1

Let's assume x is the divisor and it may possibly by 0.

The program runs this:

Scanner reader = new Scanner(System.in);

int y = 5;
int x = 0;

x = reader.nextInt();

System.out.println("y divided by x is " + y/x);

I know that if a program finds out that it it's dividing a variable by 0 it'll throw an ArithmeticException.

While that's fine, there are alternatives to finding out such as using an if statement to check if x is 0 before doing any dividing or doing a try/catch block.

What's the most efficient way of making sure the program doesn't divide a variable by 0?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Angelo Rivera
  • 121
  • 1
  • 1
  • 11

2 Answers2

1

Use assertion statement:

assert x != 0; 

But they should not be used in production code.
Note: You need to run the code with -ea argument to activate it at runtime. They are by default ignored.

You can certainly replace it with an if-else block, and what would certainly not affect much the efficiency of your code. In fact, you should not bother about code efficiency at such minute level.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    Is this the most efficient? Also, the question is stupid. – Sotirios Delimanolis Jul 16 '13 at 20:04
  • @SotiriosDelimanolis. Not sure about efficiency. I'm not an expert in that. but certainly this should be the preferred way. – Rohit Jain Jul 16 '13 at 20:06
  • -1: Not really, I will never use this in production code (as you state), so this won't save you from having this scenario in production =\ – Luiggi Mendoza Jul 16 '13 at 20:21
  • @LuiggiMendoza. Still I don't understand the downvote? I clearly stated that thing in my answer, and also the aternative? – Rohit Jain Jul 16 '13 at 20:22
  • @LuiggiMendoza. Please add a more efficient way than an `if-else` block, if you know one. – Rohit Jain Jul 16 '13 at 20:25
  • *What's the most efficient way of making sure the program doesn't divide a variable by 0?* The assert won't save you from it. Also, having lot of `if-else` in your code isn't the most efficient solution as well. Just catch the `ArithmeticException` and log it, probably with a WARN log level. – Luiggi Mendoza Jul 16 '13 at 20:25
  • @LuiggiMendoza. Really? Adding an `if-else` block will affect the efficieny by that difference? I don't think that would really matter much. And that is why my last statement in the answer. – Rohit Jain Jul 16 '13 at 20:28
  • @RohitJain if is a basic application with a single division, it could be accepted. If it's a finantial application when division is widely used among different formula operations (and having each on different methods), then intensive use of `if-else` is not the way to go. – Luiggi Mendoza Jul 16 '13 at 20:29
  • @LuiggiMendoza. Convinced :) – Rohit Jain Jul 16 '13 at 20:33
0

System.out.println("y divided by x is " + (x == 0 ? "infinity" : y/x));

edit: I don't know if this is the most efficient way but honestly looking at efficiency of something this simple would be completely unreasonable.

ug_
  • 11,267
  • 2
  • 35
  • 52