1 / 15

Methods

Methods. A Java Program. A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements. A Java Program. Java classes. Java Methods. … - What is a method …. public class class-name { method1 method 2

mort
Download Presentation

Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Methods

  2. A Java Program A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements A Java Program Java classes Java Methods

  3. … - What is a method … public class class-name { method1 method 2 method 3 … … method n }

  4. Example of a Java Program Class name Main method Class body Instruction

  5. Exercise • Write a program that computes: • factorial (n!) of 6 • factorial of 3, then • factorial of 10.

  6. Method definition using method Method invocation Return type Method parameter Return instruction

  7. Method Structure public or private<static> <void or typeReturned>myMethod(<parameters>) { statement statement statement … … statement } Method name If method doesn’t return value Type of the return value Variable list Method body

  8. Invoking a Methods • The statements inside a method body are executed when the corresponding method is called from another method. • Calling a method is also called invoking a method • Each time the method is invoked, its corresponding body is executed

  9. - return Statements … • The body of a method that returns a value must also contain one or more return statements • A return statement specifies the value returned and ends the method invocation.

  10. return Statements • A void method need not contain a return statement, Example : write a method that prints all the numbers between 10 and 30

  11. public classTestSum{ public staticvoid main)String[] args){ float  i =5; float  j =2; float  k =Sum(i ,j); System.out.println)”The sum =“ + k); } Public static float Sum( float x , float y ) { float z ; z= x + y ; return z; } }

  12. - Method Parameters: Array Parameters A parameter of type array

  13. - Method Overloading … • In java the same class can have methods with the same name. Such methods are called overloaded methods. • Overloaded methods must differ in the number or type of their arguments. • The compiler treats overloaded methods as completely different methods..

  14. - Method Overloading … These 3 methods have the same name but different signatures

More Related