0% found this document useful (0 votes)
10 views23 pages

Chapter-1 (1)

Uploaded by

mmhj6470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Chapter-1 (1)

Uploaded by

mmhj6470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter -1 Introduction to

Methods
Opening
Problem
• Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to
45, respectively.
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
Solutio
n

public static int sum(int i1, int i2) {


int sum = 0; method
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}

public static void main(String[] args)


{ System.out.println("Sum from 1 to 10 is " + sum(1,
10)); System.out.println("Sum from 20 to 30 is " +
sum(20, 30)); System.out.println("Sum from 35 to 45 is "
+ sum(35, 45));
}
Defining
Methods
• A method is a collection of statements that are grouped together to
perform an operation.
Defining
Methods
Method
Signature
Method signature is the combination of the method name and the parameter list.
Formal
Parameters
The variables defined in the method header are known as
formal parameters.
Actual
Parameters
When a method is invoked, you pass a value to the
parameter.
This value is referred to as actual parameter or argument.
Return Value
• AType
method may return a value.
• The returnValueType is the data type of the value the method
returns.
• If the method does not return a value, the returnValueType is the
keyword void.
• For example, the returnValueType in the main method is void.
Static Method

• A method that has static keyword is known as static method.


• In other words, a method that belongs to a class rather than an instance of a
class is known as a static method.
• We can also create a static method by using the keyword static before the
method name.

• The main advantage of a static method is that we can call it without creating an
object.
• It can access static data members and also change the value of it.
• It is used to create an instance method.
• It is invoked by using the class name.
• The best example of a static method is the main() method.
Calling
Methods
CAUTIO
N
• A return statement is required for a value-returning method.
• The method shown below in (a) is logically correct, but it has a
compilation error
because the Java compiler thinks it possible that this method does not
return any value.

To fix this problem, delete if (n < 0) in (a), so that the compiler will see a return
statement to be reached regardless of how the if statement is evaluated.
Reuse Methods from Other
Classes
• NOTE: One of the benefits of methods is for
reuse.
• The max method can be invoked from any class
besides TestMax.
• If you create a new class Test, you can invoke the max
method using
• ClassName.methodName
• (e.g.: TestMax.max).
Call
Stacks
“Each time a method is invoked, the system stores parameters
and variables in an area of memory (RAM) known as a stack,
which stores elements in last-in, first-out fashion."
 When a method calls another method, the caller’s stack space is
kept intact, and new space is created to handle the new method
call.
 When a method finishes its work and returns to its caller, its
associated space is released.
last-in first-out (LIFO)
Call
Stacks
void Method
Example
• This type of method does not return a value. The method
performs some actions.
Passing
Parameters
public static void nPrintln(String message, int n) {
for (int i = 0; i < n; i++)
System.out.println(message);
}

Suppose you invoke the


method using
nPrintln(“Welcome to
Java”, 5);
What is the output?

Suppose you invoke the


method using
nPrintln(“Computer Science”,
15);
What is the output?
Pass by
Value
public class Calculator {
public static void main(String[] args)
{
int a=6;
int b=2;
Add(a,b);
Subtract(a,b);
Multiplication(a,b);
Division(a,b);
}
public static void Add(int a, int b) {
System.out.println(a+b);
}
public static void Subtract(int a, int b) {
System.out.println(a-b);
}
public static void Multiplication(int a, int b) {
System.out.println(a*b);
}
public static void Division(int a, int b) {
System.out.println(a/b);
}
}

19
public class Calculator { public class Calculator {
public static void main(String[] args)
{
int a=6; public static void Add(int a, int b) {
int b=2; System.out.println(a+b);
Add(a,b); }
Subtract(a,b);
Multiplication(a,b); public static void Subtract(int a, int b) {
Division(a,b); System.out.println(a-b);
} }
public static void Add(int a, int b) {
System.out.println(a+b); public static void Multiplication(int a, int b) {
} System.out.println(a*b);
public static void Subtract(int a, int b) { }
System.out.println(a-b);
} public static void Division(int a, int b) {
public static void Multiplication(int a, int b) { System.out.println(a/b);
System.out.println(a*b); }
} Gives the
public static void Division(int a, int b) { Same output public static void main(String[] args) {
System.out.println(a/b); int a=6;
} int b=2;
}
Add(a,b);
Subtract(a,b);
Multiplication(a,b);
Division(a,b);

}
Overloading
Methods
Overloading methods enables you to define the methods with the same name if
their signatures are different.
Overloading the max Method

public static double max(double num1, double num2) {


if (num1 > num2)
return num1;
else
return
num2;
}

public static int max(int num1, int num2) {


if (num1 > num2)
return num1;
else
return
num2;
} public static void main(String[] args)
{
System.out.println(max(1.0, 2));
System.out.println(max(1.5, 6));

}
Ambiguous
Invocation
• Sometimes there may be two or more possible matches for an
invocation of a
method, but the compiler cannot determine the most specific
match.
• This is referred to as ambiguous invocation.
• Ambiguous invocation
public is a compilation
class AmbiguousOverloading { error.
public static void main(String[] args) {
System.out.println(max(1, 2));
}
public static double max(int num1, double num2){
if (num1 > num2)
return num1;
else
return
num2;
}
public static double max(double num1, int num2){
if (num1 > num2)
return num1;
else
return
num2;
}
}
The End of Chapter-1

You might also like