# what's the narrowest valid return type for the method

Essam AbdelAziz
Ranch Hand
Posts: 34
Dear All ,can you help me to solve this

public class Test{

returnType method(byte x,double b){
return (long)x/y*2

}

}

what's the narrowest valid return type for the method

thanks

Ranch Hand
Posts: 1585
Hi,

In the statement:

It must be "b" instead of "y" right ? If so, the the return type can only be double or Double.

If it's "y", please specify its type.

Best of luck ...

Essam AbdelAziz
Ranch Hand
Posts: 34
sorry it was aritten misatke it supposed to be (b)
but the question now?
why double not any thing else

Ranch Hand
Posts: 1585
Because it's returning a double, only a double or a Double (as a narrowest) can be defined as a return type.

A long can't fit, because there will be a lost of precision.

As a matter of fact, you can define a Number or an Object as the return type.

Good luck ...

Essam AbdelAziz
Ranch Hand
Posts: 34

Thank you

Chandra Bhatt
Ranch Hand
Posts: 1710

public class Test{

returnType method(byte x,double b){
return (long)x/y*2

}

}

Hi Balidimir,
Here we did explicit casting from double to long that is ok, so why can't we return long. An we see long is returned after casting double to long, so why can't return type of the method be long.

Regards,
cmbhatt

Pratik Patel
Greenhorn
Posts: 9
Qoute:Here we did explicit casting from double to long that is ok, so why can't we return long.

You didnt cast the value of an expression to long. You just type casted the value of varialbe x. So finally the value of whole expression will be promoted to double since y is of type double.

If you want to cast the value of expression to long you should do it in the following way.

(long)(expression)... (long)(x*y/2).
Then you can return the long type from method.

Pratik

Chandra Bhatt
Ranch Hand
Posts: 1710
Thanks Pratik for pointing out the real concern.

Thanks and Regards,
cmbhatt