The method in Java programming language

method in Java programming language

In this tutorial. we will discuss the method in the Java programming language

Java method

The method is one of the important concepts for oop program languages similar to java. A method is just a segment of code that does a particular job a Java method has a collection of statements that are grouped together to perform a particular operation. the particular method has a method header and a method body
1. The method is used to time-saving to the programmers and helps to reuse the code without retyping to coders.
2. A method has the following syntax
modifier returnType methodName(list of parametters)

{
Method body
}


Access modifier  – public private,protectedand others

return type= The data type of te value returned by the method

Eg


  • void – method does not return a value
  • int – Method return a value


3.
The method name – Some of rules should be apply the method name


4. The parameter list in parenthesis – you can pass list of input  parameter separated with comma and enclosed the  parenthesis (parameter list), if you use no parameter you must use empty breckets ()


5. The method body, Enclosed, between braces(curly brackets) the method,s code including the declaration of local variable goes here.

Java method types

Java language has two types of methods

  1. standard library method
  2. User-defined method

 

Standard library method

Standard library methods are pre-defined (build-in) method in Java language. build in methods are part of the java compilar.

 

User-defined method

User can define the method as their wish in Java language. it called user-defined method

 

Java has three types of user defined methods

Three types of method available in Java

  1. The Dumb Method
Does the same thing every time

Eg – void add()
{
int a=5,b=7;
System.out.println(a+b);
}

 

Example

class dumbmethod{
public static void main(String args[]){
add();
}
public static void add()
{
int a=15;
int b=25;
System.out.println(a+b);
}
}

2. The Clever Method
Ask the value for the input

Eg – void add(int a, int b)
{
System.out.println(a+b);
}

class clevermethod{
public static void main(String args[]){
add(24,34);  //argument passing
add(40,67);

}
public static void add(int a,int b)  //parametter passing
{

System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
}

}

When the above code is executed, it prodeces te following result

3. The Smart Method
Ask the value for input and also returns a result to the main method

Eg – void add(int a, int b)
{
int c=(a+b);
return c;
}

program 1

class smartmethod{
public static void main(String args[]){
int x=add(24,34);

System.out.print(x);
}
public static int add(int a, int b)
{
int sum=a+b;
return(sum);

}
}

How to create a user-defined method

we can declare a user-defined method as follows

Method definiyions have four basic parts

Metod name – Name of te method

return type – the variable type identifies the method return a value or not

parameter list – A list of parameter, we can pass one or more parameter

Method body – Statements inside the method body

 

No argument metod

public static void my_method() {                      //No parameter method
System.out.println("I am a user-defined method in Java");
}

Here, we have defined a Java method named as my_Method() with no argument.

We can use three keywords public, static, void in the initial part of the method name

 

Method with argument

public static int my_method(int a, int b) {                      //The method with parameter
System.out.println("I am a user-defined method in Java");
}

Public static – Access modifiers

int – return type

my_Metod – The name of the method

int a, int b– list of parameter

How to call Java method

 

 

Example program using metod

public class javamethod{
public static void main(String args[]){
myfirstmethod1();   // this is calling method1 under main method
myfirstmethod();       // this is calling method1 under main method
}
public static void myfirstmethod(){     // this is method 1
System.out.println(“my name is vannan”);
System.out.println(“i am a teacher”);
System.out.println(“nagarkovil a,m,t,m,school”);
}
public static void myfirstmethod1(){ // this is method 2
System.out.println(“this is my pen”);
System.out.println(“it is my book”);
System.out.println(“this is my school”);
}
}
parametter passing – string
public class javamethod2{
public static void main(String args[]){
myfirstmethod(“john”); //argument passing String
myfirstmethod(“tom”);
myfirstmethod(“jeyaproya”);
myfirstmethod(“kowsi”);
}
public static void myfirstmethod(String name) //parametter passing String
{
System.out.println(“Hello “+name);
}
}
}
parametter passing integer
public class javamethod3{
public static void main(String args[]){
add(100,200,300,400); //argument passing for parameter
}
public static void add(int a,int b,int c,int d) //parametter passing
{
System.out.println(a+b+c+b);
}

}

parametter passing integer (argument passing )
public class javamethod3{
public static void main(String args[])
{
add(100,200,300,400); //argument passing
add(101,201,301,401);
add(111,211,311,411);
}
public static void add(int a,int b,int c,int d) //parametter passing
{
System.out.println(a+b+c+b);
}
}

Java method with argument and return value

parameter passing integer (argument passing and returning the result to the main method )

program 1

public class javamethod5{
public static void main(String args[]){

int sum=add(100,200,300,400);   //argument passing
System.out.println(sum);
int result=sum*15;
System.out.println(result);    //display result

}

public static int add(int a,int b,int c,int d)   //parametter passing
{
//System.out.println(a+b+c+d);
return(a+b+c+d); //return value to main method

}

}

When the above code is executed, it prodeces the following result

program 2

class findmax{
public static void main (String args[]){
int result;
result=max(23,45);
System.out.println(result);
}
public static int max(int a, int b)
{int max; 
if(a>b)
{
max=a;
}
else
{
max=b;

}
return max;
}

}

 

When the above code is executed, it prodeces te following result


Related post

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.