parameter passing with java see also the examples: parameters1.java, parameters2.java,...

6
Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 200

Upload: rosamund-snow

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003

Parameter Passing with Java

See also the examples:

Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java

© Juhani Välimäki 2003

Page 2: 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

SUB

PROGRAM

void sub( int number ){ // displays: value got from main, 1234 System.out.println( number );

number = 1111; // displays the modifed 1111 System.out.println( number ); }

main(){ int num = 1234; // displays: 1234 System.out.println( num );

sub( num );

// displays: 1234 System.out.println( num );

}

1234

1111

MAIN

Only the value of the variable num, the integer value 1234, will be passed with the method call to the variable number in thesubprogram. That’s why the value 1111 is not transferred back to the main program.

value 1234 ->

MAIN PROGRAM METHOD (SUBROUTINE)

Modification not transferred!

© Juhani Välimäki 2003

( However, using return number; the modifiedvalue could be returned to the main program )

Page 3: Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003

MAIN

PROGR

SUB

PROGRAM

void sub( OwnClass obj ){ // displays: orginal "1234" System.out.println( obj.getName() );

obj.setName(”1111”); // displays: modified "1111" System.out.println( obj.getName() ); }

main(){ OwnClass object1 =

new OwnClass(”1234”);

// displays: 1234 System.out.println( object1 );

sub( object1 );

// displays "1111" that was set // by the subprogram System.out.println( object1.getName() );}

MAIN

Now both the main program and the subprogrampoint at the very same object. That’s how modificationsto that object in the subprogram will be seen in themain program as well.

object reference ->

MAIN PROGRAM METHOD (SUBROUTINE)name

An object of type OwnClass: new Ownclass("1234");

2. Object Reference - OwnClass

Using object references themodification is transferred!

© Juhani Välimäki 2003

getName()

setName()

”1111”

Page 4: Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003

MAIN

PROGR

SUB

PROGRAM

void sub( int[ ] numbers ){ // displays: the original1234 System.out.println( numbers[0] );

numbers[0] = 1111; // displays: changed value 1111 System.out.println( numbers[0] ); }

main(){ int[ ] intArray = new int[3]; intArray[0] = 1234;

// displays: 1234 System.out.println(intArray[0] );

sub( intArray );

// displays: changed 1111 System.out.println( intArray[0] );

}

MAIN

After the method call both programs have a reference to thevery same array object. Thus modifications made by thesub routine will change the contents of the same object andmain program will see the changes.

(arrays in Java = objects)

MAIN PROGRAM METHOD (SUBROUTINE)1111

An integer array object created by new int[3]

3. Array Object Reference - int[ ]

Also using array references themodifications are transferred!

© Juhani Välimäki 2003

0 0clone()

equals()

object reference ->

Page 5: Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003

4. String - Badly Behaving Object

MAIN

SUB PROGRAM

void sub( String text ){ // displays: "original" System.out.println( text );

text = ”modified”; //new String object // displays: created String "modified" System.out.println( text ); }

main(){ String text = ”original”; // displays: "original" System.out.println( text );

sub( text );

// displays: "original" System.out.println( text );

}

name

MAIN

After the method call both the main program and the subprogram point at the same object. However String is a constanttext object. If it's contents are changed, a new String object willbe created. That's why after modification the main program andthe sub program are pointing at a different String object.

object reference ->

MAIN PROGRAM METHOD (SUBROUTINE)

© Juhani Välimäki 2003

Modification not transferred!

”original”

”modified”

A String object

A new String object

( Here as well, using return text; we could return the modifiedString to the main program. But object references cannot be used)

Page 6: Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003

More about objects and references

© Juhani Välimäki 2003

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)