Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

kumar abhay
Ranch Hand
Posts: 53
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

Dave Vick
Ranch Hand
Posts: 3244

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

 It is sorta covered in the JavaRanch Style Guide.