JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx

JohnMarkDeJesus4 8 views 17 slides May 01, 2024
Slide 1
Slide 1 of 17
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
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

They are used to divide and sort Functionalities within a class so that the code will be readable even if its long.


Slide Content

JAVA METHOD

METHOD / FUNCTIONS They are used to divide and sort Functionalities within a class so that the code will be readable even if its long.

Creating Methods modifiers returntype methodname () { //Do anything here }

Creating Methods static void sayHello () { System.out.println(“Hello World”); }

Calling Method public static void main(String[] args) { methodname(); }

Calling Method public static void main(String[] args) { sayHello(); }

Comments // This is a single line comment /* This is a multi-line comment you can comment here too!! */

Variable Scoping Global Variable Are variables declared within a class, it can be accessed within a whole class. Local Variable Are variables declared inside a method, condition, loops and any other block of code, it can be accessible within that block of code.

Arguments / Parameters A value that needs to be passed on a method so that the method can use value and perform various operations on it. PS: You can have as many Arguments / Parameters as you want, they act as a Local variable inside a method/function.

Method with Arguments modifiers returntype methodName(arguments) { //Do anything here }

Method with Arguments static void say(String word) { System.out.println(word); }

Return Keyword return keyword is used to return a value from the method. It is used when a method has a result. Example: A method that performs Math equations. A method that concatenates Strings.

Return Type The type of the value that will be returned, return type are same as the datatypes. void returns Nothing. int returns integers. String return strings. And so on……

Method w/ Return modifiers returntype methodName(arguments) { //Do anything here return value; }

Method w/ Return static int add(int num1, int num2) { return num1 + num2; }

Overloading Methods You can use the same method name but different parameters so that you will cater to every possibility of a method.

Overloading Methods static int add(int num1, int num2) { return num1 + num2; } static int add(int num1, int num2, int num3){ return num1 + num2 + num3; }
Tags