This week's book giveaways are in the Java EE and JavaScript forums. We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line! See this thread and this one for details.

hi please take a look at the following piece of code class Apps { public static void main(String[] args) { boolean b= 100>99; int s='A'; System.out.println(s);

int a=10; double w=6.5; System.out.println(w); double c='A'/a; System.out.println(c); System.out.println(b); } } the value of c come out to be 6.0 after the code is run.... my question is that the value of c should be 6.5 as the ascii value of 'A' is 65... why is it returning 6.0 instaed of 6.5

I think that what is happening is that 'A' is converted to an int and divided by 10, result is 6. But as the result must be a double, you then get 6.0.

Yes, that's basically it. The result of dividing one integer by another is an integer. It's then converted to a double only because you're storing it in a double variable. You might say

Infact what happens in Java arithmetic calculations is the smaller both the digits which are participating in a calculation are are converted to the datatype with the lower bits.

For example : 1) if u divide Float by int result will be int 2) if u multiply int by double again result will be int.

Infact what happens in Java arithmetic calculations is the smaller both the digits which are participating in a calculation are are converted to the datatype with the lower bits.

Note that float and int are both 32 bits. The rule is more or less the opposite of what you've implied, though: if either argument is a floating-point type, the calculation is done using that type and the result is of that type. The result is an int only if both arguments are of integral type.

For example : 1) if u divide Float by int result will be int

No, it will be float.

2) if u multiply int by double again result will be int.

No, it will be a double.

Folks, please don't post answers to questions as if you know the answer if you're really just guessing. It doesn't help anybody. If you must guess, then please say "I don't know for sure, but I think..." or something like that. Especially given that the first two replies in this thread already answered the question correctly and completely.

Poonam Gupta
Greenhorn

Joined: Jun 02, 2005
Posts: 4

posted

0

Thanks for replying to my question Im having a great time learning java

Hi Poonam What happens in java actually is that Whenever any arithmatic operation is involved such as 'A'/6 then the datatype which takes the largest space for storing bits is taken.....In this case of yours both are integers so the result is integer and it is assigned to a varialbe of type double so the output you get is 6.0.....If you want the correct output ie 6.5 then either of the operand has to be of type double ...so that the other operand is also converted to the same largest type and then the operation takes place....So what basically happens is first the operands are viewed and the one which takes larger space becomes the king and rest all operands are converted to this type. njoi learning!!!.

Originally posted by Mithun H. Salgar: Hi Poonam What happens in java actually is that Whenever any arithmatic operation is involved such as 'A'/6 then the datatype which takes the largest space for storing bits is taken.....

More precisely the operand which type is lower in the following list is converted to the type of the other operand:

If both operands are char, short or byte, they are both converted to int.

(Float has 32 bits but it is considered larger than long with 64bits.)

If both operands of / are integer types, integer division is used. So value of 65/10 is 6.