Re: C# Fundamentals Part 3: ReferenceEquals question



Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:
| It prints "true" - meaning that x and y are references to the same
| object, despite them being the results of "new" expressions.

Both x and y are initialzed to an empty string (== String.Empty), which by
itself is an interned string. The result is that x and y are references to
the same string object.

Yes - but this is the only time it ever happens. You can't create a
reference to any other interned string using "new" - and it actually
violates the spec, which says:

<quote>
The new operator implies creation of an instance of a type
</quote>

In this case, no new instance is being created :(

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: File.lastModified *extremely* slow ?
    ... > he talks of the interned String data being kept outside the heap. ... He did claim in the discussion that interned String data is kept outside ... JLS, in particular: "Literal strings within the same class in the ...
    (comp.lang.java.programmer)
  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... |> itself is an interned string. ... The result is that x and y are references ... | reference to any other interned string using "new" - and it actually ... On the current version of the CLR, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ActionEvent Performance Issue
    ... Java does not prevent the "same" interned String from ... Since == compares object references, not the contents of the string, ... value as .equals() when .equalsitself returns the results you need ...
    (comp.lang.java.gui)
  • Re: How string literal is working
    ... The compiler takes care of it. ... String s1= new String ... The first let s reference an interned String. ...
    (comp.lang.java.programmer)
  • Re: Why String is Immutable?
    ... > That makes a String and then converts is to a StringBuffer ... it take the interned String in the constant pool and creates a ... > it just makes Java code more painful and boring to read. ...
    (comp.lang.java.programmer)

Loading