<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: The JVM needs Value Types</title>
	<atom:link href="http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/feed/" rel="self" type="application/rss+xml" />
	<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/</link>
	<description>Ben Hutchison's contribution to our Internet mind</description>
	<lastBuildDate>Tue, 01 Dec 2009 07:27:41 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Niclas Wiberg</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-257</link>
		<dc:creator>Niclas Wiberg</dc:creator>
		<pubDate>Wed, 05 Aug 2009 14:13:30 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-257</guid>
		<description>The really interesting question is: How to make the JVM optimize away a reference object into a value object?

This is in my view the most interesting question, since it appears difficult at this point to introduce a really new concept into the Java language. As far as I can tell, the main reason why the JVM can&#039;t make value types of immutable classes is the object identity problem.

So, my idea was to introduce a small change that lets the JVM know that object identity is not a problem for a particular class.

For instance, there could be a new abstract class java.lang.Immutable. Its subclasses would be required to have only primitive (or Immutable) final fields and to follow some rules for the equals() and hashCode() methods (essentially being based on the field values). Object identity (the == operator) would be defined as the same as equals(), and null-valued variables of Immutable type would not be allowed. This would allow a smart JVM to make inline value objects of variables declared as such classes.

There are some problems. For instance, an Immutable object might be assigned to an Object variable and used like a reference object. That would probably require that a &quot;real&quot; object is created for that value, so that it can have a reference. The question is what happens with object identity in that case. It could be solved by keeping a register of referenced immutable objects, sharing them when necessary so that there are never two unique but equal instances. A bit awkward, but it would preserve language semantics while only incurring a cost for the new Immutable classes, and only when they are used in the perhaps &quot;least interesting&quot; way. An alternative would be to have a special handling of &quot;==&quot; for variables declared as Object, checking whether they are Immutable or not. But that would come at a cost. Yet another alternative would be to let object identity be undefined for subclasses of Immutable. But that would break with Java language semantics.</description>
		<content:encoded><![CDATA[<p>The really interesting question is: How to make the JVM optimize away a reference object into a value object?</p>
<p>This is in my view the most interesting question, since it appears difficult at this point to introduce a really new concept into the Java language. As far as I can tell, the main reason why the JVM can&#8217;t make value types of immutable classes is the object identity problem.</p>
<p>So, my idea was to introduce a small change that lets the JVM know that object identity is not a problem for a particular class.</p>
<p>For instance, there could be a new abstract class java.lang.Immutable. Its subclasses would be required to have only primitive (or Immutable) final fields and to follow some rules for the equals() and hashCode() methods (essentially being based on the field values). Object identity (the == operator) would be defined as the same as equals(), and null-valued variables of Immutable type would not be allowed. This would allow a smart JVM to make inline value objects of variables declared as such classes.</p>
<p>There are some problems. For instance, an Immutable object might be assigned to an Object variable and used like a reference object. That would probably require that a &#8220;real&#8221; object is created for that value, so that it can have a reference. The question is what happens with object identity in that case. It could be solved by keeping a register of referenced immutable objects, sharing them when necessary so that there are never two unique but equal instances. A bit awkward, but it would preserve language semantics while only incurring a cost for the new Immutable classes, and only when they are used in the perhaps &#8220;least interesting&#8221; way. An alternative would be to have a special handling of &#8220;==&#8221; for variables declared as Object, checking whether they are Immutable or not. But that would come at a cost. Yet another alternative would be to let object identity be undefined for subclasses of Immutable. But that would break with Java language semantics.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Hutchison</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-256</link>
		<dc:creator>Ben Hutchison</dc:creator>
		<pubDate>Wed, 05 Aug 2009 11:38:52 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-256</guid>
		<description>Niclas,

Thanks for your comments.

Re: Changed semantics for value types (eg equals, locking, etc)... absolutely. One cant switch Integer between a Value type and a Ref type without breaking things like equals(). 

Java&#039;s Integer shouldn&#039;t be retrofitted anyway, since we already have &#039;int&#039;. Its more complex types that are more interesting/useful, like tuples and arrays.

.NET is IMO the best guide for how to do it decently.

I wouldnt expect to support locking over value types. locking over arbitary objects is widely seen as a near useless feature anyway, a mistake in Java&#039;s design. Use dedicated Monitors instead.

Re:</description>
		<content:encoded><![CDATA[<p>Niclas,</p>
<p>Thanks for your comments.</p>
<p>Re: Changed semantics for value types (eg equals, locking, etc)&#8230; absolutely. One cant switch Integer between a Value type and a Ref type without breaking things like equals(). </p>
<p>Java&#8217;s Integer shouldn&#8217;t be retrofitted anyway, since we already have &#8216;int&#8217;. Its more complex types that are more interesting/useful, like tuples and arrays.</p>
<p>.NET is IMO the best guide for how to do it decently.</p>
<p>I wouldnt expect to support locking over value types. locking over arbitary objects is widely seen as a near useless feature anyway, a mistake in Java&#8217;s design. Use dedicated Monitors instead.</p>
<p>Re:</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niclas Wiberg</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-255</link>
		<dc:creator>Niclas Wiberg</dc:creator>
		<pubDate>Wed, 05 Aug 2009 11:23:23 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-255</guid>
		<description>I would like to have support for value types in the JVM. But I think it may be necessary to support it on the source code level as well.

At first sight, it may appear that immutable classes (see http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/) could be treated as value types by the JVM. Indeed, the &quot;Automatic Object Inlining&quot; mentioned above seems to do exactly this. I doubt that it could be a general solution however, because of the problem of unique but equal instances.

For instance, Integer a = new Integer(3); Integer b = new Integer(3); creates two equal but unique objects.

If immutable classes like Integer would be implemented internally as value types, what would be the desired object identity semantics? For instance, when checking the identity of two variables using the == operator, the only sensible thing seems to be to use equals(). But that would break with current practice, for instance a == b is false in the above example.

Another problem is locking, owning the lock of object a above is not the same as owning the lock of object b, although the objects are equal.

I would guess that a JVM implementation of &quot;Automatic Object Inlining&quot; has to be very restrictive, making sure to avoid these problems. That probably means that such &quot;value types&quot; can not be stored in arrays. Which would be a pity because, as indicated in the article here, that is one of the really interesting scenarios for value types.

Perhaps it is a mistake to have public constructors on immutable classes, since they open up the possibility of creating unique equal objects. An immutable class should perhaps instead only provide object creation using static methods, like Integer.valueOf(int). That way, software should never rely on object identities.

What about this: New immutable classes should be written without public constructors, providing object construction using static methods. Such classes could get special treatment from the JVM, implementing them as value types when deemed suitable. Object identity would then be defined according to either the equals() method or just identity in all fields. Perhaps such classes should be annotated in a special way, or extend a common class java.lang.Immutable.</description>
		<content:encoded><![CDATA[<p>I would like to have support for value types in the JVM. But I think it may be necessary to support it on the source code level as well.</p>
<p>At first sight, it may appear that immutable classes (see <a href="http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/)" rel="nofollow">http://creativekarma.com/ee.php/weblog/comments/value_types_in_java/)</a> could be treated as value types by the JVM. Indeed, the &#8220;Automatic Object Inlining&#8221; mentioned above seems to do exactly this. I doubt that it could be a general solution however, because of the problem of unique but equal instances.</p>
<p>For instance, Integer a = new Integer(3); Integer b = new Integer(3); creates two equal but unique objects.</p>
<p>If immutable classes like Integer would be implemented internally as value types, what would be the desired object identity semantics? For instance, when checking the identity of two variables using the == operator, the only sensible thing seems to be to use equals(). But that would break with current practice, for instance a == b is false in the above example.</p>
<p>Another problem is locking, owning the lock of object a above is not the same as owning the lock of object b, although the objects are equal.</p>
<p>I would guess that a JVM implementation of &#8220;Automatic Object Inlining&#8221; has to be very restrictive, making sure to avoid these problems. That probably means that such &#8220;value types&#8221; can not be stored in arrays. Which would be a pity because, as indicated in the article here, that is one of the really interesting scenarios for value types.</p>
<p>Perhaps it is a mistake to have public constructors on immutable classes, since they open up the possibility of creating unique equal objects. An immutable class should perhaps instead only provide object creation using static methods, like Integer.valueOf(int). That way, software should never rely on object identities.</p>
<p>What about this: New immutable classes should be written without public constructors, providing object construction using static methods. Such classes could get special treatment from the JVM, implementing them as value types when deemed suitable. Object identity would then be defined according to either the equals() method or just identity in all fields. Perhaps such classes should be annotated in a special way, or extend a common class java.lang.Immutable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: benhutchison</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-157</link>
		<dc:creator>benhutchison</dc:creator>
		<pubDate>Thu, 02 Oct 2008 23:46:35 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-157</guid>
		<description>Art B, 

it could be done with annotations, however changing between value and references types changes the semantics (ie meaning/function) of a program. In general, you could not switch back an forth without breaking the software. In some limited cases, it seems you can, as is described in the link Ismael posted.</description>
		<content:encoded><![CDATA[<p>Art B, </p>
<p>it could be done with annotations, however changing between value and references types changes the semantics (ie meaning/function) of a program. In general, you could not switch back an forth without breaking the software. In some limited cases, it seems you can, as is described in the link Ismael posted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: benhutchison</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-156</link>
		<dc:creator>benhutchison</dc:creator>
		<pubDate>Thu, 02 Oct 2008 23:41:17 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-156</guid>
		<description>Ismael,

Thanks for the link. &quot;Automatic Object Inlining&quot; seems like clever stuff.

Ive been told that GHC Haskell automatically decides it can use unboxed records (ie value types) internally in some cases.</description>
		<content:encoded><![CDATA[<p>Ismael,</p>
<p>Thanks for the link. &#8220;Automatic Object Inlining&#8221; seems like clever stuff.</p>
<p>Ive been told that GHC Haskell automatically decides it can use unboxed records (ie value types) internally in some cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: benhutchison</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-155</link>
		<dc:creator>benhutchison</dc:creator>
		<pubDate>Thu, 02 Oct 2008 23:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-155</guid>
		<description>Denis: I&#039;m proposing to bring it into the JVM platform, /not/ Java the language. 

There is software it is simply not possible to build competitively on the JVM unless value types are used. To me, saying &quot;use another language&quot; is to admit defeat.</description>
		<content:encoded><![CDATA[<p>Denis: I&#8217;m proposing to bring it into the JVM platform, /not/ Java the language. </p>
<p>There is software it is simply not possible to build competitively on the JVM unless value types are used. To me, saying &#8220;use another language&#8221; is to admit defeat.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Art B</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-154</link>
		<dc:creator>Art B</dc:creator>
		<pubDate>Thu, 02 Oct 2008 21:44:11 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-154</guid>
		<description>Wouldn&#039;t it make more sense to  use annotations to denote classes the programmer feels are value-ish and let HotSpot them optimise them that way? That way if at a later date you want to extend them then you don&#039;t have to worry about making majour changes, HotSpot will just stop applying the optimization?</description>
		<content:encoded><![CDATA[<p>Wouldn&#8217;t it make more sense to  use annotations to denote classes the programmer feels are value-ish and let HotSpot them optimise them that way? That way if at a later date you want to extend them then you don&#8217;t have to worry about making majour changes, HotSpot will just stop applying the optimization?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ismael Juma</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-153</link>
		<dc:creator>Ismael Juma</dc:creator>
		<pubDate>Thu, 02 Oct 2008 04:13:52 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-153</guid>
		<description>Hi,

Although I think value types would be useful to have in the JVM, it&#039;s worth mentioning that there are HotSpot modifications that propose to get many of their benefits automatically. See the &quot;Automatic Object Inlining&quot; section of:

wikis.sun.com/display/HotSpotInternals/Publications+JKU

As usual, there are trade-offs when it comes to choosing an automatic versus manual approach.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Although I think value types would be useful to have in the JVM, it&#8217;s worth mentioning that there are HotSpot modifications that propose to get many of their benefits automatically. See the &#8220;Automatic Object Inlining&#8221; section of:</p>
<p>wikis.sun.com/display/HotSpotInternals/Publications+JKU</p>
<p>As usual, there are trade-offs when it comes to choosing an automatic versus manual approach.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Denis</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-152</link>
		<dc:creator>Denis</dc:creator>
		<pubDate>Tue, 30 Sep 2008 10:11:26 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-152</guid>
		<description>Used to code for Symbian. Now I consider to distinguish types in this particular  way as a bad practice. Frankly, old times they had one single part in a book just for string-types (it was a real pain). Nowdays they are trying to get rid of it in new Symnian OS versions. Don&#039;t bring this into Java. There are so many other languages for it. Just don&#039;t mess it into Java, please.</description>
		<content:encoded><![CDATA[<p>Used to code for Symbian. Now I consider to distinguish types in this particular  way as a bad practice. Frankly, old times they had one single part in a book just for string-types (it was a real pain). Nowdays they are trying to get rid of it in new Symnian OS versions. Don&#8217;t bring this into Java. There are so many other languages for it. Just don&#8217;t mess it into Java, please.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://benhutchison.wordpress.com/2008/06/15/the-jvm-needs-value-types/#comment-151</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Mon, 29 Sep 2008 06:11:39 +0000</pubDate>
		<guid isPermaLink="false">http://benhutchison.wordpress.com/?p=57#comment-151</guid>
		<description>Thanks man, now i really understand what is value types. I personally like an idea to bring them to JVM.</description>
		<content:encoded><![CDATA[<p>Thanks man, now i really understand what is value types. I personally like an idea to bring them to JVM.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
