When calling a method, you must pass in the expected values as arguments to the method.
The expected values are defined by the signature of the method.
Remember, when calling the method, ou are NOT declaring the arguments.
Many methods expect arguments to be passed to them in order to process different values. If the method expects a certain number of arguments, you must pass them by listing them inside the parentheses that come after the method name in the method call. Each argument that you pass in the method call must match the expected data type, which is specified in a method's signature.
You can pass constants, primitives, expressions, or objects, as long as the data type of the value you pass matches what the method expects. For instance, the println() method expects one argument and it must be a String. Let's look at example code using the println() method that demonstrates this. Remember, println() expects one argument and it must be a string.
Argument Passing to println()
String day = "Mon";
int dayNum = 1;
// "day" is a string constant
System.out.println("day"); // prints the string day to the screen
// day is an object of class String
System.out.println(day); // prints the string Mon to the screen
// day + "day" is an expression using concatenation. The result is a string.
System.out.println(day + "day"); // prints the string Monday to the screen
// dayNum is an int. It is converted to a string and then passed
System.out.println(dayNum); // prints the string 1 to the screen
// dayNum + 1 is an expression using concatenation. The result is a string.
System.out.println(dayNum + 1); // prints the string 11 to the screen
Because printing is such a basic operation, the Java compile automatically converts the objects to strings, but no other methods will convert arguments. You must only pass arguments of the correct type. If you do not pass the correct data type, you will get errors from the Java compiler.
Error- Passing a String Argument when an int is expected
Here is a code snippet that demonstrates the Point class:
Point p1;
p1 = new Point();
System.out.println("The Point p1 is: " + p1);
p1.setLocation(10, 200);
System.out.println("The Point p1 is now: " + p1);
We will add a call to setLocation() and pass a string for the first argument, instead of the integer that the method expects.
p1.setLocation("10", 200);
When we compile this code, we will get an error message like this:
Incompatible type for method. Can't convert java.lang.String to int. p1.setLocation("10", 300); ^ 1 error
As we look at more methods, remember that when you call a method, you must pass it the correct number of arguments and they must be the expected data type.
Part of understanding methods requires that you understand what the method manipulates and what processing it will do. A method will either return an answer or return nothing at all. This is determined by what the method does and is called the return value for the method.
For instance, the setLocation() method takes two arguments as x and y coordinated and changes the object used in the dot-notation. It does not have an answer to return, so doesn't return one. On the other hand, the parseDouble() method takes a single String argument, converts it to a double, and returns the answer. Like argument types, the return type for a method is also specified in a method's signature.