Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Tricky question.... plz help

Poonam Gupta
Greenhorn
Posts: 4
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

Roger Chung-Wee
Ranch Hand
Posts: 1683
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.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
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

double c='A';
c = c / a;

to get the result you expect.

Megs Maquito
Ranch Hand
Posts: 84
Maybe you should initialize 'A' as a double also.

Rahul Juneja
Ranch Hand
Posts: 425
Poonam,

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

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24211
35
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.

Poonam Gupta
Greenhorn
Posts: 4
Thanks for replying to my question
Im having a great time learning java

Ranch Hand
Posts: 65
Hi ,

If you want the result to be 6.5

double c=(double ) 'A' /a;

Here ( double ) do's the magic for you.

Archies Gomes
Ranch Hand
Posts: 34
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!!!.

Ranch Hand
Posts: 245
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.

Sonny Gill
Ranch Hand
Posts: 1211
Ah..let ME tell you what REALLY happens...

Poonam Gupta
Greenhorn
Posts: 4

Rahul Juneja
Ranch Hand
Posts: 425
So Poonam,

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

Cheers,
Rahul

Poonam Gupta
Greenhorn
Posts: 4
Hi Rahul,
G8, Thanks for ur concern. I'll definately come up with more question
Thank u
Bye