From the course: Oracle Java Certification: 3. Methods and Inheritance

Arguments and return values of methods - Java Tutorial

From the course: Oracle Java Certification: 3. Methods and Inheritance

Start my 1-month free trial

Arguments and return values of methods

- [Instructor] A method in Java defines a procedure or a function. It creates a reusable block of code that can be called from other parts of the program. In Java, a method is always associated with an object. Given a reference to the object, a method can be called if the method is visible or accessible. This visibility is also known as the scope of the method definition. A method can also take arguments or parameters from the caller as input. Because Java is a strongly-typed language the types of the actual parameters we pass into a method must be assignable to the parameter types in the method definition. Since Java 5, Java method can also take a variable number of arguments. Let's look an example. What do you think this program would output? At line two we define a print method that takes a string object followed by a number of primitive integers. In the body of the method we print the label, and all the integers we get as input. The first print method call at line 12 should work, because the method it declared to take any number of integers. The integer arguments one, two, three, and four, will get packaged into a integer array. The next call at line 13 should also work, because the integer array is passed in. This is an alternative syntax we can use. Please note that the array is declared and initialized with anonymous initializer at the same time, which is perfectly fine. What about line 14? It is essentially the same as line 13, except the array is defined in another place. So it should work as well. Now let's look at line 15. It does look similar to line 14 without defining a new variable. The anonymous initializer is used directly as the argument. Well it turns out that this method call won't compile at all. It is because the anonymous initializer doesn't provide enough type information. Now let's run the program to see what we can get. As expected we get a error message from line 15 because the method call has a syntax error in it. But if we comment out this line and rerun this program, we can see that the first three method calls are all valid and they produce the same output. Another important rule to remember about variable arguments is that each method can have at most one variable-length argument, and it must be defined as the last thing in the parameter list. Let's look at another example. Here we have four methods defined, and they're called from the Main method. Are the method definitions valid? What would this program output? The first two methods declare to return nothing. And they don't return anything. Therefore they are valid. Note that the return statement is optional in a method with a void return type. The third method at line eight is problematic. In fact it won't compile, because it declares to return a float object, but it actually returns a double floating-point type. Even though they're both floating-point types, a double floating-point type is not assignable to a float object type. The method at line 12 fixes the problem by defining the literal value as a single-precision floating-point type. Let's run this program to see what we can get. So as expected, we get the compiler error, and the message clearly tells us the problem.

Contents