MAIN PROGRAMMETHOD (SUBROUTINE) name An object of type OwnClass: new Ownclass("1234"); 2. Object Reference - OwnClass Using object references the modification is transferred! © Juhani Välimäki 2003 getName() setName() ”1111”"> MAIN PROGRAMMETHOD (SUBROUTINE) name An object of type OwnClass: new Ownclass("1234"); 2. Object Reference - OwnClass Using object references the modification is transferred! © Juhani Välimäki 2003 getName() setName() ”1111”">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003."— Presentation transcript:

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

2 1. Primitive Data Types - int MAINMAIN SUBPROGRAMSUBPROGRAM 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 MAINMAIN 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. value 1234 -> MAIN PROGRAMMETHOD (SUBROUTINE) Modification not transferred! © Juhani Välimäki 2003 ( However, using return number; the modified value could be returned to the main program )

3 M AI N P R O G R SUBPROGRAMSUBPROGRAM 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() ); } MAINMAIN 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 -> MAIN PROGRAMMETHOD (SUBROUTINE) name An object of type OwnClass: new Ownclass("1234"); 2. Object Reference - OwnClass Using object references the modification is transferred! © Juhani Välimäki 2003 getName() setName() ”1111”

4 M AI N P R O G R SUBPROGRAMSUBPROGRAM 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] ); } MAINMAIN 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. (arrays in Java = objects) MAIN PROGRAMMETHOD (SUBROUTINE) 1111 An integer array object created by new int[3] 3. Array Object Reference - int[ ] Also using array references the modifications are transferred! © Juhani Välimäki 2003 00 clone() equals() object reference ->

5 4. String - Badly Behaving Object MAINMAIN SUB PROGRAMSUB 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 MAINMAIN 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. object reference -> MAIN PROGRAMMETHOD (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 modified String to the main program. But object references cannot be used)

6 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)


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

Similar presentations


Ads by Google