You are on page 1of 27

Created By: Priyanka Pareek

12/08/21 Created By: PRIYANKA PAREEK 1


introduction
 Program: is a set of instructions given to
computer. These instructions initiate some
action and sometimes called as executable
instructions.
In Java programs instructions are specified
through Methods or functions.
Methods or Function is a sequence of some
declaration statements and executable
statements.
12/08/21 Created By: PRIYANKA PAREEK 2
A method is a separate piece of code that
can be called by a main program or any
other method to perform some specific
function.
The following are characteristics of methods:
 It can return one or no values
 It may accept as many parameters it needs or no
parameter at all. Parameters are also called
function arguments.
 After the method has finished execution, it goes
back to the method that called it.
12/08/21 Created By: PRIYANKA PAREEK 3
Why Methods?
• To Cope With Complexity
• Hiding Details
• Reuse

12/08/21 Created By: PRIYANKA PAREEK 4


METHOD/ FUNCTION DEFINITION
A method or a function is a sequence of
statements that carry out specific task(s)
A method or function must be defined before it is
used anywhere in the program:
[access- specifier ] [modifier] return-type method
name (parameter list)
{
body of the method
}
12/08/21 Created By: PRIYANKA PAREEK 5
Where
 access specifier : can be either public or
protected or private. Default is friendly
 modifier: can be one of: final, native,
synchronized, transient, volatile.
 return-type: specifies the type of value that the
return statement of the method returns. If no
value being returned, it should be void.
 Method-name: valid java identifier.
 Parameter list: it is a comma-separated list of
variables of a method referred to as its arguments
or parameters.
12/08/21 Created By: PRIYANKA PAREEK 6
Method Prototype and Signature:
• A Method Prototype is the first line of the
method definition that tells the program about the
type of the value returned by the method and the
number and type of arguments.
• Method Signature: it basically refers to the
number and types of arguments. It is a part of the
method prototype.

12/08/21 Created By: PRIYANKA PAREEK 7


Use of Keyword void
• Methods not returning in any value are declared void
methods i.e., having void s their return type.
syntax: void method’s-name(parameter list);
example:
void setLabelText( )
{
jLabel.setText(“### Thank U###”);
}

12/08/21 Created By: PRIYANKA PAREEK 8


Methods
• Methods in Java define the behavior of the class.
• Methods are similar to procedures or subroutines in other
languages.
• The real benefits of object orientation come from hiding the
implementation of a class behind its operations.
• Methods access the internal implementation details of a class
that are hidden from other objects.
• Hiding data behind methods is so fundamental to object
orientation it has a name - encapsulation.
• Methods have zero or more parameters.
• A method can have a return value.
• A method's statements appear in a block of curly braces { and }
that follow the method's signature.
public void speak () {
System.out.println("Hey Barney ...");
12/08/21 } Created By: PRIYANKA PAREEK 9
Invoking a method
• Provide an object reference and the method name,
separated by a dot (.):
fred.speak();
• Parameters are passed to methods as a comma-
separated list.
• A method can return a single value as a result, such
as an int:
public int add (int a, int b) {
return a+b;
}
• A method can return no result:
public void setFirstName (String firstName) {
this.firstName = firstName;
}

12/08/21 Created By: PRIYANKA PAREEK 10


Accessing/Invoking a method
• A method is called(or invoked or executed) by
providing the method name, followed by the
parameters sent enclosed in parentheses.
Example:
float area(float a, float b);
c= area(a,b);

12/08/21 Created By: PRIYANKA PAREEK 11


• Example: to print cube of a given number using
a method:
private void
ComputeActionPerformed(java.awt.event.ActionEvent
evt) {
double n =
Double.parseDouble(jTextField1.getText());
double res = CalcCube(n);
jLabel3.setText("" +res); }
double CalcCube(double b){
double cu = b * b * b;
return cu; }
12/08/21 Created By: PRIYANKA PAREEK 12
Actual and Formal parameters

• Actual parameters: are Formal Parameters:


the parameters
appearing in the method int mult (int x, int y) {
call statement. return x* y;
• Formal parameters: }
are the ones that appear
in method definition. Actual parameters:
• Example: int length = 10;
int width =5;
Int area = mult(length, width);
12/08/21 13

Created By: PRIYANKA PAREEK


Arguments to Methods
• Arguments to methods can be:
– of primitive data types i.e. char, byte, short, int,
long, float, double, boolean.
– of reference data types i.e. objects or arrays
• A method is invoked in two manners: Call by
Value and Call by Reference. These two
ways are also called as Pass By Value and
Pass by Reference.

12/08/21 Created By: PRIYANKA PAREEK 14


Call by Value (or Pass By Value )
• When a pass-by-value occurs, the method makes a copy of
the value of the variable passed to the method. The
method cannot accidentally modify the original argument
even if it modifies the parameters during calculations.
• In the given example, we called the method test and
passed the value of i as parameter. The value of i is copied
to the variable of the method j. Since j is the variable
changed in the test method, it will not affect the variable
value if i in main since it is a different copy of the
variable.

12/08/21 Created By: PRIYANKA PAREEK 15


Call by Value (or Pass By Value )

12/08/21 Created By: PRIYANKA PAREEK 16


Call by Reference(or Pass by Reference)
• When a pass-by-reference occurs, the reference
to an object is passed to the calling method. This
means that, the method makes a copy of the
reference of the variable passed to the method.
However, unlike in pass-by-value, the method
can modify the actual object that the reference is
pointing to, since, although different references
are used in the methods, the location of the data
they are pointing to is the same.

12/08/21 Created By: PRIYANKA PAREEK 17


Call by Reference(or Pass by Reference)

12/08/21 Created By: PRIYANKA PAREEK 18


Figure : Pass-by-reference example

A common misconception about pass-by-reference in Java is


when creating a swap method using Java references. Take note
that Java manipulates objects 'by reference,‘ but it passes object
references to methods 'by value.'" As a result, you cannot write a
standard swap method to swap objects.
12/08/21 Created By: PRIYANKA PAREEK 19
Returning from a method
• A method returns value through return statement.
• A method terminates when either a return
statement is encountered or the last statement in
the method is executed.
• A return statement is used to terminate a method
whether or not it returns a value.

12/08/21 Created By: PRIYANKA PAREEK 20


• return statement: is useful in 2 ways:
– Exit from method is cause d as soon as a return
statement is encountered and control passes back to
the calling method.
– Use of return statement is that it is used to returna
value to the calling code.
– Example:
boolean chkNumber(int a){
if (a>=0)
return true;
else
return false;
12/08/21 Created By: PRIYANKA PAREEK 21
}
Returning values
• They may of three types of methods in Java:
– Computational Methods: these are the methods
that calculate or compute some value and returned
the computed value. Example: Math.sqrt () and
Math.cos( ) are computational methods.
Computational methods always return a computed
result.
– Manipulative Methods: these are the methods
that manipulate information and return a success or
failure code.
• Procedural methods: these are the methods that
perform an action and have no explicit return
value. Example: System.out.println() method.
Scope revisited
• The scope determines where in the program the
variable is accessible. The scope also
determines the lifetime of a variable or how
long the variable can exist in memory. The
scope is determined by where the variable
declaration is placed in the program.
• Def: The program part(s) in which a particular
piece of code or a data value(e.g.. Variable) can
be accessed is known as the piece-of-code’s or
variable’s Scope.
Constructors
• Constructors: a member method with the
same name as its class is called Constructor
and it is used to initialize the objects of that
class type with a legal initial value.
• Example:
class A( ){
int a;
float b;
public A( ){
a = 0;
b = 0.0; } }

You might also like