KEMBAR78
Java method | PPTX
JAVA METHODS
SUNIL D. CHUTE
ASSIST. PROF.
M. G. COLLEGE ARMORI
• A method is a block of code that performs a specific task.
• Suppose you need to create a program to create a circle and
color it. You can create two methods to solve this problem:
• a method to draw the circle
• a method to color the circle
• Dividing a complex problem into smaller chunks makes your
program easy to understand and reusable.
TWO TYPES OF METHODS
•Predefined Method
•User-defined Method
DECLARING A JAVA METHOD
• Syntax
returnType methodName()
{
// method body
}
Here,
returnType - It specifies what type of value a method returns For example if a method has an int
return type then it returns an integer value.
If the method does not return a value, its return type is void.
methodName - It is an identifier that is used to refer to the particular method in a program.
method body - It includes the programming statements that are used to perform some tasks.
The method body is enclosed inside the curly braces { }.
PREDEFINED METHOD
• In Java, predefined methods are the method that is already defined in the Java class
libraries is known as predefined methods. It is also known as the standard library
method or built-in method. We can directly use these methods just by calling them in the
program at any point. Some pre-defined methods are length(), equals(), compareTo(),
sqrt(), etc. When we call any of the predefined methods in our program, a series of codes
related to the corresponding method runs in the background that is already stored in the
library.
• Each and every predefined method is defined inside a class. Such as print() method is
defined in the java.io.PrintStream class. It prints the statement that we write inside the
method. For example, print("Java"), it prints Java on the console.
public class Demo
{
public static void main(String[] args)
{
// using the max() method of Math class
System.out.print("The maximum number is: " + Math.max(
9,7)); }
}
Output:
The maximum number is: 9
• In the above example, we have used three predefined methods main(),
print(), and max(). We have used these methods directly without declaration
because they are predefined. The print() method is a method
of PrintStream class that prints the result on the console. The max() method is a
method of the Math class that returns the greater of two numbers.
• We can also see the method signature of any predefined method by using the
link https://docs.oracle.com/. When we go through the link and see the max()
method signature, we find the following:
• n the above method signature, we see that the method signature has access
specifier public, non-access modifier static, return type int, method
name max(), parameter list (int a, int b). In the above example, instead of
defining the method, we have just invoked the method. This is the advantage of a
predefined method. It makes programming less complicated.
• Similarly, we can also see the method signature of the print() method.
USER-DEFINED METHOD
• The method written by the user or programmer is known as a user-
defined method. These methods are modified according to the requirement.
• How to Create a User-defined Method
• Let's create a user defined method that checks the number is even or odd. First,
we will define the method.
//user defined method
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
• We have defined the above method named findevenodd(). It has a
parameter num of type int. The method does not return any value that's why we
have used void. The method body contains the steps to check the number is even
or odd. If the number is even, it prints the number is even, else prints the
number is odd.
• How to Call or Invoke a User-defined Method
• Once we have defined a method, it should be called. The calling of a method in a
program is simple. When we call or invoke a user-defined method, the program
control transfer to the called method.
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from the user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}

Java method

  • 1.
    JAVA METHODS SUNIL D.CHUTE ASSIST. PROF. M. G. COLLEGE ARMORI
  • 2.
    • A methodis a block of code that performs a specific task. • Suppose you need to create a program to create a circle and color it. You can create two methods to solve this problem: • a method to draw the circle • a method to color the circle • Dividing a complex problem into smaller chunks makes your program easy to understand and reusable.
  • 3.
    TWO TYPES OFMETHODS •Predefined Method •User-defined Method
  • 4.
    DECLARING A JAVAMETHOD • Syntax returnType methodName() { // method body } Here, returnType - It specifies what type of value a method returns For example if a method has an int return type then it returns an integer value. If the method does not return a value, its return type is void. methodName - It is an identifier that is used to refer to the particular method in a program. method body - It includes the programming statements that are used to perform some tasks. The method body is enclosed inside the curly braces { }.
  • 6.
    PREDEFINED METHOD • InJava, predefined methods are the method that is already defined in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly use these methods just by calling them in the program at any point. Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc. When we call any of the predefined methods in our program, a series of codes related to the corresponding method runs in the background that is already stored in the library. • Each and every predefined method is defined inside a class. Such as print() method is defined in the java.io.PrintStream class. It prints the statement that we write inside the method. For example, print("Java"), it prints Java on the console.
  • 7.
    public class Demo { publicstatic void main(String[] args) { // using the max() method of Math class System.out.print("The maximum number is: " + Math.max( 9,7)); } } Output: The maximum number is: 9
  • 8.
    • In theabove example, we have used three predefined methods main(), print(), and max(). We have used these methods directly without declaration because they are predefined. The print() method is a method of PrintStream class that prints the result on the console. The max() method is a method of the Math class that returns the greater of two numbers. • We can also see the method signature of any predefined method by using the link https://docs.oracle.com/. When we go through the link and see the max() method signature, we find the following:
  • 9.
    • n theabove method signature, we see that the method signature has access specifier public, non-access modifier static, return type int, method name max(), parameter list (int a, int b). In the above example, instead of defining the method, we have just invoked the method. This is the advantage of a predefined method. It makes programming less complicated. • Similarly, we can also see the method signature of the print() method.
  • 10.
    USER-DEFINED METHOD • Themethod written by the user or programmer is known as a user- defined method. These methods are modified according to the requirement. • How to Create a User-defined Method • Let's create a user defined method that checks the number is even or odd. First, we will define the method. //user defined method public static void findEvenOdd(int num) { //method body if(num%2==0) System.out.println(num+" is even"); else System.out.println(num+" is odd"); }
  • 11.
    • We havedefined the above method named findevenodd(). It has a parameter num of type int. The method does not return any value that's why we have used void. The method body contains the steps to check the number is even or odd. If the number is even, it prints the number is even, else prints the number is odd. • How to Call or Invoke a User-defined Method • Once we have defined a method, it should be called. The calling of a method in a program is simple. When we call or invoke a user-defined method, the program control transfer to the called method.
  • 12.
    import java.util.Scanner; public classEvenOdd { public static void main (String args[]) { //creating Scanner class object Scanner scan=new Scanner(System.in); System.out.print("Enter the number: "); //reading value from the user int num=scan.nextInt(); //method calling findEvenOdd(num); }