|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
Tomorrow, (a) bring a camera to lab, and (b) don't wear green. (I'll try to get a green-screen, and next week you can use that image to superimpose yourself on another image [of the exact same dimensions].)
void tryAndSwap( int a, int b ) { int tmp; tmp = a; a = b; // Assigning to a parameter -- bad style! b = tmp; // Assigning to a parameter -- bad style! } |
int myFavoriteInt = 5; int yourFavoriteInt = 23; tryAndSwap( myFavoriteInt, yourFavoriteInt ); myFavoriteInt == 5 // True, or false? yourFavoriteInt == 5 // True, or false? |
// In class Robot: public static void swapRobotShape( Robot r1, Robot r2 ) { // What to put here? } // From the code pad, say: // Robot optimus = new Robot( "X47", "Daisy", false, false ); Robot optimusPrime = new Robot( "X47'", "Mack Truck", false, false ); optimus.toString(); Robot.swapRobotShape(optimus, optimusPrime); optimus.toString(); |
We say: “Java always passes arguments by value”: it takes the actual argument's value, and uses (a copy of) that value to initalize the parameter. The thing to realize is that (in java) “references are values”.
Some languages allow something different:
they let you “pass arguments by reference”,
a special language-feature
that would let you write something like
So as we're calling
We might also write
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Dec.01 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |