On Wed, 13 Sep 2000, Geeta Ramani wrote:
> Properly chastised, (;-), I went ahead and tested several things in
> a trivial extension of Kevin's Test class and after some reading
> concluded that i was indeed wrong, and Kevin was indeed right.
> Apparantly the fact that String is an "immutable" class is the
> underlying reason for the fact that a putValue is neccessary for
> String objects, but, not needed for say, StringBuffer objects or
> Vector objects or Array objects (or probably most anything else?).
> So I guess the fact that it is a String is sort of special.
> Hopefully i am not too far off..?
That is only part of it. It also has to do with the way Java passes
objects around, as references (I'm avoiding that nasty "p" word :-).
For example, suppose you do:
String myString = (String) session.getValue("myString");
Vector myVector = (Vector) session.getValue("myVector");
myString and myVector now refer to the same objects as are stored in
the session. If you then do:
myString = "some new string";
myVector = new Vector();
*Neither* of these change anything in the session, you are simply
changing what myString and myVector refer to, that is, making them
refer to different objects. So String is not special in this regard.
However, if instead you do something like:
myVector.addElement(someObject);
This *does* change something in the session. Here, you are
manipulating the object that myVector refers to (which is the same
thing that the session refers to), as opposed to making it refer to a
different object. Here is where the immutability of Strings makes a
difference, making Strings "sort of special" -- there is no way to
manipulate what the String refers to.
Hope this is clear.
> Kevin Mukhar wrote:
>
> > Geeta Ramani wrote:
> > >
> > > Hmm, really? I thought that since String is not a primitive data
> > > type, when you pass a String to a variable, you are passing a
> > > reference to that String obect, (and it is not passed by
> > > value)..? So my guess would have been that doing a "putValue"
> > > wouldnt have been necessary.. Of course it should easy enough to
> > > check..
> >
> > And if you had checked it before leaping to reply, you would have
> > found...
> >
> > h.get("myInfo") is [this is string 1]
> > myInfoB is [This is an entirely different string]
> >
> > ------------------ test code ------------------------------
> > import java.util.*;
> >
> > public class Test {
> > public static void main(String[] args) {
> > Hashtable h = new Hashtable();
> > String myInfo = "this is string 1";
> > h.put("myInfo", myInfo);
> >
> > //simulate servlet b with a different myInfo variable
> > String myInfoB = (String) h.get("myInfo");
> > myInfoB = "This is an entirely different string";
> >
> > System.out.println("h.get(\"myInfo\") is [" + h.get("myInfo") + "]");
> > System.out.println("myInfoB is [" + myInfoB + "]");
> > }
> > }
> > ------------------ test code ------------------------------
> >
> > You are partly right. A reference to the string is passed to the
> > session object. When servlet B gets the string from the session,
> > it is really getting a reference to the string. That reference is
> > assigned to some variable. If a new reference is assigned to that
> > variable, the reference inside the session object is unchanged--
> > it still points to the original string object. Assigning a new
> > reference (of ANY type) to a new variable does not change the
> > references held by other variables.
> >
>
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html