Java Methods
Java Methods
Methods or Functions are a block of code that accept certain parameters and perform actions whenever they are called. Methods are always written inside a java class and can be called by simply referring to the method name.
Passing Parameters:
Parameters are nothing but the variables that are passed inside the parenthesis of a method. We can pass a single or more than one parameter of different datatypes inside our method.
Example 1: Passing single parameter
Output:
Example 2: Passing multiple parameters
Output:
Example 3: Method with loop
Output:
Example 4: Method with control statements
Output:
So what can we make out from e.g.3 and e.g.4? In e.g.3, we have created a method without using the void keyword, this lets us return the value inside the method. Whereas in e.g.4, we have created a method using void keyword, this means that our method wont return a value.