In Java, functions are known as methods. Methods are blocks of code that perform specific tasks and can be called from other parts of the program. Methods can also take input parameters and return output values.
Java supports both static methods and instance methods. Static methods belong to the class and can be called using the class name, while instance methods are associated with objects and require an instance to be invoked. methods can be reused throughout a program, which can save time and effort.
Syntax of methods :-
modifier returnType nameOfMethod (Parameter List)
{
//Hare Write Statements of method
}
Predefined functions :- These are functions that are already defined in the Java language. They are also known as library functions or built-in functions. Examples of predefined functions include System.out.println(), String.length(), and Math.pow().
User-defined functions :- These are functions that are created by the programmer. They are defined within a class and can be either static (belong to the class itself) or non-static (belong to an object of the class). To create a user-defined method, you must first declare the method header. The method header specifies the name of the method, the type of data that the method returns (if any), and the type of data that the method takes as parameters (if any).
Once you have defined the method header and the method body, you can call the method from anywhere in your code.
To call a user-defined method, you simply need to type the name of the method followed by parentheses and a semicolon (;) in the method. If the method has parameters, you pass them to the method inside the parentheses.
Method call by varealbe Syntax :-
DataType Variable_Name = Method();
Method call by varealbe Syntax :-
Object_Refrence. Method(); //
User-defined functions can be used to encapsulate code and make it more reusable. They can also be used to create complex functions that would be difficult to write using only the built-in functions of Java.
The void keyword allows us to create methods which do not return a value. The void keyword can also be used as the return type of a main method. The main method is the entry point for a Java application, and it is always declared as void.
Example :- public static void Demo() // Create Method With Name Demo
{
System.out.println(“This is Method.”);
}
int result = Demo( );
System.out.println(result);
Output :- This is Method.
1. Non-Peramitrized Methods :- A non-parameterized method in Java is a method that does not take any input parameters. Non-parameterized methods should be used to perform simple tasks that do not require any input parameters. Non-parameterized methods are simple to write and use.
Example :– public class DemoClass
{
Public static void Demo-fun() // Declare “Demo-fun”Methods without Perameter.
{
Int x=a =10;
Int y=b=15;
System.out.println(x+y)
}
public static void main(String[] args) // Declare Main Methods
{
DemoClass ob=new DemoClass(); //Object create DemoClass
ob.Demo-fun(); // Method Call in variable sum.
System.out.println(ob); // print sum of object.
}
}
Output :- 25
2. Peramitrized Methods :- Parameterized methods in Java are methods that can take one or more parameters with Any types of Data. This means that the method can be used with any type of data, as long as the type parameters are met. While working under calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Parameters can be passed by value or by reference.
Example :– public class DemoClass
{
Public static void Demo-fun(int a,int b) // Declare “Demo-fun”Methods with Perameter.
{
Int x=a; //Pass perameter Value in variable “x”.
Int y=b; //Pass perameter Value in variable “y”.
System.out.println(x+y); // Sum Of “x” and “y”.
}
public static void main(String[] args) // Declare Main Methods
{
DemoClass ob=new DemoClass(); //Object create DemoClass.
Ob.Demo-fun(5,8); // Method Call in object with Passing the value.
System.out.println(ob); // print sum of passing value.
}
}
Output :- 13
Methods in Java are a great way to organize and reuse code. They can make your code more readable, maintainable, and efficient. Parameterized methods are a special type of method that can be used with any type of data.
Method overloading is a feature that allows a class to have multiple methods with the same name, but with different parameters. This allows you to write more reusable and flexible code.It is different from overriding. In overriding, a method has the same method name, type, number of parameters, etc.
Example :-
public class DemoClass
{
public int add(int a, int b)
{
return a + b;
}
public double add(double a, double b)
{
return a + b;
}
public static void main(String[] args)
{
DemoClass object = new DemoClass();
int result1 = object.add(15, 35);
double result2 = object.add(5.5, 1.5);
System.out.println(result1); // Result is 45
System.out.println(result2); // Result is 7.0
}
}
Output :- 45
7.0