1

Question 1: So in Java, when you alter a string it, creates a new string object if its not in the pool because its immutable. so the following:

String s = "Lunch Time Doubly so";
s = s.substring(0, 10);

would create 3 String objects (s, s.sub, and the new reference to s), so does:

s = s.substring(0,2) & " sometext " & s.substring(2, 2);

create 3 String objects?

Question 2 so in .net does the same weight hold? for example in .net does:

dim s as string = "0213"
s = s.substring(0, 2) & " days and " & s.substring(2, 2) & " hours since you rocked on to Electric Avenue"

create 4 String objects?

Argyle Ghost
  • 141
  • 5
  • 14
  • 1
    There is a gap somewhere.. in the first example, there would be no more than two strings created with one coming from the string literal (and thus interned). – user2864740 Apr 01 '16 at 23:37
  • Just to note, `"a" & "b"` doesn't mean two strings, and it *may* be optimized as `ab` by compiler.. – Eser Apr 02 '16 at 00:06
  • In java if you have String s = "abc"; and then s.substring(0,1); There has been 2 strings created, but s still equals "abc" and the s.substring(0,1) goes to the garbage collection cause there is no reference pointing to it. So technically there are 2 strings that have been created. I am not talking about just doing "a" & "b". that is totally different. – Argyle Ghost Apr 02 '16 at 00:17
  • You may find this [question](http://stackoverflow.com/questions/2365272/why-net-string-is-immutable) and answers to it somewhat helpful. –  Apr 02 '16 at 01:53

0 Answers0