0% found this document useful (0 votes)
8 views

Day 3 Methods in Java

The document provides an overview of methods in Java, including their types, declarations, and how to call them. It discusses method overloading, parameter passing, and the differences between call-by-value and call-by-reference. Additionally, it explains the structure of method headers and bodies, as well as the return statement and control flow in method invocations.

Uploaded by

sipesog774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Day 3 Methods in Java

The document provides an overview of methods in Java, including their types, declarations, and how to call them. It discusses method overloading, parameter passing, and the differences between call-by-value and call-by-reference. Additionally, it explains the structure of method headers and bodies, as well as the return statement and control flow in method invocations.

Uploaded by

sipesog774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Methods in Java

Day 3

Java Training for WIPRO TalentNext


13-Mar-18 1
2017 -Vaibhav Diwan
Methods
• A class contains data declarations (static and instance
variables) and method declarations (behaviors)

class Month
int month;
int year Data declarations

Method declarations

Java Training for WIPRO TalentNext


13-Mar-18 2
2017 -Vaibhav Diwan
Method Types
There can be various types of methods (behavior declarations)
– access methods : read or display states (or those that can be
derived)
– predicate methods : test the truth of some conditions
– action methods, e.g., print
– constructors: a special type of methods
they have the same name as the class
– there may be more then one constructor per class (overloaded
constructors)
they do not return any value
– it has no return type, not even void
they initialize objects of the class, using the new construct:
– e.g. m1 = new Month();
you do not have to define a constructor
– the value of the state variables have default value

Java Training for WIPRO TalentNext


13-Mar-18 3
2017 -Vaibhav Diwan
Methods
• A program that provides some functionality can be long and
contains many statements

• A method groups a sequence of statements and should


provide a well-defined, easy-to-understand functionality

– a method takes input, performs actions, and produces output

• In Java, each method is defined within specific class

Java Training for WIPRO TalentNext


13-Mar-18 4
2017 -Vaibhav Diwan
General method declarations

• Header
qualifiers properties return-type
method-name ( formal-parameters )
throws-clause
• Body
{
Statements;
}

Java Training for WIPRO TalentNext


13-Mar-18 5
2017 -Vaibhav Diwan
Method Declaration: Header

• A method declaration begins with a method header

class MyClass
{ …
public static int min ( int num1, int num2 )

parameter list
The parameter list specifies the type
method
and name of each parameter
name
Qualifiers
return The name of a parameter in the method
type declaration is called a formal argument

properties
Java Training for WIPRO TalentNext
13-Mar-18 6
2017 -Vaibhav Diwan
Method Declaration: Body
The header is followed by the method body:

class MyClass
{

static int min(int num1, int num2)
{
int minValue = num1 < num2 ? num1 : num2;
return minValue;
}


}

Java Training for WIPRO TalentNext


13-Mar-18 7
2017 -Vaibhav Diwan
The return Statement

• The return type of a method indicates the type


of value that the method sends back to the
calling location
– A method that does not return a value has a
void return type

• The return statement specifies the value that


will be returned
– Its expression must conform to the return type
Java Training for WIPRO TalentNext 8
13-Mar-18 8
2017 -Vaibhav Diwan
Calling a Method
• A method can be called by object of the class.
Using dot operator.
– objectName.methodName(parameter list);
• A static method can be called directly
independent of object.
– ClassName.methodName(parameter list);
• Within a class method can be called directly
– mehtodName(parameter list);

Java Training for WIPRO TalentNext


13-Mar-18 9
2017 -Vaibhav Diwan
Multiple invocations or calling

• At any given point in time, a single method


may have been invoked through multiple
locations in a program.
• Every invocation gets its own copy of the
methods local variables (including the
formal parameters)
• A method may even call invoke itself. This
is called recursion.

Java Training for WIPRO TalentNext


13-Mar-18 10
2017 -Vaibhav Diwan
Calling a Method
• Each time a method is called, the values of the
actual arguments in the invocation are assigned
to the formal arguments
int num = min (2, 3);

static int min (int num1, int num2)


{
int minValue = (num1 < num2 ? num1 : num2);
return minValue;
}

Java Training for WIPRO TalentNext


13-Mar-18 11
2017 -Vaibhav Diwan
Method Control Flow
• A method can call another method, who
can call another method, …
main
min(num1, num2, num3) println()

min(1, 2, 3);
…println(…)

Java Training for WIPRO TalentNext


13-Mar-18 12
2017 -Vaibhav Diwan
Method Overloading
• A class may define multiple methods with the same name-
--this is called method overloading
– usually perform the same task on different data types

• Example: The PrintStream class defines multiple println


methods, i.e., println is overloaded:
println (String s)
println (int i)
println (double d)

• The following lines use the System.out.print method for
different data types:
System.out.println ("The total is:");
double total = 0;
System.out.println (total);
Java Training for WIPRO TalentNext
13-Mar-18 13
2017 -Vaibhav Diwan
Method Overloading: Signature

• The compiler must be able to determine which


version of the method is being invoked
• This is by analyzing the parameters, which form the
signature of a method
– the signature includes the type and order of the
parameters
• if multiple methods match a method call, the compiler picks the
best match
• if none matches exactly but some implicit conversion can be done
to match a method, then the method is invoke with implicit
conversion.
– the return type of the method is not part of the signature
Java Training for WIPRO TalentNext
13-Mar-18 14
2017 -Vaibhav Diwan
Method Overloading
Version 1 Version 2

double tryMe (int x) double tryMe (int x, double y)


{ {
return x + .375; return x * y;
} }

Invocation

result = tryMe (25, 4.32)

Java Training for WIPRO TalentNext


13-Mar-18 15
2017 -Vaibhav Diwan
Two Types of
Parameter Passing
If a modification of the formal argument has
no effect on the actual argument,
– it is call by value

If a modification of the formal argument can


change the value of the actual argument,
– it is call by reference

Java Training for WIPRO TalentNext


13-Mar-18 16
2017 -Vaibhav Diwan
Call-By-Value and
Call-By-Reference in Java
• Depend on the type of the formal argument
• If a formal argument is a primitive data type, a modification
on the formal argument has no effect on the actual
argument
– this is call by value, e.g. num1 = min(2, 3);
num2 = min(x, y);
• This is because primitive data types variables contain their
values, and procedure call trigger an assignment:
<formal argument> = <actual argument>

int x = 2; int y = 3; int x = 2;


int num = min (x, y); int y = 3;
… int num1 = x;
static int num( int num1, int num2) int num2 = y;
{ … } Java Training for WIPRO TalentNext
{ … }
13-Mar-18 17
2017 -Vaibhav Diwan
Call-By-Value and
Call-By-Reference in Java
• If a formal argument is not a primitive data type, an
operation on the formal argument can change the actual
argument
– this is call by reference
• This is because variables of object type contain pointers to
the data that represents the object
• Since procedure call triggers an assignment
<formal argument> = <actual argument>
it is the pointer that is copied, not the object itself!
MyClass x = new MyClass(); x = new MC();
MyClass y = new MyClass(); y = new MC();
MyClass.swap( x, y);
x1 = x;

static void swap( MyClass x1, MyClass x2)
x2 = y;
{ … } Java Training for WIPRO TalentNext
{ … }
13-Mar-18 18
2017 -Vaibhav Diwan
THANK YOU

Java Training for WIPRO TalentNext


13-Mar-18 19
2017 -Vaibhav Diwan

You might also like