A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.
Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, overload methods using the same names, and apply method abstraction in the program design.
Creating Method:
Considering the following example to explain the syntax of a method:
public static int funcName(int a, int b) { // body }
Here,
- public static : modifier.
- int: return type
- funcName: function name
- a, b: formal parameters
- int a, int b: list of parameters
Methods are also known as Procedures or Functions:
- Procedures: They don't return any value.
- Functions: They return value.
Method definition consists of a method header and a method body. The same is shown below:
modifier returnType nameOfMethod (Parameter List) { // method body }
The syntax shown above includes:
- modifier: It defines the access type of the method and it is optional to use.
- returnType: Method may return a value.
- nameOfMethod: This is the method name. The method signature consists of the method name and the parameter list.
- Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
- method body: The method body defines what the method does with statements.
Example:
Here is the source code of a method which takes two parameters n1 and n2 and returns the minimum of the two:
/** the snippet returns the minimum between two numbers */ public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; }
Method Calling:
For using a method, it should be called. There are two ways in which a method is called i.e. method returns a value or returning nothing (no return value).
The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when:
- return statement is executed.
- reaches the method ending closing brace.
The methods returning void is considered as call to a statement. Lets consider an example:
System.out.println("This is tutorialspoint.com!");
The method returning value can be understood by the following example:
int result = sum(6, 9);
Example:
Following is the example to demonstrate how to define a method and how to call it:
public class ExampleMinNumber{ public static void main(String[] args) { int a = 11; int b = 6; int c = minFunction(a, b); System.out.println("Minimum Value = " + c); } /** returns the minimum of two numbers */ public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; } }
This would produce the following result:
Minimum value = 6
Variable
A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. The basic form of a variable declaration is shown here:
data type variable [ = value][, variable [= value] ...] ;
Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.
Following are valid examples of variable declaration and initialization in Java:
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'
This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java:
-
Local variables
-
Instance variables
-
Class/static variables
Local variables
Instance variables
Class/static variables
No comments:
Post a Comment