473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String Immutable - Explain

I Knew Java String is Immutable. Because we cannt change the value of String
object when it was created. explain with one code . thank you
Mar 1 '08 #1
4 3155
JosAH
11,448 Expert 8TB
I Knew Java String is Immutable. Because we cannt change the value of String
object when it was created. explain with one code . thank you
A lot of String instantiations share their char[] data buffer without knowing who their
co-owners of the buffer are. Changing the buffer would result in changing all the
co-owners' buffers and result in a mess.

kind regards,

Jos
Mar 1 '08 #2
BigDaddyLH
1,216 Expert 1GB
I Knew Java String is Immutable. Because we cannt change the value of String
object when it was created. explain with one code . thank you
Explain what?

* The definition of "immutable"?
* The design decision that String ought to be immutable?
* How String's API demonstrates string is immutable?
Mar 2 '08 #3
gaya3
184 100+
Explain what?

* The definition of "immutable"?
* The design decision that String ought to be immutable?
* How String's API demonstrates string is immutable?
Hi,
I have some explanation to answer your query..
i) Definition of "immutable": "immutable" means "unchange" or "not able to change".
ii)String ought to be immutable:
please try out the following example..it explains good...
Expand|Select|Wrap|Line Numbers
  1. class String_immutable{
  2.          public sttaic void main(String args[]){
  3.           String s1 = new String("StringValue");
  4.           StringBuffer s2 = new StringBuffer("String buffer value");
  5.           s1.replace('a','b');
  6.           s2.replace(0,2,"zzz");
  7.           System.out.println("\n String value after replace "+s1+"  String buffer  
  8.                                       after replace  "+s2);
  9.         }
  10.     }
From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.

iii) Api's:
you may try out with any API's . for better understanding "replace()" can be used..

-Thanks & Regards,
Hamsa
Mar 5 '08 #4
sukatoa
539 512MB
Hi,
I have some explanation to answer your query..
i) Definition of "immutable": "immutable" means "unchange" or "not able to change".
ii)String ought to be immutable:
please try out the following example..it explains good...
class String_immutable{
public sttaic void main(String args[]){
String s1 = new String("StringValue");
StringBuffer s2 = new StringBuffer("String buffer value");
s1.replace('a','b');
s2.replace(0,2,"zzz");
System.out.println("\n String value after replace "+s1+" String buffer
after replace "+s2);
}
}
From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.

iii) Api's:
you may try out with any API's . for better understanding "replace()" can be used..

-Thanks & Regards,
Hamsa
Please use code tags, this thread might be useful for future members

Sukatoa ;-)
Mar 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string....
6
by: AT | last post by:
Can someone please explain me the following: quote from documentation "Unlike other intrinsic data types, String is a reference type. When a variable of reference type is passed as an argument...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
9
by: amyxmas | last post by:
I am trying to write a routine which follows this logic, but not sure how to implement: <script> xxx = "... apple green yellow blue ..."; {right here I need to modify part of the string, so...
30
by: nano2k | last post by:
Hi I need an efficient method to convert a string object to it's byte equivalent. I know there are LOTS of methods, but they lack in efficiency. All methods allocate new memory to create the byte...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.