SlideShare a Scribd company logo
2
Most read
5
Most read
12
Most read
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 { }.
Java method
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);
}

More Related Content

PPTX
java interface and packages
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Java program structure
PPTX
Classes objects in java
PPT
Final keyword in java
PPS
Introduction to class in java
PDF
Java threads
PDF
Class and Objects in Java
java interface and packages
Basic Concepts of OOPs (Object Oriented Programming in Java)
Java program structure
Classes objects in java
Final keyword in java
Introduction to class in java
Java threads
Class and Objects in Java

What's hot (20)

PPTX
constructors in java ppt
PPTX
Packages in java
PPSX
Exception Handling
PPTX
Interface in java
PPTX
Interfaces in java
PPTX
PPS
String and string buffer
PPTX
this keyword in Java.pptx
PPTX
Java constructors
PPT
08 c++ Operator Overloading.ppt
PPT
Core java concepts
PPTX
Constructor and Types of Constructors
PPTX
Java package
PPTX
Java program structure
PPTX
Static Members-Java.pptx
PPT
9. Input Output in java
PPTX
Multiple inheritance possible in Java
PPTX
Java string handling
PPT
Exception handling
constructors in java ppt
Packages in java
Exception Handling
Interface in java
Interfaces in java
String and string buffer
this keyword in Java.pptx
Java constructors
08 c++ Operator Overloading.ppt
Core java concepts
Constructor and Types of Constructors
Java package
Java program structure
Static Members-Java.pptx
9. Input Output in java
Multiple inheritance possible in Java
Java string handling
Exception handling
Ad

Similar to Java method (20)

PPTX
Java method present by showrov ahamed
PPTX
Computer programming 2 Lesson 15
PDF
CIS 1403 lab 3 functions and methods in Java
PPTX
UNIT1-JAVA.pptx
PDF
ppts foe design and analysis of algorithm
PPT
UNIT-1-PPTS-DAA_INTRODUCTION_TO_DAA_GH.ppt
PPT
UNIT-2-PPTS-DAA.ppt
PPT
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
PPT
UNIT 1- Design Analysis of algorithms and its working
PPT
UNIT-1-PPTS-DAA.ppt
PPT
Introduction to Design Algorithm And Analysis.ppt
PPT
UNIT-1-PPTS-DAA.ppt
PPTX
OOPJ.pptx
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PDF
Week 7 Java Programming Methods For I.T students.pdf
DOCX
Methods in Java
DOCX
PPTX
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
PPTX
Lesson 3 Functions OOP Lessons For CS AND IT
PPTX
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
Java method present by showrov ahamed
Computer programming 2 Lesson 15
CIS 1403 lab 3 functions and methods in Java
UNIT1-JAVA.pptx
ppts foe design and analysis of algorithm
UNIT-1-PPTS-DAA_INTRODUCTION_TO_DAA_GH.ppt
UNIT-2-PPTS-DAA.ppt
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT 1- Design Analysis of algorithms and its working
UNIT-1-PPTS-DAA.ppt
Introduction to Design Algorithm And Analysis.ppt
UNIT-1-PPTS-DAA.ppt
OOPJ.pptx
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
Week 7 Java Programming Methods For I.T students.pdf
Methods in Java
Module 1 full slidfcccccccccccccccccccccccccccccccccccdes.pptx
Lesson 3 Functions OOP Lessons For CS AND IT
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
Ad

More from sunilchute1 (10)

PPTX
Programming construction tools
PPTX
Introduction to data structure
PPTX
Sorting method data structure
PPTX
Introduction to data structure
PPTX
Input output statement
PPTX
Basic data types in python
PPTX
Call by value and call by reference in java
PPTX
Constructors in java
PPTX
C loops
PPT
Basic of c language
Programming construction tools
Introduction to data structure
Sorting method data structure
Introduction to data structure
Input output statement
Basic data types in python
Call by value and call by reference in java
Constructors in java
C loops
Basic of c language

Recently uploaded (20)

PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Classroom Observation Tools for Teachers
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chinmaya Tiranga quiz Grand Finale.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
Paper A Mock Exam 9_ Attempt review.pdf.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Weekly quiz Compilation Jan -July 25.pdf
History, Philosophy and sociology of education (1).pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx

Java method

  • 1. JAVA METHODS SUNIL D. CHUTE ASSIST. PROF. M. G. COLLEGE ARMORI
  • 2. • 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.
  • 3. TWO TYPES OF METHODS •Predefined Method •User-defined Method
  • 4. 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 { }.
  • 6. 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.
  • 7. 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
  • 8. • 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:
  • 9. • 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.
  • 10. 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"); }
  • 11. • 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.
  • 12. 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); }