Junior — Middle
Can you explain what defining a function in Java means and how it is described?
sobes.tech AI
Answer from AI
In Java, a function is usually called a method and is described inside a class. A method is a block of code that performs a specific task and can accept parameters and return a result.
The declaration of a method includes:
- Access modifiers (e.g., public, private)
- Return type (e.g., int, void)
- Method name
- List of parameters in parentheses
- Method body in curly braces
Example of a method that returns the sum of two numbers:
public int sum(int a, int b) {
return a + b;
}