Conteúdo do Curso
Java Extended
Java Extended
Creating Our Own Methods
How to use methods?
In the previous chapter, we looked at creating a method that adds two numbers. Now let's see how we can actually call this method in our code:
Main
package com.example; public class Main { public static void main(String[] args) { int a = 10; int b = 31; // here we are using our method to add two numbers - "a" and "b" int sum = addTwoNumbers(a, b); System.out.println(sum); } static int addTwoNumbers(int firstNumber, int secondNumber) { int result = firstNumber + secondNumber; return result; } }
Note
You may have noticed that we added
static
before the method signature. For now, you don't need to pay attention to this keyword. We are using it only because the main method isstatic
by default, and static methods can only use other static methods within them.
- We created two variables of type
int
with different values and then passed them as parameters to our method; - Since our method returns an
int
type, we can initialize the variablesum
with the result of calling the method; - Thus, our method calculated and returned the
sum
of the two numbers.
We can also pass arrays as parameters and return them from methods. Let's look at an example:
Main
package com.example; import java.util.Arrays; public class Main { public static void main(String[] args) { // creating unsorted array int[] array = {4, -1, 5, 0, 4, -6, 2, 7, 4, 1}; // using method to sort and increment every element int[] newArray = sortAndIncrementEveryElement(array); // printing our new array for (int element : newArray) { System.out.print(element + " "); } } static int[] sortAndIncrementEveryElement(int[] inputArray) { // creating a result array int[] result = new int[inputArray.length]; // incrementing every element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sorting our result array Arrays.sort(result); // returning sorted and incremented array return result; } }
We have written a method that sorts and increments each element of an integer array (int[]
). Then we create an unsorted array and use the method on it, initializing a new array called newArray
with the returned value.
It is worth noting that we can use this method multiple times in the same code, for example:
Main
package com.example; import java.util.Arrays; public class Main { public static void main(String[] args) { // creating unsorted arrays int[] array = {4, -1, 5, 0, 4, -6, 2, 7, 4, 1}; int[] secondArray = {2, 2, 0, -5, 1, 8, 13, -9, 0}; // using method to sort and increment every element int[] newArray = sortAndIncrementEveryElement(array); int[] newSecondArray = sortAndIncrementEveryElement(secondArray); // printing our new arrays for (int element : newArray) { System.out.print(element + " "); } System.out.println(System.lineSeparator()); for (int element : newSecondArray) { System.out.print(element + " "); } } static int[] sortAndIncrementEveryElement(int[] inputArray) { // creating a result array int[] result = new int[inputArray.length]; // incrementing every element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sorting our result array Arrays.sort(result); // returning sorted and incremented array return result; } }
Thus, we can see that we can use our method multiple times in the code. We have used it on two different integer arrays, and each of them is now sorted and incremented.
Obrigado pelo seu feedback!