• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Immutability

 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am reading chapter 6 of Kathy and Sierra. I was just wondering, is the immutable nature of String a short-coming? If yes, then why?

Arhaan
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why would it seem so???
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In chapter 6, page 426, it is stated like this


The StringBuilder class has exactly same methods as the old StringBuffer class, but StringBuilder is faster because
it's methods are not synchronized. Both classes give you String like objects that handle some of the String class's
shortcomings (like IMMUTABILITY)


I am just wondering what this paragraph means. I know that String immutability is not a short-coming so whats the
crux of this sentence. May be i am understanding it wrong.

Thanks,
Arhaan
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a design flaw of String. But sometimes you want an immutable string, and sometimes you want a mutable one. It depends on how you're using it. So StringBuilder gives you something you can use when you need a mutable one.

Is a hammer poorly designed because you can't screw screws in with it?
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if i am guessing ir right, the only complain that you can have about immutability is when you are doing a lot of input and output, if you are using String objects, then a lot of them are being created and left in the String constant pool without a reference.I think the same can be sorted out if you use the StringBuilder and StringBuffer.


 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Mathew,

Thanks a ton for a wonderful explanation of the aspect. I know it is not a short-coming, i was only curious
why the line in the book wrote it as a short-coming. Just a curiosity. But i understand now why they must have
written it. Thanks a ton.

Thanks,
Arhaan
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just a turn of phrase. I'd use "limitation" rather than "short-coming". But the limitation is there for a reason.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input Jishnu.

Thanks a ton Mathews again for giving your feedback.

Arhaan
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading chapter 6 of Kathy and Sierra.


Kathy Sierra is one name The book is commonly referred as "Sierra and Bates".
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah Well,

Thanks for the correction, but i personally think, it depends upon the personal choice what we want to call.
Some people say it as K&B, in that case it should be known as S&B. (Again personal choice comes)

So i personally think, it's waste of time to comment over it. Thanks again for your information though

Arhaan
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, Arhaan and Singhania.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Karthik,

You are just landing a educational discussion on personal terms. I guess better you should concentrate on
answering some questions from other people in the forum rather then wasting time on such nitty gritty issues.
I already told you, good that you have given this input but it doesn't mean you keep the thread going for no
good reasons. Grow up.

Arhaan
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now lets all take a deep breath and calm down shall we. We try to be friendly to other people at this place. Lets put an end to this discussion and not escalate it...
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ankit,

I completely agree with you. I put a pause to this discussion from my end. You too take it easy Karthik,
sorry if i sounded a little harsh to you.

Arhaan
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I meant when I said "shortcomings"...

Java's got a huge API - that's because there's no single solution for every problem. For instance, if you look at the collections-related classes in java.util you will find that each class has its strengths and each has its weaknesses. Maps are better for some problems and Lists are better for others.

Oh, BTW, we prefer K&B
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bert,

Boss, i will make sure, i mention K&B from now on, sweet, short and simple. This forums rocks.
It was indeed a pleasure that you yourself addressed what i was confused about. Thanks a ton.

Arhaan
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those interested in the design flaws in the APIs themselves, there are a few. java.util.Stack / Properties are examples. But they are beyond the scope of the SCJP
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic