This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what's the narrowest valid return type for the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what Watch "what New topic
Author

what's the narrowest valid return type for the method

Essam AbdelAziz
Ranch Hand

Joined: Mar 10, 2007
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
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
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 ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Essam AbdelAziz
Ranch Hand

Joined: Mar 10, 2007
Posts: 34
sorry it was aritten misatke it supposed to be (b)
but the question now?
why double not any thing else
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
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

Joined: Mar 10, 2007
Posts: 34
could you please clarify more

Thank you
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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.

Please clarify that,

Thanks in advance,

Regards,
cmbhatt


cmbhatt
Pratik Patel
Greenhorn

Joined: Mar 20, 2007
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

Joined: Feb 28, 2007
Posts: 1707
Thanks Pratik for pointing out the real concern.






Thanks and Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what's the narrowest valid return type for the method
 
Similar Threads
return-type problem in code - help required
Constructor