Strings in Switch .. and classes

Mark Thornton mthornton at optrak.co.uk
Wed Dec 16 11:28:06 PST 2009


Paul Benedict wrote:
> Tom,
>
> On Wed, Dec 16, 2009 at 12:37 PM, Tom Hawtin <Thomas.Hawtin at sun.com> wrote:
>   
>> Paul Benedict wrote:
>>
>>     
>>> Class c = o.getClass();
>>> if (c instanceof String) {  .. }
>>>       
>>> Can be de-sugared into the new String switch:
>>>
>>> switch (object.getClass().getName()) {
>>>  case "java.lang.String":
>>>       
>> Class names are not unique.
>>
>>     
>
> Can you expound on this some more? I am surprised, so I want to hear
> more about it. I thought these two are equivalent, no?
>
> boolean x = c instanceof String;
> boolean y = c.getName().equals("java.lang.String");
>
> Paul
>
>   
Classes of the same name can be loaded by different ClassLoader's, the 
full identity of a Class is a pair of (ClassLoader, classname). This 
shouldn't happen with classes like String which are part of the base 
platform.


Mark Thornton




More information about the coin-dev mailing list