kumar abhay

Ranch Hand

Posts: 53

posted 16 years ago

hi there,

hope u r doin� fine. I have some problem in the coming topic n I hope that u will solve my problem,given below:

1. int I=45;

I=(int)(i/0.0);

//output is given as 214474836 which is the maximum value of the integer

why output is the maximum integer number?

2. byte b=45;

b=(byte)(i/0.0);

// output is given as �1

sir my question is that while applying int output was the maximum value which was given n in the case of byte or short the output is �1.

sir I hope that u will solve it .thank u very much in advance.

take care

with regards

kumar abhay

hope u r doin� fine. I have some problem in the coming topic n I hope that u will solve my problem,given below:

1. int I=45;

I=(int)(i/0.0);

//output is given as 214474836 which is the maximum value of the integer

why output is the maximum integer number?

2. byte b=45;

b=(byte)(i/0.0);

// output is given as �1

sir my question is that while applying int output was the maximum value which was given n in the case of byte or short the output is �1.

sir I hope that u will solve it .thank u very much in advance.

take care

with regards

kumar abhay

Dave Vick

Ranch Hand

Posts: 3244

posted 16 years ago

Kumar

In floating point operations any division of a non-zero finite number by 0 will reult in signed infinity. Then when you cast it to an int you follow the rules of the JLS section 5.1.3 and the result is converted to the maximum value of an int.

In this case the result of the floating point division is the same (positive infinity), however to convert to a byte a two step process is used, first it is converted to an int as the first example was, then the int is converted to a byte. when the int is converted to a byte all but the lowest order bits of the new type (in this case the lowest 8) are discarded, so the remaining bits are all 1 which is -1.

hope that helps

------------------

Dave

Sun Certified Programmer for the Java� 2 Platform

1. int I=45;

I=(int)(i/0.0);

//output is given as 214474836 which is the maximum value of the integer

why output is the maximum integer number?

Kumar

In floating point operations any division of a non-zero finite number by 0 will reult in signed infinity. Then when you cast it to an int you follow the rules of the JLS section 5.1.3 and the result is converted to the maximum value of an int.

2. byte b=45;

b=(byte)(i/0.0);

// output is given as �1

sir my question is that while applying int output was the maximum value which was given n in the case of byte or short the output is �1.

In this case the result of the floating point division is the same (positive infinity), however to convert to a byte a two step process is used, first it is converted to an int as the first example was, then the int is converted to a byte. when the int is converted to a byte all but the lowest order bits of the new type (in this case the lowest 8) are discarded, so the remaining bits are all 1 which is -1.

hope that helps

------------------

Dave

Sun Certified Programmer for the Java� 2 Platform

Dave

It is sorta covered in the JavaRanch Style Guide. |