Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

java bug ?

  Asked By: Hamish    Date: Aug 24    Category: Java    Views: 780
  

i am a newbie in java and i was trying out this program to print a
value of type long.


class multiply_bug
{
public static void main(String args[])
{

long distance;
distance=1000*186000*24*60*60;
System.out.println(distance);

}
}


the result i got is :-1367621632

whereas the correct value is : 16070400000000


i then tried out the example given in the book as is :



class multiply
{
public static void main(String args[])
{

long days,distance,seconds;
int speed;

speed=186000;
days=1000;

seconds=days*24*60*60;
distance=seconds*speed;

System.out.println(distance);

}
}

and i get the correct output


why does this happen .when i assign values to variables and then
multiply it works fine , but if i specify the values directly it
prints out some junk as we saw in the above.

i would appericiate your comments on this.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Julian Long     Answered On: Aug 24

You are experiencing an overflow error. The number in the
multiplication is larger than the maximum a normal int  can hold. It
goes negative due to 2's complement notation. By splitting the
multiplications, you keep the products in an int's range until it can
be stored in a long, at which point the maximum is bumped up.

 
Answer #2    Answered By: Omar Walker     Answered On: Aug 24

This does not happen because you assigned the values  to variables  and
used them, but rather than what operations you are doing on what data
types. In your first program, distance  is declared as long  whereas the
numbers 1000, 186000, 24, 60 and 60 are of type  int. Now int  in Java is
always a signed 32-bit integer value. I assume you know how signed and
unsigned int works. Basically numbers that start with a 0 are positive
and with a 1 are negative. I am not going to go into binary arithmetic
here, but when this line is executed: distance=1000*186000*24*60*60; the
calculation returns a binary value with leading 1's. ( 111010011101
10101110011110111100000000000000 to be exact, I have included the split
to show the overflow of the 32 bits allowed. When you do a 2's
complement to the existing 32-bits, which is complementing each bit and
adding a 1 to the result, you get 1010001100001000100000000000000, which
is, voila, you guessed it : 1367621632 with a negative sign in front of
it.) The program, while using it in binary form, simply represents it in
negative form because of the leading 1's. However, in your second
program, you are breaking up your calculations into manageable chunks.
So the line seconds=days*24*60*60; returns
00000101001001100101110000000000, which is positive and accepted. And
since distance and seconds  are of type long, an automatic conversion
occurs and the resultant answer, which is
00000000000000000000111010011101 10101110011110111100000000000000, is
positive and you get your correct  answer.
Also, this situation is not just for Java but for any programming
language and is a serious warning of what will happen if you don't mind
your data types. I seriously hope this made this situation a lot more
clearer. :-P Just kidding. Have fun.

 
Didn't find what you were looking for? Find more on java bug ? Or get search suggestion and latest updates.




Tagged: