Home >Java >javaTutorial >The difference between functions and methods in java
Functions and methods in Java are synonyms. They are both used to perform tasks or operations, accept input parameters and return results. The key differences between the two are: 1. Background: functions are common in non-object-oriented environments, while methods are a concept of object-oriented programming; 2. Attribution: functions are independent of classes and objects, while methods belong to specific classes or objects; 3. Access permissions: Functions have no access permission modifiers, but methods can be declared as public, protected, or private; 4. Calling method: Functions are called directly with the function name, while methods are called through object references.
The difference between functions and methods in Java
Get straight to the point:
In Java , Functions and methods are the same concept. For Java, functions are methods.
Elaboration:
In Java, functions and methods are used to perform specific tasks or operations. They can all accept input parameters and return results or perform specific operations.
Key Difference:
Similarities:
Example:
Function:
<code class="java">public static int add(int a, int b) { return a + b; }</code>
Method:
<code class="java">public class MyClass { public int add(int a, int b) { return a + b; } }</code>
Usage:
int sum = add(10, 20);
Method:MyClass myClass = new MyClass();
<code> `int sum = myClass.add(10, 20);`</code>
The above is the detailed content of The difference between functions and methods in java. For more information, please follow other related articles on the PHP Chinese website!