Methods/Functions in Java
• A method is a block of code that is written to perform a specific task.
• A method is like someone you hire to do something for you.
• Java Programs are divided into Data and Methods.
• Methods are used to process data.
• A method is also called as a function/process/procedure/routine
General Structure of Methods in Java
Access_Specifier Return_Type method_name(Arguments/Inputs)
{
//Processing
}
1.Access_Specifier : Controls the Access to the method.
2.Return_Type : Determines the type of the value being returned by the
method
3.Method_Name : A name given to a method.
4.Arguments : Inputs passed to a method
Example of a Method
public int addTwoNumbers(int a, int b)
{
int sum = a+b;
return sum;
}
Types of Methods
• Java Methods are classified into four different types based on:
• 1. Return Type
• 2. Arguments or Data being passed.
Type 1. Return Type With Arguments
public double run(double a)
{
return a*3.4;
}
Type 2. Return Type With No Arguments
public int move()
{
return 300;
}
Type 3. No Return Type With Arguments
public void fight(int z, int w)
{
System.out.println(z*w);
}
Type 4. No Return Type With No Arguments
public void useless()
{
System.out.println(“I am not Useless”);
}
Calling Methods
Points
• Methods are created to provide service.
• Methods can be called whenever we
need their services.
• Methods in Java are a part of a class or
an object.
• Normally methods belong to an object
unless they are static
• We need to create an object of the class
that contains the method to be able to
use them.
Example
Class A
{
public void hi()
{
}
}
A obj = new A();
obj.hi();
Mechanism of Calling methods
points
• A method is called by:
• 1. Its name
• 2.Number and type of Arguments
• 3. Return Types.
• Note that if a method returns
something we need to prepare a
suitable container to hold the
returned value from the function
Exmaple
class A
{
public int add(int a)
{
}
}
A obj = new A();
int container = obj.add(3);

Methods in Java its a presentation that .pptx

  • 1.
    Methods/Functions in Java •A method is a block of code that is written to perform a specific task. • A method is like someone you hire to do something for you. • Java Programs are divided into Data and Methods. • Methods are used to process data. • A method is also called as a function/process/procedure/routine
  • 2.
    General Structure ofMethods in Java Access_Specifier Return_Type method_name(Arguments/Inputs) { //Processing } 1.Access_Specifier : Controls the Access to the method. 2.Return_Type : Determines the type of the value being returned by the method 3.Method_Name : A name given to a method. 4.Arguments : Inputs passed to a method
  • 3.
    Example of aMethod public int addTwoNumbers(int a, int b) { int sum = a+b; return sum; }
  • 4.
    Types of Methods •Java Methods are classified into four different types based on: • 1. Return Type • 2. Arguments or Data being passed.
  • 5.
    Type 1. ReturnType With Arguments public double run(double a) { return a*3.4; }
  • 6.
    Type 2. ReturnType With No Arguments public int move() { return 300; }
  • 7.
    Type 3. NoReturn Type With Arguments public void fight(int z, int w) { System.out.println(z*w); }
  • 8.
    Type 4. NoReturn Type With No Arguments public void useless() { System.out.println(“I am not Useless”); }
  • 9.
    Calling Methods Points • Methodsare created to provide service. • Methods can be called whenever we need their services. • Methods in Java are a part of a class or an object. • Normally methods belong to an object unless they are static • We need to create an object of the class that contains the method to be able to use them. Example Class A { public void hi() { } } A obj = new A(); obj.hi();
  • 10.
    Mechanism of Callingmethods points • A method is called by: • 1. Its name • 2.Number and type of Arguments • 3. Return Types. • Note that if a method returns something we need to prepare a suitable container to hold the returned value from the function Exmaple class A { public int add(int a) { } } A obj = new A(); int container = obj.add(3);