Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Declaration confusion!!!

 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a method title:
Public double method1(int) // does it convert int to double b4 method returns ?
Public int method(double) //does it cast double into int b4 method returns
will one of these methods yield an error?
Thanks in Advance!
 
Janna Lockhart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method does not necessarily return the same variable that it accepts. A method could look like this:
public String traverseStringArray(int i)
{
String [] names = {"Bob", "Jim", "Craig"};
if (i < names.length())
return names[i];
else
return String fail = "Sorry, there's no one here by that identification number!";
}
If you were assuming when you asked the question that you were returning the value you handed it, I believe you could do that without an error because it would cast for you.

[This message has been edited by Janna Lockhart (edited April 10, 2001).]
 
Buburub
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both of cases of you explanation you are returning String which is the return type of the method. I'm asking
public int method1(x)
if x is int type, //then there is no prob
if x is smaller than int //is there conversion? or error?
if x is bigger than int //is there casting? or error?
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buburub,
I hate to say this but, your name does not comply with the Javaranch naming guidelines which can be found at http://www.javaranch.com/name.jsp
please register again with a valid name.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Buburub:
When you have a method title:
Public double method1(int) // does it convert int to double b4 method returns ?
Public int method(double) //does it cast double into int b4 method returns
will one of these methods yield an error?
Thanks in Advance!


First, why do you think it would need to convert?
Second, why do you think either would give an error? (I mean other than the fact that "p" in "public" shouldn;t be capitalized.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic