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

Methods and Paramenters

Methods in Java can be void or value methods. Void methods perform tasks without returning values, such as printing lines or drawing shapes. Value methods calculate and return a single result, such as returning an integer or boolean value. Methods are invoked or called by name, and may require parameters to provide additional information, making methods more flexible. Parameters must match the method signature when calling a method.

Uploaded by

Randy Fin
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)
41 views

Methods and Paramenters

Methods in Java can be void or value methods. Void methods perform tasks without returning values, such as printing lines or drawing shapes. Value methods calculate and return a single result, such as returning an integer or boolean value. Methods are invoked or called by name, and may require parameters to provide additional information, making methods more flexible. Parameters must match the method signature when calling a method.

Uploaded by

Randy Fin
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/ 33

METHODS IN JAVA

METHODS & PARAMETERS


VOID METHODS
wallBuilder
A method is a named, neatly packaged
fragment of Java code
Method to
A void method is a method that performs build a wall
a task

If we give the method a name (or


identifier) then we can reuse the method

All methods must therefore be given a


name/identifier
TERMINOLOGY
To use/reuse a void method we must call
or invoke the method

method
We call/invoke the method by using its
identifier

ALL of our void methods perform a


small task or job for us

Examples of void methods include:


• A method to print out some information
• A method to print out a ‘header’
PARAMETERS straw
bricks
It might be that our method requires some wood
‘extra information’ to make it work properly?

This ‘extra information’ helps to change a


method from being specific to being more
general (and typically more useful)

Method to
More than 1 piece of extra information may build a wall
be required

Regardless, the extra information is often


referred to as parameter(s)
PARAMETERS

12 7 4
4 9 3 11 -2 -7

Method to Method to Method to


calculate calculate calculate
the product the product the product
of 3 integers of 3 integers of 3 integers
EXAMPLE VOID METHODS
A method that prints out a few blank lines for us
• The information required to make this method work properly
might be an int value which specifies the precise number of
blank lines to be printed out

A method that draws a rectangle:


• The information required to make this method work properly
might be values which specify the dimensions of the
rectangle (i.e. length and breadth)
MORE ON THE “ADDITIONAL
INFORMATION”
This additional information:
 must be formally specified in the method heading
 must be actually supplied when we call (invoke) the method

We say that we must ‘pass the information to the method’

Note that NO information is ‘passed back from’ a void method


• Void methods simply perform their tasks (prints out a few
lines, draws a rectangle etc.) but do nothing else
1 “Yes”

VALUE METHODS -21.3


‘A’

Value methods are used to BOTH:


• calculate/establish a single result or value and
• return the calculated value

It is the return of the calculated value that is important

The kind of value calculated/established and returned is


referred to as “the nature of the method” e.g.
• The nature of the result / method might be an int value, a
boolean value, a double value, a String value etc.
RETURNING A VALUE
In addition to calculating a single value, ALL value methods of
capable of returning that value

Recall that with void methods we said that NO information was


‘passed back’ from a void method

With value methods the opposite is the case


• ALL value methods return (or pass) something back
• The ‘something returned’ is the result calculated/established
The nature of the value returned MUST be identified within the
method heading
EXAMPLE VOID METHODS
static void voidMethod1 ( ) {

// Method that needs no additional information to work


// WHY?? Answer: EMPTY SIGNATURE

}
static void voidMethod2 (int pNum) {

// Method that requires some additional information to


// work – in this case a single int value

}
MORE EXAMPLE VOID METHODS
static void voidMethod3 (int pNum1, int pNum2) {

// Method that requires some additional information to


// work – in this case 2 int values
}

static void voidMethod4 (int pNum1, int pNum2, String pString) {

// Method that requires some additional information to


// work – in this case 2 int values and 1 String value
// NB ORDER IS HIGHLY SIGNIFICANT!!!
}
MORE DETAIL
The signature of
The name/identifier of
The code of the the method
the method
method Method name +
parameters

static void voidMethod4 (int pNum1, int pNum2, String pString) {

// Method that requires some additional information to


// work – in this case 2 int values and 1 String value
// NB ORDER IS HIGHLY SIGNIFICANT!!!

……….
}
VALUE METHODS
static int return20 ( ) {

// Method that needs no additional information to work


// Calculates and returns an int value
return 20;
}

static int returnDouble (int pNum) {

// Method that requires some additional information to


// work – in this case a single int value
// Calculates and returns an int value
return (pNum * 2);
}
MORE VALUE METHODS
static boolean isGreater (int pNum1, int pNum2) {

// Method that requires some additional information to


// work – in this case 2 int values
return (pNum1 > pNum2);
}

static char nthChar (int pNum, String pString) {

// Method that requires some additional information to


// work – in this case 1 int value and 1 String value
// NB ORDER IS HIGHLY SIGNIFICANT!!!
return ( pString.charAt (pNum) );
}
A GOOD TIP!
As value methods always return a value, they must have a return
statement
• The return statement is the vehicle that allows the value to be returned

The return statement must identify the (single) value to be


returned
• What follows the word return must be the single value that is to be
returned
• This value must be ‘of the same kind of information’ as indicated in the
method heading
• Ensure that all possible routes through a method return a value

GOOD TIP! Where possible make the return statement the


LAST statement within the method
INVOKING METHODS
You invoke a void method by simply stating its name

We call/invoke value methods very differently

Such methods are normally called either:


• On the right-hand-side of an assignment statement
• Where the value that is returned is assigned to (say) a variable
that appears on the left-hand-side of the assignment statement

• Within (or as part of) a print or a println statement


• Where the value that is returned is being printed out and thus
appears within a print or a println statement
METHODS WITH DIFFERENT
SIGNATURES OR ARGUMENTS
Consider the following method heading:

