gudii9
asked on
String example
>>You have created two strings containing names. Thus
String fname="John";
String lname="String"
How can you go about changing these strings to take new values within the same block of code?
1)
fname="Fred";
lname="Jones";
2)
String fname=new String("Fred");
String lname=new String("Jones");
3)
StringBuffer fname=new StringBuffer(fname);
StringBuffer lname=new StringBuffer(lname);
4) None of the above
i was going through above question, answer from following link
http://www.jchq.net/certkey/0802certkey.htm
It is not clear to me what exactly the question meant with respect to immutability
Any ideas, resources, links, sample code highly appreciated. thanks in advance.
String fname="John";
String lname="String"
How can you go about changing these strings to take new values within the same block of code?
1)
fname="Fred";
lname="Jones";
2)
String fname=new String("Fred");
String lname=new String("Jones");
3)
StringBuffer fname=new StringBuffer(fname);
StringBuffer lname=new StringBuffer(lname);
4) None of the above
i was going through above question, answer from following link
http://www.jchq.net/certkey/0802certkey.htm
It is not clear to me what exactly the question meant with respect to immutability
Any ideas, resources, links, sample code highly appreciated. thanks in advance.
Item 3) does not make much sense
All the rest seem reasonable
All the rest seem reasonable
ASKER
what is the best simple example on string immutability. please suggest.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>You cant change because string is final
>>
onces assign the value u cant reassign again .... but you can change like ...
String fname="John";
fname+="David";
now result is john David
are you saying we can change or not?
can you please elaborate on this. i was not clear.
>>
onces assign the value u cant reassign again .... but you can change like ...
String fname="John";
fname+="David";
now result is john David
are you saying we can change or not?
can you please elaborate on this. i was not clear.
You certainly can change.
String fname = "John";
at this point you can use fname and its value will be John
System.out.println(fname); wikill print John
then somewhere down the code you say
fname = "David";
from this point on you'll use fname and
System.out.println(fname) will print David
What they are saying is about inner wortkings of it
In fact when you say fname = "David"
it wiill not go into the same cell locations in memory
and replace character J woth chracter D.
It would rather create in memory somewhere new string David
and change the pointer associated
with fname to point at this new location.
In most of the cases you as a programmer woul not care about
these detaails, it is enoght to know fro you, that fname now points to David,
but in some situations
it is good to know about these inner workings
String fname = "John";
at this point you can use fname and its value will be John
System.out.println(fname);
then somewhere down the code you say
fname = "David";
from this point on you'll use fname and
System.out.println(fname) will print David
What they are saying is about inner wortkings of it
In fact when you say fname = "David"
it wiill not go into the same cell locations in memory
and replace character J woth chracter D.
It would rather create in memory somewhere new string David
and change the pointer associated
with fname to point at this new location.
In most of the cases you as a programmer woul not care about
these detaails, it is enoght to know fro you, that fname now points to David,
but in some situations
it is good to know about these inner workings
> are you saying we can change or not?
you can change them. But be aware you are creating new strings (not changing the contents of the string)
as strings are immutable
actually reread the question and the answer is actually 1)
It creates new strings and assigns then to the two variables
you can change them. But be aware you are creating new strings (not changing the contents of the string)
as strings are immutable
actually reread the question and the answer is actually 1)
It creates new strings and assigns then to the two variables
ASKER
>>can you go about changing these strings to take new values within the same block of code
so they are asking actually cell location character content right?
>>it wiill not go into the same cell locations in memory
and replace character J woth chracter D.
so since string create new object and points the reference there rather than changing the content(characters here) of each and every cell.
Where as stringbuffer change the content(characters here) of each and every cell. am i right? please advise
so they are asking actually cell location character content right?
>>it wiill not go into the same cell locations in memory
and replace character J woth chracter D.
so since string create new object and points the reference there rather than changing the content(characters here) of each and every cell.
Where as stringbuffer change the content(characters here) of each and every cell. am i right? please advise
Yes, you are right.
StringBuffer or StringBuilder function like an instance of a mutable Object
So they allocate a sequence of cells for character and then each cell can be individually
changed within that block.
Actual physical implemenatation may be more complex - these cells may be somehow scattered -
but for our purposese as a programmer, we don need to go into details how it happens in the actual chip -
it is sufficient to think it sthis way - yes it is ablock of cells-characters where we have accees
to indivual cells, we can take part of this block attach to another block and
doo all sorts of "construction" work from this block.
As opposed to String, where the whole blok can be just forgottoned and replaced with another sequnce with new
pointer to that new sequence, but no details inside can be relaced.
You may think of stringBuffer mmade of bricks
and string carved of one solid piece of marble if you will
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>strings to take new values within the same block of code
what the question mean when it says same block of code.please advise
what the question mean when it says same block of code.please advise
By block of code they mean something between the
braces within which these variables are defined;
as opposed to say you call some method - then you move to another
block of code - the one in the method
braces within which these variables are defined;
as opposed to say you call some method - then you move to another
block of code - the one in the method
ASKER
does the concept of string and string buffer changes if it is within same block of code or not. please advise
No, no concepts are affected. It is just the way to say - in this scope, in this method.
of this you are chnaging contents you are just playing with the
references