Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Our Own Methods | Methods
Java Extended
course content

Course Content

Java Extended

Java Extended

1. Deep Java Structure
2. Methods
3. String Advanced
4. Classes
5. Classes Advanced

bookCreating Our Own Methods

How to use methods?

We looked at creating a method that adds two numbers. Now let's see how we can actually call this method in our code:

java

Main

copy
123456789101112131415161718192021
package com.example; // do not modify the code below this comment public class Main { public static void main(String[] args) { int a = 10; int b = 31; // use the method to add two numbers: `a` and `b` int sum = addTwoNumbers(a, b); // print the sum of the two numbers System.out.println(sum); } // method to add two numbers and return the result static int addTwoNumbers(int firstNumber, int secondNumber) { int result = firstNumber + secondNumber; // return the result of the addition return result; } }
  • 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 variable sum 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:

java

Main

copy
1234567891011121314151617181920212223242526272829303132
package com.example; import java.util.Arrays; // do not modify the code below this comment public class Main { public static void main(String[] args) { // creating an unsorted array int[] array = {4, -1, 5, 0, 4, -6, 2, 7, 4, 1}; // use a method to sort the array and increment each element by 1 int[] newArray = sortAndIncrementEveryElement(array); // print the new array after sorting and incrementing each element for (int element : newArray) { System.out.print(element + " "); // output each element from the new array } } // method to increment each element of the array by 1 and then sort it static int[] sortAndIncrementEveryElement(int[] inputArray) { // create a result array with the same length as the input array int[] result = new int[inputArray.length]; // increment each element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sort the result array Arrays.sort(result); // return the 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:

java

Main

copy
1234567891011121314151617181920212223242526272829303132333435363738
package com.example; import java.util.Arrays; // do not modify the code below this comment 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}; // use a method to sort and increment each element in both arrays int[] newArray = sortAndIncrementEveryElement(array); int[] newSecondArray = sortAndIncrementEveryElement(secondArray); // print the new arrays after sorting and incrementing the elements for (int element : newArray) { System.out.print(element + " "); // output each element of the first array } System.out.println(System.lineSeparator()); for (int element : newSecondArray) { System.out.print(element + " "); // output each element of the second array } } // method to increment each element by 1 and then sort the array static int[] sortAndIncrementEveryElement(int[] inputArray) { // create a result array with the same length as the input array int[] result = new int[inputArray.length]; // increment each element by 1 for (int i = 0; i < inputArray.length; i++) { result[i] = inputArray[i] + 1; } // sort the result array Arrays.sort(result); // return the 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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
some-alt