1

One of the many advantages of an immutable class is that their internals can actually be shared somehow in other related classes.

What can be cited as an example of this technique from core java library ? Why does this technique work ?

Edit : This was actually asked in an Oracle interview to me .

James P.
  • 19,313
  • 27
  • 97
  • 155
Geek
  • 26,489
  • 43
  • 149
  • 227
  • String class is one such example - or so I guess. – nhahtdh Jul 31 '12 at 03:20
  • The Java library source code is easily browsable in a number of places. Since SO is not a discussion site, this type of question is kind of off-topic. – Jim Garrison Jul 31 '12 at 03:21
  • @nhahtdh how does String go on to achieve this ? I do not need the details here , just the concept . – Geek Jul 31 '12 at 03:29
  • @Geek, `String` is a fairly thin wrapper around `char[]` values, with a start and end index. Given this, it's fairly easy to implement a lightweight sub-string operation (same `char[]`, different start and end values). – clstrfsck Jul 31 '12 at 03:48

1 Answers1

3

One of the many advantages of an immutable class is that their internals can actually be reused somehow in other related classes.

I don't buy that argument / premise:

  • Java doesn't allow you to reuse the internals of a class ... unless you count copy-and-paste programming a legitimate reuse strategy. In Java, you reuse the entire class, or none of it.

  • You can reuse mutable classes in the same way as immutable classes.

  • If you need the entities to be mutable, the immutability in the original class cab actually be an impediment to reuse.


This premise is actually written out in Effective Java as mentioned in this answer that got 121 up votes.

Big deal! Just because you quoted (without any context) from a well known book doesn't make your premise credible. Lots of well known books say things that are debatable or even plain wrong.

Just because 121 people voted for an Answer that quoted this one line (among a number of others) doesn't add to the credibility. They were voting for the entire answer, not necessarily that sentence. Besides, votes don't indicate objective correctness. They indicate subjective agreement.

If this was a debating / discussion forum, you should counter my arguments with proper arguments of your own. But it is not, and this Question has (IMO rightly) been closed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This premise is actually written out in Effective Java as mentioned in this [answer](http://stackoverflow.com/questions/93091/why-cant-strings-be-mutable-in-java-and-net?lq=1) that got 121 up votes – Geek Jul 31 '12 at 03:41
  • I have edited my question . It would be "share" instead of "reuse" . Sorry for the confusion. – Geek Jul 31 '12 at 03:43
  • @Geek - share and reuse mean the same thing to me in this context. – Stephen C Jul 31 '12 at 04:06
  • What about msandiford's comment ? subString operation doesn't reuse the internals of a String ? – Geek Jul 31 '12 at 04:13
  • @Geek - I don't actually see the relevance of msandiford's (or nhahtdh's) comment to your question. String.substring is not sharing internals with a related class. It is sharing internal state between different instances of String. Indeed, String is `final` so you *can't* share state with different classes. – Stephen C Jul 31 '12 at 13:45
  • + 1 for the explanation . Thanks for clarifying my doubt. – Geek Jul 31 '12 at 13:52