Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java multiply operation behavior

I wrote a method to convert a given number from days to milliseconds:

private long expireTimeInMilliseconds;
...
public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = expireTimeInDays * 24 * 60 * 60 * 1000;
}

I had a hard time to figure out what I did wrong. Now my question: Is that error so obvious ?

The corrected method:

private long expireTimeInMilliseconds;
...
public void setExpireTimeInDays(int expireTimeInDays)
{
   expireTimeInMilliseconds = ((long) expireTimeInDays) * 24 * 60 * 60 * 1000;
}

If I don't convert the integer to long before calculating, I get a complete wrong result.

like image 906
Gilberto Olimpio Avatar asked Feb 04 '09 17:02

Gilberto Olimpio


People also ask

How do you multiply operators in Java?

You can create longer chains of multiplication using the * operator. Here is an example of multiplying 3 values with each other: int prod = 10 * 20 * 30; Multiplying a variable with a value and assigning the value back to the variable is a common math operation in Java applications.

Can you multiply a character in Java?

No. Java does not have this feature. You'd have to create your String using a StringBuilder, and a loop of some sort. StringUtils has been performance tuned and tested.

What is multiply program in Java?

Multiplication of two integers public class MultiplyExample { public static void main(String[] args) { int a; int b; int c; a = 5; b = 58; c = a*b; //integer number to keep the result of multiplication System. out. println("5*58 = " + c); } } The output is: 5*58 = 290.

Can you multiply two numbers without using operator in Java?

How do I multiply a number without using an operator in Java? You can multiply the number by running the loop of second no times and each time adding the first no to itself. Since multiplication is just a repetitive addition. Also, when we add num1 to itself num2 times then same result will be produced.


2 Answers

Is it obvious? I guess it depends on how long you've been using Java and how many times you've had to deal with milliseconds. Of course, it should be okay for up to about 24 days...

I think the biggest hint should be that System.currentTimeMillis() returns a long. That's a good indication that a number of milliseconds can get big. The type of the variable you're setting should be a good hint too.

Of course, you've also got to know that if you do arithmetic operations with ints, the result will be int with wrap-around on overflow. Whether that's sufficiently obvious or not could be debated, but it would be a pretty pointless discussion. In C# if you turned overflow checking on, you'd have found the bug pretty quickly - but then not many developers do that (indeed, I don't although I probably should).

like image 149
Jon Skeet Avatar answered Oct 15 '22 05:10

Jon Skeet


Yes, it's pretty obvious if you've done it before. Any time you see a string of numbers multiplied out you should automatically start thinking about integer overflow errors. In this case you're set to overflow if expireTimeInDays is more than 24. Technically you should be thinking about overflow errors any time you're working with integers, but multiplying a group of them like this should be a very big red flag.

like image 33
Bill the Lizard Avatar answered Oct 15 '22 05:10

Bill the Lizard