Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is a Method? | Methods
Java Extended
course content

Contenido del Curso

Java Extended

Java Extended

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

bookWhat is a Method?

Method

In Java, a method is a block of code that performs a specific task. It is a reusable unit of code that can be called and executed multiple times throughout a program. Methods are used to break down the complexity of a program into smaller, manageable parts, making the code more organized and easier to understand.

A method consists of a method signature, which includes the method name and any parameters it accepts, and a method body, which contains the code that defines the behavior of the method.

Methods can have a return type, indicating the type of value that the method returns after execution, or they can be void, meaning they do not return any value.

The basic syntax of any method looks like:

java

Main

copy
123
ReturnType methodName(ParameterType parameter, ParameterType parameter) { // code that runs when this method is called }

Now let's go through everything that has been written above:

  • ReturnType: This is the type of value that the method will return. In simple terms, it is the result of executing the method. If the functionality of the method is to add two numbers and return an integer value, then the ReturnType will also be int;
  • MethodName: It's just the name of our method. It is good practice to give methods informative names. For example, if the method adds two numbers, a good name would be addTwoNumbers;
  • ParameterType: This is the type of the parameter that the method will accept when it is used. Again, let's take the example of summing two numbers: in this case, we will have two parameters with the type int;
  • Parameter: This is the name of the parameter. This parameter can be used inside the body of the method. It will become clearer when you see a real example of method usage.

Now let's take a look at an example of a method that adds two numbers and returns their sum:

java

Main

copy
1234
int addTwoNumbers(int firstNumber, int secondNumber) { int result = firstNumber + secondNumber; return result; }

This method takes two int values as parameters, creates a new variable called result within itself, and assigns it the sum of the two parameters. Then we use the return keyword, which returns the value of result.

In the next chapter, we will delve deeper into how this method works and learn how to create our own methods.

In the next chapter, we will delve deeper into how this method works and learn how to create our own methods.

1. What is a method in Java?
2. What is the purpose of methods in Java?
What is a method in Java?

What is a method in Java?

Selecciona la respuesta correcta

What is the purpose of methods in Java?

What is the purpose of methods in Java?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 1
some-alt