Professional Documents
Culture Documents
Lecture - 10
Agenda
• Parameter passing in Java.
• Sample programs.
• Introduction to Object-Oriented
Programming.
• Next Lab session.
Parameter Passing in Java
They are the ways in which parameters are
transferred between functions/methods when
one method calls another.
Ex: We can call a method mysort(parameters)
from our main() method in Java.
Please refer to lecture – 9 for example.
• We can pass pass parameters by two
mechanisms in general
Pass – By – Value/Pass – By – Copy
Pass – By - Reference
Pass-By-Value
• A copy of the argument is made and then passed to the receiving method.
Public static void MyMethod(int num)
{
System.out.println(“In MyMethod, the value of num is:”+num);
num = 3;
System.out.println(“num is:”+num);
} Callee
Public static void main(String[] args)
{
int number = 1;
MyMethod(number);
System.out.println(“Number is:”+number);
}
What is an argument? Caller
Pass By Value
• Pass-By-Value: A copy of the argument is
made and then passed to the receiving
method.
If the callee method modifies the value of
the argument inside of its method body,
the caller will never see the results of
these modifications.
Pass By Reference
• Later, when we cover Objects.
Scope
Java rules for scope in case of local variables:
1) A variable’s scope extends from the line it is declared
until the end of the block it is contained in.
2) A formal parameter’s scope is the entire method. What
is a formal parameter? They are the variables declared
in the parameter list of a method. In our case ‘int num’
in the method MyMethod().
3) A variable declared in the ‘for’ loop control has a scope
that extends to the end of ‘for’ loop
A local variable is the one which is accessible only within
the function or block where it is declared.
Passing Strings as parameters in
Java
For primitive type arguments in Java, the changes
made in the methods are not reflected in the
caller program/method.
Thus parameters are passed by value.
With non-primitive types, the changes made in the
methods are reflected in the caller
program/method. Exception: Strings.
cap Write
Ink_amount Close
Fill_ink
Another Example
Car (is an Object)
State Behavior
Color SpeedUp
Speed Brake_apply
gear Gear_change
Another Example
Table-Lamp (is an Object)
State Behavior
On Turn On
Off Turn Off
Software Object
• Like real world objects, Software objects
are conceptually similar. They consist of
state and behavior.
• An object stores its state in fields
(variables in some programming
languages) and behavior through methods
(functions in some programming
languages).
• A set of possible variables and methods
for our object ‘pen’ might be:
Variables: boolean cap;
double ink_amount;
Methods : write();
fill_ink();
Data Types in Java
• Java is a strongly typed language. It
means that every variable used has a type
that is known at compile time.
• Importance of type: It helps limit the
values that a variable can hold, limit the
operations on those variables.
• Strong typing helps detect errors at
compile time.
Data Types in Java
• In Java types are divided into two
categories: primitive types and reference
types.
• Primitive types: int, float, char etc.
• Reference types: class type, interface type
and array type.
Memory representation-Arrays
• When you declare an array, memory does
not get allocated. It just simply allocates
memory to contain a reference to the
array!
• int[] myarr; 100
myarr
234
Memory allocation - Arrays
• When array size is ‘5’, int[] myarr = new
int[5] Myarr[0] 100
Myarr[1] 101
Myarr[2] 102
Myarr[3] 103
Myarr[4] 104
Reference Types
• A data type is a set of values and set of
operations defined on those values.
• The java class provides a mechanism for
defining our own data types. In a class we
specify the data type values and
implement data type operations.
Designing Our Data Type
Example:
}
car()
{
color = "RED";
speed = 80;
void printCarDetails()
{
System.out.println("New Speed is:"+speed+" NewGear is:"+gear);
}
}
toyota_instance.speedUp(30);
BMW_instance.speedUp(20);
System.out.println("My toyota car details are:");
toyota_instance.printCarDetails();
System.out.println("BMW car details are:");
BMW_instance.printCarDetails();
}
}
• In Java, each data type value is stored in
an Object.
• You can create any number of objects of a
particular class.
• In my previous example, I had 2 car
objects.
Static Methods & Instance Methods
• Static methods should be invoked with the
class name. Ex: Math.abs().
• Instance methods is invoked with Object
name.
• Instance methods are used to implement
data type operations.
• Instance methods manipulate object value
where as static methods compute return
value.