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

Methods

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

Methods

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

Object-Oriented Programming

with Java
Methods

Quan Thanh Tho, Ph.D.


CSE – HCMUT
[email protected]
1
Methods
l Hello World!
l Java program compilation
l Introducing Methods
l Declaring Methods
l Calling Methods
l Passing Parameters by value
l Overloading Methods

2
A Simple Application
Example 1
//This application program prints “Hello World!”
public class HelloWorld
{
int n = 10;
public static void main(String[] args)
{
System.out.println(”Hello World ” +
n + ” times!");
}
}

3
Compiling Java Programs
lCommand line on cs
¡javac ClassName.java

Java Source
File
ClassName.java

Compiler

Bytecode ClassName.class

4
Executing Applications
lCommand line on cs
¡java ClassName

Bytecode ClassName.class

Java Java Java


Interpreter Interpreter Interpreter
...
on Windows on Linux on Sun Solaris

5
Example 1
l javac HelloWorld.java

l java HelloWorld

l Hello World 10 times!

6
Compiling and execution
l Compilation:
javac HelloWorld.java
Result of compilation of Java file HelloWorld.java is
file HelloWorld.class with bytecode

l Execution
java HelloWorld
Result is “HelloWorld 10 times!” to standard output

7
Simple skeleton of Java application
File: ProgramName.java
• public class ProgramName
• {
• // Define program variables here.
• double d;
• ...
• // Define program methods here.
• int method1()
• { // Do something
• }
• . . .
• //Define the main method here.
• public static main(String args[])
• {
• // Main method body
• }//end of the main method.
• } //End of class ProgramName
8
Introducing Methods
Method Signature
A method is a modifier methodName

collection of returnValueType parameters

statements that are method


public static int max(int num1, int num2)
grouped together to heading
{
int result = 0;
perform an operation. method
body
if (num1 > num2)
result = num1;
else
result = num2;

return result;
}
return value

9
Method signature
The combined name and parameter list for each method in a
class must be unique. The uniqueness of a parameter list takes
the order of the parameters into account.
So int myMethod (double q, int n) is unique from
int myMethod (double q, double x) and
int myMethod (int k, double y).

10
Declaring methods
[modifiers] return_type method_name (parameter_list)
{
[statement_list]
}
Everything within square brackets [] is optional.
The minimal method declaration includes:
• Return Type: The return type is either a valid Java type (primitive or class)
or void if no value is returned. If the method declares a return type, every
exit path out of the method must have a return statement.
• Method Name: The method name must be a valid Java identifier.
• Parameter List: The parentheses following the method name contain zero or
more type/identifier pairs that make up the parameter list. Each parameter is
separated by a comma. Also, there can be zero parameters.

11
Declaring Methods

int max(int num1, int num2)


{
int x;
if (num1 > num2)
x = num1;
else
x = num2;
return x;
}
12
Calling Methods
public class TestMax
{
/**A method for finding a max between two numbers*/
int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}

public static void main(String[] args)


{
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println("The maximum between " + i +
" and " + j + " is " + k);
}
13
}
Passing parameters by value
l When a primitive value is passed into a
method, a copy of the primitive is made.
The copy is what is actually manipulated in
the method.
l So, the value of the copy can be changed
within the method, but the original value
remains unchanged.

14
Passing parameters by value
int myMethod(int a, int n)
{
int S = 0;
for (int i=0; i<=n; i++)
{
S += a;
a++;
}
return S;
}
-----------------------

a = 10;
System.output.printkn(“a=”+a); // a=10
int b = myMethod(a,5);
System.output.println(“a=”+a); // a=?
15
Passing parameters by value
int myMethod(int a, int n)
{
int S = 0;
for (int i=0; i<=n; i++)
{
S += a;
a++;
}
return S;
}
-----------------------

a = 10;
System.output.println(“a=”+a); // a=10
int b = myMethod(a,5);
System.output.println(“a=”+a); // a=10
16
Polymorphism: Overloading Methods

l The practice of defining more than one method in a


class with same name is called method overloading.
l Java resolves overloaded method names using the
types of the argument expressions.
l When the Java compiler encounters a method
invocation involving an overloaded method, it selects
the ``best'' (most specific) match from among the
alternatives.
l If no best method exists, the program is ill-formed and
will be rejected by the Java compiler.
17
Overloading Methods
int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
double max(double num1, double num2)
{
if (num1 > num2)
return num1;
else
return num2;
}

18

You might also like