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

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.

Cheers,

Rahul

Rahul Juneja

ThoughtClicks - http://techlabs.thoughtclicks.com

Originally posted by Rahul Juneja:

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.

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.

The future is here. It's just not evenly distributed yet. - William Gibson

Sonny Gill LinkedIn

Tweets

How are you doing in Learning Java now. You are welcome with more questions.

Cheers,

Rahul

Rahul Juneja

ThoughtClicks - http://techlabs.thoughtclicks.com