Boxes nd Arrows There re two kinds of vriles in Jv: those tht store primitive vlues nd those tht store references. Primitive vlues re vlues of type long, int, short, chr, yte, oolen, doule, nd flot. References re used to ccess ojects. A is good metphor for either kind of vrile. Drwing es for primitive vriles is esy. The contents of the is the stored primitive vlue. A few exmples pper elow. 3 true 7.59 'X' n int vrile oolen vrile doule vrile chr vrile An oject is drwn s tht contins its dt memers, for exmple: clss Account { privte doule myblnce; privte long myid; privte oolen isspecil; myblnce 1523.67 myid 12345678 isspecil true Finlly, reference to n oject (returned y new) is drwn s n rrow: clss Account { privte doule myblnce; privte long myid; privte oolen isspecil; myblnce myid isspecil Account = new Account ( ); clss Word { privte String mychrs; Word toguess = new Word ("syzygy"); toguess mychrs "syzygy" A reference-vlued vrile is sometimes referred to s pointer. 64
How the ssignment sttement reltes to es An ssignment sttement merely copies the vlue in one to nother. When the contins reference n rrow two references to the sme oject result. Below re some sequences of ssignment sttements, long with their -nd-rrow representtions. We strt with int vriles: int m, n; m = 5; m 5 n? n = m; m = 7; m 7 n 5 In lmost ll progrmming lnguges, the effect is the sme: the vlue 5 in m gets copied into n, then the vlue 7 gets copied into m, replcing the 5 tht it formerly contined. Now consider n Account clss with single dt memer nmed myblnce nd three methods, constructor, deposit method, nd withdrw method. pulic clss Account { pulic Account (int initamount) { myblnce = initamount; pulic void deposit (int mount) { myblnce += mount; pulic void withdrw (int mount) { myblnce -= mount; privte int myblnce; Account = new Account (100); 100 Account = ; = new Account (200); Here, the reference vlue initilly stored into nmely, the rrow is copied into. After the ssignment sttement, nd contin references to the sme ccount. A new reference vlue is then creted vi the new opertor, nd tht reference is stored into, replcing s former contents. 200 100 65
Let s try nother one. Account = new Account (100); 100 Account = ;.withdrw (10); 90 A reference to new ccount is stored into. Tht reference (the rrow) is copied into ; oth vriles now contin references to the sme ccount. The withdrwl from tht ccount is visile either through s reference or through s. In C (without structs) nd Scheme, we hve essentilly the sme ehvior. Code segments tht hve the effect of the exmple just discussed pper elow. C int [1] = {100; int* = ; [0] -= 10; Scheme (define '(100)) (define ) (set-cr! (- (cr ) 10)) Prmeter pssing All method prmeters in Jv re pssed y vlue, which mens they re copied into temporry vriles in the function. As with the ssignment sttement, the copying of reference involves only the reference itself, not the oject referred to. (Agin, this is the sme ehvior s in C nd Scheme.) Suppose we supply n dditionl method for the Account clss: pulic void mke900 (Account ) { = new Account (900); Consider now the effect of the two ssignment sttements = new Account (); mke900 (); The first sets up reference to n ccount with lnce of $ The second sttement, cll to mke900, cretes copy of the reference nd stores it in the vrile (which is locl to mke900). 66
Within mke900, reference to new ccount is creted nd stored into. The effect is shown elow. 900 Upon return from mke900, the locl vrile disppers nd the reference still points to the ccount with $. Consider now method mke99 defined inside the Account clss: pulic void mke99 (Account ) {.myblnce = 99; Contrst the effect of the two sequences of ssignment sttements: ssignment sttements result immeditely efore returning from the method = new Account (); mke900 (); 900 = new Account (); mke99 (); 99 For one finl exmple, we consider the following method: pulic void swp (Account, Account ) { Account temp; temp = ; = ; = temp; Account = new Account (3000); Account crol = new Account (1500); swp (, crol); 67
Wht s the effect of the cll to swp? It hs no effect on either of the ccounts. Here s why. We strt with the initiliztion of nd crol: 3000 crol 1500 Then we enter the method, copying the rgument references to the locl vriles nd : 3000 temp crol 1500 After the first two ssignments, we hve 3000 temp crol 1500 The finl ssignment sttement copies reference to Mike s ccount into. Neither the vrile, the crol vrile, nor the corresponding Account ojects re modified y the swp method. 68
Comprisons A comprison of two references using == determines whether the two things compred re references to the sme ojects. (This ehvior is the sme s in C, nd the sme s wht the eq? function provides in Scheme.) Here s n exmple. Account = new Account (100); Account = ;.withdrw (10); Account c = new Account (90); c 90 90 After execution of the four sttements, comprisons etween the reference vlues would hve the results shown elow. expression vlue reson == true oth nd refer to the sme oject == c flse nd c refer to different ojects, even though those ojects oth hve the sme lnce == c flse sme reson Comprison of two ojects in Jv is conventionlly done with the equls method s shown elow. pulic oolen equls (Account ) { return this.myblnce ==.myblnce; 69