Parameters and Arguments
We have learned how methods can return a value, but you can also send values to methods. To do this, methods use a parameter list. Let’s use our “MyMethod” program and add a parameter to our method “aCoolMethod”.
public class MyMethod { staticvoid
aCoolMethod(int theMethodsInt
) { System.out.println("I am a method.");System.out.println("You gave me a value of:"); System.out.println(theMethodsInt);
} public static void main(String[] args) {aCoolMethod(3);
} }
I changed the method back to type void because it is not returning anything.
You can see the round brackets after the method’s name. This space is for the method’s parameter list. The parameter list, is a list of variables that the method uses to store values that we pass into it.
Down in the main part of our program, where we made a call to the method, we supplied a value of 3. This value is called the argument.
Once a method has an item in its parameter list, it expects to receive a value when it is called. If you do not supply the value, you will get an error at compile time.
A method can have as many items in its parameter list as you want. But each item must be separated by a comma.
Let’s change our method to take an additional int value. And while we are at it, let’s change our method type again to return a value. That way our method is doing something a little more interesting than nothing at all.
public class MyMethod { staticint
aCoolMethod(int int1, int int2
) { return (int1 + int2); } public static void main(String[] args) {int myNeatInt = aCoolMethod(5, 10); System.out.println(myNeatInt);
} }
You can see how when you make a method call, the number of supplied argument values must match the number of parameters in the parameter list. And the supplied value types must match the parameter types.
Well, actually, there is a slight loophole to that last rule. And it goes back to the lesson on type casting. Consider this example:
public class MyMethod {
static int aCoolMethod(int int1, int int2) {
return (int1 + int2);
}
public static void main(String[] args) {
byte b = 5;
int myNeatInt = aCoolMethod(b, 10);
System.out.println(myNeatInt);
}
}
You can see how aCoolMethod is expecting two int values. However, when I made the method call, I sent the value from b, which is of type byte.
Thanks to the magic of type casting, the byte value was automatically upgraded to type int, and successfully stored in aCoolMethod’s variable int1.
You can also do a narrow type cast, but you will have to do that manually. Like this:
public class MyMethod { static int aCoolMethod(int int1, int int2) { return (int1 + int2); } public static void main(String[] args) {double d = 5.4321;
int myNeatInt = aCoolMethod((int)d
, 10); System.out.println(myNeatInt); } }
As expected, data loss occured. When 5.4321 was converted to an int, its value was cut to 5.
Challenge:
The following code example contains an error. Can you spot the problem and debug it?
public class MyMethod { static void aCoolMethod(double d1, double d2) { return (d1 + d2); } public static void main(String[] args) {double myNeatDouble = aCoolMethod(3.1, 10);
System.out.println(myNeatDouble);
} }
Comments
Post a Comment