Who Knows All.. ?

If the answer is No one. Then this blog is for U. I'm going to put all the things I know in Java

Questions based on Return type and Return values in Java

Q: Can a method declare multiple return values? Or is there some way to return more than one return value?
Ans:           Sort of.A method can return only one return value. But.. if you wanna return, say return three int values, then the declared return type can be an int array. Stuff those ints into an array, and pass it on back.
But you can’t  return, let say an int and String for the same method.
Q: Does a method have to return the exact type it declared?
Ans:           A method can return anything that can be implicitly promoted to that type. So, you can pass a byte where an int is expected. The caller wont care, because the byte just fits fine into the int the caller will use for assigning the result. But you must use an explicit cast when the desired type is smaller than what you are trying to return.
Q: Do I have to do something with the return value of a method? Can I just ignore it?

Ans:          Java doesn’t require you to acknowledge a return value.You might want to call a method with a non-void return type,even though you don’t have care about return value. In this case you are calling the method for the work it has done inside the method, rather than what the method gives returns. In java you don’t have to assign or use the return value,but having said that if you really want to, you can assign a return value to that type of variable. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


This entry was posted on December 18, 2011 by in Hr, Interview info, Java return types, P.I, return values.
%d bloggers like this: