[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String example

Posted on 2011-04-26
15
Medium Priority
?
325 Views
Last Modified: 2012-05-11
>>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.
0
Comment
Question by:gudii9
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35471773
This does not have to do with mutability, in none
of this you are chnaging contents you are just playing with the
references
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35471794
Item 3) does not make much sense

All the rest seem reasonable
0
 
LVL 7

Author Comment

by:gudii9
ID: 35471799
what is the best simple example on string immutability. please suggest.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 35471800
Its asking how you can change the string values.
1 and 2 won't worl because String is immutable
3 will work because StringBuffer is mutable
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 200 total points
ID: 35473187
You cant change because string is final

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

 onces assign the value u cant reassign again ....  but you can change like ...

String fname="John";
fname+="David";  

now result is john David
0
 
LVL 7

Author Comment

by:gudii9
ID: 35748135
>>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.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35748281
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

0
 
LVL 92

Expert Comment

by:objects
ID: 35751316
> 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
0
 
LVL 7

Author Comment

by:gudii9
ID: 35888747
>>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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35888816

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


0
 
LVL 47

Accepted Solution

by:
for_yan earned 1400 total points
ID: 35888960
I actually started thinking that analogy of bricks vs block of marble
works pretty well.

With StringBuffer you can add more bricks and write more charcters on them ,
you can replace bricks/charcaters - all the time using and reusing the same
set of bricks or at least some part of it.

With marble you cannot do all of that.
If you had a piece of marble where some string was etched, and now you want
another string; pretty much your only option is to throw away
that piece of marble and work with the new one.
After you did it, you can stiil attach yor piece of marble
tto the same facade of the building (use the same variable to point at
the string), but in fact there is nothing left of your old
piece of marble, you completely replaced it with a new one.
That's what happens, when you change
String s = "Peter;
s="John";

With string buffer - you still can use the same bricks or maybe replace some of them.
Let me knwo if something is still not clear.
 
0
 
LVL 7

Author Comment

by:gudii9
ID: 36004851
>>strings to take new values within the same block of code

what the question mean when it says same block of code.please advise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36004872
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 36012135
does the concept of string and string buffer changes if it is within same block of code or not. please advise
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36012151
No, no concepts are affected. It is just the way to say - in this scope, in this method.


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question