# Thursday, 28 December 2006
« New Snapshot | Main | IKVM 0.32 Released »
Object Schizophrenia

Early on in the design of IKVM, I decided that I didn't want to use wrapper objects to bridge the Java and .NET type systems as this leads to either object identity problems (and I just found out that this is sometimes called object schizophrenia and that triggered this post) or horrible performance.

By contrast, J# does use wrapper objects in a least one case and this does indeed lead to object schizophrenia:

public class Program
  public static void main(String[] args)
    String foo = "Foo";
    Comparable comp = foo;
    System.out.println(foo == comp);

Compiling this Java code with Visual Studio 2005 and running it produces false, while it clearly should print true.

While IKVM doesn't suffer from this, there is a somewhat related problem to look out for when using IKVM ghost interfaces from a .NET language:

using System;
using java.lang;

public class Program
  static void Main(string[] args)
    CharSequence seq = "foo";
    object obj1 = seq;
    object obj2 = seq;
    Console.WriteLine(obj1 == obj2);

Running this C# app produces False. This is because CharSequence is a ghost interface and represented by a value type. Assigning it to a local variable of type object boxes the value type instead of giving the underlying string object. Note that this only applies to ghost interfaces (i.e. java.lang.CharSequence, java.lang.Cloneable and java.io.Serializable). To fix this problem, you have to explicitly unwrap the object from the value type:

CharSequence seq = "foo";
object obj1 = seq.ToObject();
object obj2 = seq.ToObject();
Console.WriteLine(obj1 == obj2);

Unfortunately .NET doesn't have any mechanism for a value type to prevent (implicit) boxing or defining a custom implicit object conversion operator.

Thursday, 28 December 2006 14:07:23 (W. Europe Standard Time, UTC+01:00)  #    Comments [2]
Saturday, 30 December 2006 03:37:30 (W. Europe Standard Time, UTC+01:00)
Stupid question: what's different about Comparable that makes it not a ghost interface, compared to Cloneable, Serializable and CharSequence? It's not like System.String *really* implements that interface. Or does Comparable directly map to System.IComparable since there's a one-to-one mapping between their methods?
Monday, 17 November 2008 15:31:36 (W. Europe Standard Time, UTC+01:00)
This is only the simplest part of the problem.

Object Schizophrenia happens if the Object characterized by one or more of the following:

- Broken Identity.. i.e: Object's identity is a collection of other objects identities.

- Broken State.. same as Broken ID.
- Broken Interface.. same as Broken ID.

for more details on OS and OSP visit:
Abdullah O
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Comment (HTML not allowed)  

Live Comment Preview