Lecture 12 (Methods01)
Lecture 12 (Methods01)
Rizwan Rashid
Opening Problem
• Find the sum of integers from 1 to 10, from 20
to 30, and from 35 to 45, respectively.
Problem
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);
Problem
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);
Solution
public static int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}
• Pre-defined Methods
– Already defined in the Java class libraries known
as standard library method or built-in method
– println(), Math.pow(), input.nextInt(), etc.
• User-defined Methods
– The method written by the user or programmer
– Modified as per requirement
Defining a Method
• Syntax
modifier returnValueType methodName(list of parameters)
{
// Method body;
}
Defining a Method
• Access Specifier:
– It specifies the visibility of the method. Java provides four
types of access specifier:
– Public: The method is accessible by all classes
– Private: The method is accessible only in the classes in
which it is defined
– Protected: The method is accessible within the same
package or subclasses in a different package
– Default: When we do not use any access specifier in the
method declaration, Java uses default access specifier by
default. It is visible only from the same package only.
Method Signature
Method signature is the combination of the method name and the
parameter list.
static:
Static methods are the method
which invokes without creating
the objects, so we do not need
any object to call the main()
method.
String args[]:
main(): The main() method also
void: It is a default signature which is accepts some data from the
Void keyword acknowledges predefined in the JVM. It is called user. It accepts a group of
the compiler that main() by JVM to execute a program line strings, which is called a string
method does not return any by line and end the execution after array. It is used to hold the
value. completion of this method. We can command line arguments in
also overload the main() method. the form of string values.
Java main() method
• What happens if the main() method is written without String
args[]?
– The program will compile, but not run, because JVM will not recognize
the main() method
– JVM always looks for the main() method with a string type array as a
parameter. public static void main(String[] args){
System.out.println("Hello");
• Execution Process }
static //static block
1. First, JVM executes the static block {
System.out.println("Static block");
2. Then it executes static methods }
3. Then it creates the object needed by the program
4. Finally, it executes the instance methods
Different way to write main()
• public static void main(String args[])
• static public void main(String args[])
• static public void main(String[] x)
• static public void main(String []x)
• static public void main(String...args)
Calling a Method
• Calling a method executes the code in
the method
• Method definition: indicates what the
method does
• How to call a method?
1. Value returning method: a call to the
method is treated as a value
int larger = max(3, 4);
or System.out.println(max(3, 4));
23
Void method with return
Passing arguments by values
• The arguments are passed by value to parameters
when invoking a method (pass-by-value)
– If the argument is a variable, the value of the variable is
passed to the parameter
• Parameter order association
– Arguments must be in the same order as their respective
parameters in the method signature
• Example:
• nPrintln(‚Hi‛, 3);
• nPrintln(3, ‚Hi‛);
• nPrintln(‚Hi‛, ‘3’);
Pass-by-value (example 1)
Pass-by-value (example 2)
A common mistake
Summary
• Method definition & calling
• Value returning method & void method
• The call stack
• Transfer of control
• Pass-by-value (argument passing)
• Method overloading
• The scope of variables