-8

this question is unnecessary as I got the answer but stack overflow doesn't let me delete and I dont have any more questions to ask and also stack overflow on top of it tells me to improve this question. Literally confused.

  • 12
    If you're wondering what happens in an imaginary world where no string pool exists, then all can happen, since it's imaginary. – JB Nizet Jul 04 '18 at 08:39
  • The interviewer asked me this question and I was like why its not even worth talking about it because its not implemented like this but i am just confused now. You are right all can happen. I will change my focus now. Thanks – Naveen Kumar Jul 04 '18 at 08:42
  • There is a string pool. The question does not make sense. In the imaginary case where there weren’t, one might still imagine that the compiler would make sure the two string references refer to the same object, but that would be a language and standard library design question, so both answers are possible. – Ole V.V. Jul 04 '18 at 08:48
  • Why is this question on hold as **"too braod"**? It may lack precision with regards to "string interning", but it clearly is precise about testing "how objects work" (IMO, that was the point of the interview question). – ernest_k Jul 04 '18 at 08:54
  • 2
    I think the question is (rightfully) marked as **too broad** as we are talking about an imaginary scenario that simply does not exist. Such a scenario can unfold into multiple ways as it's impossible to predict what is actually going to happen. Which is the very definition of being "too broad too answer" in my opinion. By narrowing down the scope to "what would happen if instead XYZ would be the case" this might be answerable although I still think it would be more of a philosophical question... – Ben Jul 04 '18 at 08:58
  • @Ben The question doesn't set the context of an **imaginary world** (a comment does), it's about a world where the string pool/immutability didn't exist, and that's just one fact away from the real world... – ernest_k Jul 04 '18 at 09:03

2 Answers2

2

Even if the string pool does not exist. Since the String is immutable. When you assign s1 to a new string it changes just the reference "s1". So s2 still points to the same object.

  • 2
    ... which is handled by the String pool. – Stultuske Jul 04 '18 at 08:43
  • @Stultuske why are we adamant about dismissing the "pool didn't exist" part? – ernest_k Jul 04 '18 at 08:59
  • @Stultuske I mean, before the question of what the String pool does when it's up and running, there is what is produced at the compilation step. The Strings a class uses are listed as String constants in the compiled .class file. From there, normally, when a class is loaded, its constant Strings are obtained out of the String pool. If there was no String pool I guess one can imagine that they would just be built with new String(char[]) or something like that. In the real world, the compiler already detects identical String literals and only stores one constant. This imaginary world should too. – kumesana Jul 04 '18 at 09:22
  • @Stultuske , What do you mean it is handled by String pool ? String pool is not guaranteed by JVM Specification while String mutability is. –  Oct 29 '19 at 03:47
  • @SandeepShukla in Java, Strings are immutable, not mutable. – Stultuske Oct 29 '19 at 06:31
  • @Stultuske Yes, that was typo.Thanks for correcting it. –  Oct 29 '19 at 17:10
0

If there is no String pool Concept in Java

  • you may waste lots amount of memory

  • with mutability hackers will answer to your questions refer:-Why is String immutable in Java?

  • It may raise security issues

  • String is immutable due to the design, efficiency, and security Concerns.

Mr. Roshan
  • 1,777
  • 13
  • 33