p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Re: String question


Message #1 by "Shridhar Dighe" <mailtoshridhar@y...> on Fri, 12 Apr 2002 08:40:14
Hi,
             What u have written is perfectly right. But there is some 
misunderstanding. 

               Look when u write, message = " new Stirng"; this means 
that u r making that object ref. to point to the new stirng. U r not 
modifying that stirng. It is just like the 
                   const char *ptr &
                   char const *ptr 
                                   in C, if u know it.
Message #2 by "John B. Spindler" <spindler@k...> on Fri, 12 Apr 2002 13:35:45 +0900
My best guess (and everybody correct me if I'm wrong) is that you create
a String object message with a reference to the string "hello", when you
say message = "world" you have told Java to have message now point to
"world".  
Let's say your original String was " hello " with a space before and
after.  If you said: 

String message = new String(" hello ");
message.trim();   // trim is a method to remove leading and trailing
spaces
System.out.println(message);

This would yield " hello " because a string is immutable.  You can
change the handle in which an object is referenced to but you cannot
edit the string itself

John B. Spindler
Systems Engineer
KDDI Okinawa Co., Ltd.
Local: 926-2754
Commercial: 011-81-98-926-2754
spindler@k...


-----Original Message-----
From: zhangsc [mailto:zhangsc@n...] 
Sent: Friday, April 12, 2002 11:39 AM
To: Beginning Java
Subject: [beginning_java] String question
Importance: High


I have a question about String. I create a String,it likes:
  String message=new String("hello");
From book,I learned that since a string object is created,it's content
can't be modified.But when I modified it's content,like that:
  message="world";
or
  message=message+" world";
It's works well,Why?
Any ideas will be appreciated!
Regards,
Edward

---
Do you need true END-TO-END (e2e) Java Messaging (JMS)?  Softwired
offers industry's ONLY complete 100% JMS messaging solutions for your
needs TODAY. Visit
http://adtracking.wrox.com/track.asp?x=p2p%2Fe%2Fjava%2Dsoftwired&url=ww
w.softwired-inc.com Our unique product range includes: iBus//Mobile
(wireless data), iBus//MessageBus (IP Multicast), iBus//MessageServer
(store and forward) and much more!



Message #3 by zhangsc <zhangsc@n...> on Fri, 12 Apr 2002 10:39:25 +0800
I have a question about String. I create a String,it likes:
  String message=new String("hello");
From book,I learned that since a string object is created,it's content can't be modified.But when I modified it's content,like that:
  message="world";
or
  message=message+" world";
It's works well,Why?
Any ideas will be appreciated!
Regards,
Edward

  Return to Index