Parameter passing in Java (PowerPoint)

Download Report

Transcript Parameter passing in Java (PowerPoint)

Parameter Passing with Java
See also the examples:
Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java
© Juhani Välimäki 2003
1. Primitive Data Types - int
MAIN PROGRAM
main()
{
1234
int num = 1234;
// displays: 1234
System.out.println( num );
sub( num );
METHOD (SUBROUTINE)
M
A
I
N
value 1234 ->
Only the value of the variable num, the integer value 1234,
will be passed with the method call to the variable number in the
subprogram. That’s why the value 1111 is not transferred back
to the main program.
S
U
B
P
R
O
G
R
A
M
// displays: 1234
System.out.println( num );
}
M
A
I
N
void sub( int number )
1111
{
// displays: value got from main, 1234
System.out.println( number );
number = 1111;
}
// displays the modifed 1111
System.out.println( number );
Modification not transferred!
( However, using return number; the modified
value could be returned to the main program )
© Juhani Välimäki 2003
2. Object Reference - OwnClass
An object of type OwnClass: new Ownclass("1234");
setName()
MAIN PROGRAM
main()
{
OwnClass object1 =
new OwnClass(”1234”);
// displays: 1234
System.out.println( object1 );
sub( object1 );
getName()
METHOD (SUBROUTINE)
name ”1111”
M
AI
N
P
R
O
G
R
Now both the main program and the subprogram
point at the very same object. That’s how modifications
to that object in the subprogram will be seen in the
main program as well.
object reference -> S
U
B
P
R
O
G
R
A
M
}
// displays "1111" that was set
// by the subprogram
System.out.println( object1.getName() );
M
A
I
N
void sub( OwnClass obj )
{
// displays: orginal "1234"
System.out.println( obj.getName() );
obj.setName(”1111”);
}
// displays: modified "1111"
System.out.println( obj.getName() );
Using object references the
modification is transferred!
© Juhani Välimäki 2003
3. Array Object Reference - int[ ]
An integer array object created by new int[3]
MAIN PROGRAM
clone()
main()
{
int[ ] intArray = new int[3];
intArray[0] = 1234;
// displays: 1234
System.out.println(intArray[0] );
1111
0
M
AI
N
P
R
O
G
R
object reference ->
sub( intArray );
(arrays in Java = objects)
0
After the method call both programs have a reference to the
very same array object. Thus modifications made by the
sub routine will change the contents of the same object and
main program will see the changes.
S
U
B
P
R
O
G
R
A
M
// displays: changed 1111
System.out.println( intArray[0] );
}
METHOD (SUBROUTINE)
equals()
M
A
I
N
void sub( int[ ] numbers )
{
// displays: the original1234
System.out.println( numbers[0] );
numbers[0] = 1111;
}
// displays: changed value 1111
System.out.println( numbers[0] );
Also using array references the
modifications are transferred!
© Juhani Välimäki 2003
4. String - Badly Behaving Object
MAIN PROGRAM
A String object
”original”
main()
name
{
String text = ”original”;
// displays: "original"
System.out.println( text );
sub( text );
M
A
I
N
object reference ->
METHOD (SUBROUTINE)
After the method call both the main program and the sub
program point at the same object. However String is a constant
text object. If it's contents are changed, a new String object will
be created. That's why after modification the main program and
the sub program are pointing at a
different String object.
”modified” A new
String object
S
U
B
P
R
O
G
R
A
M
// displays: "original"
System.out.println( text );
}
M
A
I
N
void sub( String text )
{
// displays: "original"
System.out.println( text );
text = ”modified”; //new String object
}
// displays: created String "modified"
System.out.println( text );
Modification not transferred!
( Here as well, using return text; we could return the modified
String to the main program. But object references cannot be used)
© Juhani Välimäki 2003
More about objects and references
A.
Primitive data types (byte, short, int, long, float, double, boolean, char) are not
objects. Primitive data type variables are always inside a class or a method.
B.
Objects are made of a certain class, so they are instances of that class. Arrays
are objects in Java. Objects are created outside of classes and methods.
(Classes and methods only have references to the objects.)
•
Objects are created with the new-operator & constructor call:
= new OwnClass(”1234”);
= new int[3];
•
There has to be at least one reference variable of the same type pointing
at the created new object:
OwnClass myObject = new OwnClass(”1234”);
int[] array = new int[3]; int[] anotherPointer = array;
•
An object, that is not referred will be disposed by Garbage collection:
myObject = null; // Now there is no reference to this object,
thus that object is destroyed. (null means empty/nil/zero, in Java)
© Juhani Välimäki 2003