Table of Contents
In this tutorial. we will discuss the method in the Java programming language
{
Method body
}
Access modifier – public private,protectedand others
return type= The data type of te value returned by the method
Eg
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 language has two types of methods
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);
}
}
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);
}
}
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
}
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.