From: wangqianpku@... Date: 2007-09-30T11:00:08+09:00 Subject: Re: a different type of reference (shocked) On Sep 30, 12:59 am, SpringFlowers AutumnMoon wrote: > 7stud -- wrote: > > In C++, references are sometimes called 'aliases'. But ruby also has > > aliases: > > > x = "hello" > > y = x > > > y[0] = "H" > > puts x, y //Hello Hello > > but does Ruby have something like: > > x = "hello" > y = (whatever &*) x > y = "bye" > > and both x, y will print out as "bye"? > > -- > Posted viahttp://www.ruby-forum.com/. In my opinion, the references we called in ruby, java are actually pointers which needn't dereference. That means when a variable in ruby, java is used as a left value, it works like a pointer, when as a right value, it dereference automatically. In java, when we say: String s = "abc"; what the system actually does is: 1.create an anonymous object "abc". 2.create an varible(reference, actually a pointer) s. 3.let s point to abc; now we can return to the two different case: first: > x = "hello" > y = x > y = "bye" > puts x, y //hello, bye in this case, what the system does is 1.create "hello"; 2.create x; 3.let x point to "hello"; 4.create y; 5.let y point to what x points to (right value, x is dereferenced automatically); 6.create "bye"; 7.let y point to "bye"; 8.print x, y (right value, x,y are dereferenced automatically); In this case the third sentence changed the object which y points to. so at last, x,y are different. and the second case: > > x = "hello" > > y = x > > > y[0] = "H" > > puts x, y //Hello Hello we can see in this case, the object which y point to doesn't be changed. the third sentence just change the first charactor of the "hello" object, and x,y point to the same object. so, the second example doesn't means ruby has an "alias" machanism like c++, ruby's machanism is ruby's not c++'s. when passing parameters, ruby, java has only one sementics. it is "dereference automatically and pass a copy". c/c++ has two way "pass by value" and "pass by reference". in a word, in ruby, java, the sementics is not "value" or "reference" in c++, but something between them like what i say above.