Methods in Java its a presentation that .pptx

anasraufmoh 30 views 10 slides Sep 02, 2024
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

presentation about methods in java


Slide Content

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);
Tags