Java Methods
I) What is Method?
Method is a laid upwards of statements to perform an operation, methods are every bit good known every bit procedures or functions.
Generally, nosotros work Functions (Built inwards together with user defined) inwards estimator programming or scripting.
In Java nosotros work methods for code reusability.
II) Use of Methods:
Whenever nosotros desire perform same operations multiple times therefore nosotros work methods, using methods nosotros tin dismiss trim back the code size.
III) Types of Methods:
Basically nosotros convey ii types of methods inwards java.
i) Built inwards methods
ii) User defined methods.
IV) Built inwards methods
> Java has a library of (pre-defined) classes together with methods, organized inwards packages.
> In club to work Built inwards methods, nosotros import the packages (or classes individually). (Group of classes)
> java.lang parcel is automatically imported, inwards whatsoever Java program.
> Using import keyword nosotros tin dismiss import coffee pre-defined libraries.(We tin dismiss import entire packages or item class)
Categories of Built inwards methods:
a) String methods
b) Array methods
c) Character methods
d) Number methods
e) Date & Time methods
etc...
Example for Built inwards method:
System.out.println("Hello Selenium");
-------------------------------------
V) User defined methods
> Methods inwards Objects oriented Programming equivalent of Functions inwards Non object oriented programming.
Types of User defined methods
i) Method without supply whatsoever value
ii) Method amongst supply values.
Writing method:
Syntax:
modifier returnType methodName(Parameters) {
// Method body
}
modifier -It is optional, it defines access type of the method
returnType - Method may retrun a value
methodName - Name of the method.
parameters - Parameters are optional, nosotros tin dismiss work mutiple parameters yesteryear separating amongst ,
method trunk - laid upwards of statements define that what the method does.
------------------------------------------------
Example 1: Method amongst returning a value.
public static int add(int a, int b){
Statements
----------
-----------
}
Method calling:
dataType variableName = methodName(Values)
--------------
int abc = add(5, 3);
System.out.println(abc);
}
world static int add(int a, int b){
int result;
lawsuit = a + b;
supply result;
}
}
----------------------------------
Example 2: Method without returning whatsoever value
modifier methodName(Parameters) {
//Method body
}
public static void add(int a, int b) {
Statements
-----------
--------
}
Examples:
Internal method:
----------------
public static void main(String [] args){
studentRank(499);
}
world static void studentRank(int marks) {
if (marks >= 600) {
System.out.println("Rank:A1");
}
else if (marks >= 500) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
------------------------------------
Example 3: External Method (Calling from external or around other class).
import xyza.CopyArray;
public shape Apple extends CopyArray {
world static void principal (String [] args){
studentRank(678);
}
}
------------------------------------------
VI) Method Overloading inwards Java
If a shape convey multiple methods amongst same name, precisely unlike parameters, It is known every bit Method overloading.
There are ii ways to overlad the Method inwards java
i) By changing let out of Arguments.
Example: nosotros convey Two methods inwards our shape amongst the get upwards of add.
a) int add together (int a, int b)
b) int add together (int a, int b, int c)
ii) By changing information types.
a) int add together (int a, int b)
b) double add(double a, double b)
----------------------------
Example Java Program for Method Overloading:
package javaExamples;
public shape MethodOverLoading {
public static void principal (String []args){
int x = add(5, 7);
int y = add(5, 7, 9);
double z = add(5.234, 7.23);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
public static int add(int a, int b){
int result;
lawsuit = a + b;
supply result;
}
public static int add(int a, int b, int c){
int result;
lawsuit = a + b + c;
supply result;
}
public static double add(double a, double b){
double result;
lawsuit = a + b;
supply result;
}
}
Advantages of Method Overloading:
> It increases the readability of the Program.
---------------------------------
Also Read:
Java Variables
Java Operators
Java Conditional Statements
Java Loop Statements
Java Arrays
Sumber http://www.gcreddy.com/
I) What is Method?
Method is a laid upwards of statements to perform an operation, methods are every bit good known every bit procedures or functions.
Generally, nosotros work Functions (Built inwards together with user defined) inwards estimator programming or scripting.
In Java nosotros work methods for code reusability.
II) Use of Methods:
Whenever nosotros desire perform same operations multiple times therefore nosotros work methods, using methods nosotros tin dismiss trim back the code size.
III) Types of Methods:
Basically nosotros convey ii types of methods inwards java.
i) Built inwards methods
ii) User defined methods.
IV) Built inwards methods
> Java has a library of (pre-defined) classes together with methods, organized inwards packages.
> In club to work Built inwards methods, nosotros import the packages (or classes individually). (Group of classes)
> java.lang parcel is automatically imported, inwards whatsoever Java program.
> Using import keyword nosotros tin dismiss import coffee pre-defined libraries.(We tin dismiss import entire packages or item class)
Categories of Built inwards methods:
a) String methods
b) Array methods
c) Character methods
d) Number methods
e) Date & Time methods
etc...
Example for Built inwards method:
System.out.println("Hello Selenium");
-------------------------------------
V) User defined methods
> Methods inwards Objects oriented Programming equivalent of Functions inwards Non object oriented programming.
Types of User defined methods
i) Method without supply whatsoever value
ii) Method amongst supply values.
Writing method:
Syntax:
modifier returnType methodName(Parameters) {
// Method body
}
modifier -It is optional, it defines access type of the method
returnType - Method may retrun a value
methodName - Name of the method.
parameters - Parameters are optional, nosotros tin dismiss work mutiple parameters yesteryear separating amongst ,
method trunk - laid upwards of statements define that what the method does.
------------------------------------------------
Example 1: Method amongst returning a value.
public static int add(int a, int b){
Statements
----------
-----------
}
Method calling:
dataType variableName = methodName(Values)
--------------
int abc = add(5, 3);
System.out.println(abc);
}
world static int add(int a, int b){
int result;
lawsuit = a + b;
supply result;
}
}
----------------------------------
Example 2: Method without returning whatsoever value
modifier methodName(Parameters) {
//Method body
}
public static void add(int a, int b) {
Statements
-----------
--------
}
Examples:
Internal method:
----------------
public static void main(String [] args){
studentRank(499);
}
world static void studentRank(int marks) {
if (marks >= 600) {
System.out.println("Rank:A1");
}
else if (marks >= 500) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
------------------------------------
Example 3: External Method (Calling from external or around other class).
import xyza.CopyArray;
public shape Apple extends CopyArray {
world static void principal (String [] args){
studentRank(678);
}
}
------------------------------------------
VI) Method Overloading inwards Java
If a shape convey multiple methods amongst same name, precisely unlike parameters, It is known every bit Method overloading.
There are ii ways to overlad the Method inwards java
i) By changing let out of Arguments.
Example: nosotros convey Two methods inwards our shape amongst the get upwards of add.
a) int add together (int a, int b)
b) int add together (int a, int b, int c)
ii) By changing information types.
a) int add together (int a, int b)
b) double add(double a, double b)
----------------------------
Example Java Program for Method Overloading:
package javaExamples;
public shape MethodOverLoading {
public static void principal (String []args){
int x = add(5, 7);
int y = add(5, 7, 9);
double z = add(5.234, 7.23);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
public static int add(int a, int b){
int result;
lawsuit = a + b;
supply result;
}
public static int add(int a, int b, int c){
int result;
lawsuit = a + b + c;
supply result;
}
public static double add(double a, double b){
double result;
lawsuit = a + b;
supply result;
}
}
Advantages of Method Overloading:
> It increases the readability of the Program.
---------------------------------
Also Read:
Java for Selenium (Roadmap)
Java Introduction
Java Environment Setup
Java Program Structure
Java Modifiers
Java Introduction
Java Environment Setup
Java Program Structure
Java Modifiers
Java Operators
Java Conditional Statements
Java Loop Statements
Java Arrays