static void sillyMethod1 (int pNumber) {

// Method whose signature includes an int

To fully appreciate how methods are called (or invoked) we must


understand the purpose of the method’s signature
SIGNATURES REVISITED
The signature of the previous method is underlined below:

static void sillyMethod1 (int pNumber) {

// Method whose signature includes an int

To call/invoke this method we MUST obey its signature and supply an


int value in the form of:
– an explicit integer (e.g. 12, -34, 99 etc.)
– an int variable (e.g. myNumber, someValue etc.)
THE BODY OF SILLYMETHOD1
Consider the following:

static void sillyMethod1 (int pNumber) {

System.out.println (“ ************************ ”);


System.out.println (“Number passed in was: “ + pNumber);
System.out.println (“ ************************ ”);

• We have a void method – no value is being returned


• We can invoke this method in a number of ways
INVOKING/CALLING SILLYMETHOD
Each of the following represents a valid call to (or invocation of)
the method sillyMethod

sillyMethod1 (12);
sillyMethod1
sillyMethod1 (48); use 12

sillyMethod1 (20);
sillyMethod1 (0);
sillyMethod1 (-8);
MORE EXAMPLES INVOCATIONS
The following demonstrate different calls to sillyMethod
• Assuming the existence of an int variable called someNumber
• Assuming someNumber has previously been assigned a value 12

sillyMethod1 (someNumber);
sillyMethod1 (someNumber * 4);
sillyMethod1 (someNumber + 8);
sillyMethod1 (someNumber - 12);
sillyMethod1 (someNumber - 20);
MORE ON METHODS
Methods are used to split problems into
smaller tasks
We can identify a method for each of these
simpler, smaller tasks
We can then reuse our method at different
points in the program by ‘calling’ or ‘invoking’
the method
• No limit to the number of times we call/invoke
the method
Methods can have their own variables
• These may only be used/accessed within the
method
• Used to perhaps support the method
SUMMARY
The only role of some methods in life is to ‘return a value’!
• Performs a calculation based on available information and
‘return the result’ of this calculation
• Searches for a value within an array and ‘return the index’ at
which this value was found
• We often refer to such methods as value method

Other methods typically perform a task


• Very simple example – to print out a few lines
• They return nothing (as such)
• We often call such methods as void methods
PARAMETERS
Parameters allow us to ‘talk’ or ‘interact’ with the method
If we had:
• A method that allowed us to paint a wall
• A much better method would allow us to paint a wall AND specify
a colour of paint
• A method that allowed us to draw a line
• A much better method would allow us to specify a line colour, a
line width and a line style

Parameters provide methods with information that transforms


them from being general to specific
PARAMETERS
static void method1 ( ) { …. } Formal parameter list
// Parameterless method

static int method2 (int pAge, String pName) { … }


// This method requires 2 parameters

int myAge, someInt; Actual parameter list


String myName;
// call methods
method1 ( );
someInt = method2 (myAge, myName);
PARAMETERS - EXAMPLE
// A value method for calculating age
static int calcAge (int yearBorn) {
int age;
age = 2017 - yearBorn;
return age; Formal Actual
} // method calcAge parameter parameter

public static void main (String args[ ]) {


int birthYear;
System.out.print (“Please, enter the year you were born: ”);
birthYear = keyboard.nextInt();
System.out.print (“You are now roughly “ + calcAge (birthYear) );
System.out.println (“ years old.”);
} // main
PASSING PARAMETERS
When we call (or invoke) a method we pass the actual parameters
from the call statement to the formal parameters within the
method

yearBorn = birthYear;

If we update these formal parameters within the method do the


changes get copied back into the actual parameters of the
method call statement??
• No!
• You should experiment to convince yourself of this!
METHOD OVERLOADING
We overload a method when we have more than 1 method
within a given class that has the same identifier

You should be familiar with this concept:


• The word ‘wind’ in English has more than one meaning
• The symbol + in Mathematics may mean integer addition or it might
mean real number addition
• We work out its meaning according to its context

The ‘context’ used in Java programming is that, if we choose


the same identifier for a number of methods, we MUST
ENSURE that there are difference(s) in the
• number, type and order of the parameters!
SIGNATURE

Method name + parameter list

We thus say (with respect to method overloading) that we


can have methods with the same identifiers as long as they
have different parameters
SAME METHOD IDENTIFIER
DIFFERENT PARAMETERS
ALL OF THESE METHODS ARE DIFFERENT!

static void someMethod (int aParam);


static void someMethod (String aParam);
static void someMethod (double aParam);
static void someMethod (int aParam, String bParam);
static void someMethod (int aParam, double bPAram)
static void someMethod (String aParam, int bParam);
ARE THESE DIFFERENT METHODS?
static int someMethod (int aParam) { …. } Java cannot
distinguish
static boolean someMethod (int aParam) { …. } between
static String someMethod (int aParam) { …. } these 3
methods

static int someMethod (int aParam, String bParam) { …. }


static String someMethod (int aParam, String bParam) { …. }
static boolean someMethod (int aParam, String bParam) { …. }
static double someMethod (int aParam, String bParam) { …. }

Java cannot distinguish between these 4 methods


APPLICATION CLASSES
Application classes have one static ‘main’ method
They are executable through their main method
It may also have other static methods
All methods in the class follow the same syntax:

MODIFIER static RETURN-TYPE ‘name’ (parameter-list) {

statement;
more statements;

} // name
METHOD – FLOW OF CONTROL
The main method is invoked by the system when you submit
the bytecode to the interpreter

Each method call returns to the place that called it!

main method1 method2

method2();
method1();

You might also like