wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes asking about java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "asking about java" Watch "asking about java" New topic
Author

asking about java

kumar abhay
Ranch Hand

Joined: Oct 02, 2001
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

Joined: May 10, 2001
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


Dave
 
Don't get me started about those stupid light bulbs.
 
subject: asking about java
 
Similar Threads
Sign extension doubt?
Negation of Integers
asking about ++ operator
Asking about function of default in switch
Some Codes