<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiros.blog() &#187; Java</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Fri, 24 Sep 2010 07:42:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Two new C# 3.0 features that Java needs</title>
		<link>http://www.tzavellas.com/techblog/2005/09/28/two-new-c-30-features-that-java-needs/</link>
		<comments>http://www.tzavellas.com/techblog/2005/09/28/two-new-c-30-features-that-java-needs/#comments</comments>
		<pubDate>Tue, 27 Sep 2005 23:36:25 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=24</guid>
		<description><![CDATA[C# 3.0 is out. You can get the specification, a compiler other goodies from here. I am programming in Java almost exclusively for the last two years. In this post I will discuss two new features of C# 3.0 that I&#8217;d like to see in the next versions of Java. Below there is a code [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/vcsharp/future/">C# 3.0</a> is out. You can get the specification, a compiler other goodies from <a href="http://msdn.microsoft.com/vcsharp/future/">here</a>.</p>
<p>I am programming in <a href="http://java.sun.com">Java</a> almost exclusively for the last two years. In this post I will discuss two new features of <a href="http://msdn.microsoft.com/vcsharp/future/">C# 3.0</a> that I&#8217;d like to see in the next versions of <a href="http://java.sun.com">Java</a>.</p>
<p>Below there is a code fragment from the <a href="http://download.microsoft.com/download/4/7/0/4703eba2-78c4-4b09-8912-69f6c38d3a56/XLinq.wmv"><em>XLinq in action</em></a> video that demonstrates my two new favorite features (local variable type inference and lambda expressions):</p>
<pre>
var contacts = doc.Descendants("contact")
      .Where( a => (string)a.Attribute("category") == "home")
</pre>
<p>The above code fragment will query an <a href="http://www.w3.org/XML/">XML</a> document (the <code>XDocument</code> object that is assigned to the <code>doc</code> variable) to retrieve all <code>contact</code> elements with the <code>category</code> attribute matching <code>&quot;home&quot;</code>.</p>
<p>In the above code we didn&#8217;t specify the type of the <code>contacts</code> variable in the variable declaration. Instead we used the new <a href="http://msdn.microsoft.com/vcsharp/future/">C# 3.0</a> keyword <code>var</code>. When we use the keyword <code>var</code> in a variable declaration the type of the variable is inferred by the compiler for us and we do not have to specify it. This is called <em>local variable type inference</em>. The type is inferred at compile-time after checking the type of the expression on the right of the assignment.</p>
<p>Please note that the variable <code>contacts</code> has a type and its type remains the same (statically typed). You cannot reassign something that has a different type from the above expression to the contacts variable. This is in contrast with dynamically typed languages like <a href="http://www.python.org">Python</a> and <a href="http://www.ruby-lang.org/">Ruby</a> where you also do not declare the type of variables but the type of the variables is determined at run-time and the type of a variable can change dynamically (you can assign a string to a variable and then assign an integer to the same variable).</p>
<p>Three people have asked for this feature with a RFE to the <a href="http://bugs.sun.com/bugdatabase/index.jsp">SUN&#8217;s Java bug database</a>. All of the RFEs are in “Closed, will not be fixed” state because people at <a href="http://www.sun.com">SUN</a> believe that types in variable declarations provide readability and valuable redundancy.</p>
<p>Below I have included information about the RFEs.</p>
<p>Bug ID:  <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4879776">4879776</a><br />
Synopsis:  Constructor type inference (JSR14 + JSR65 ++)<br />
Submit Date: 17-JUN-2003</p>
<p>Bug ID:  <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4459053">4459053</a><br />
Synopsis:  Type inference for variable declarations<br />
Submit Date:  15-MAY-2001</p>
<p>Bug ID:  <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6242254">6242254</a><br />
Synopsis:  Language support for type inference<br />
Submit Date:  17-MAR-2005</p>
<p>I agree that repetition is good and many times helps readability but now that <a href="http://java.sun.com/j2se/1.5.0/index.jsp">Java 5</a> has generics the variable declarations can be very long and noisy.</p>
<p>Consider the below code fragments:</p>
<pre>
// 1. Non generics with construction
Pair p = new Pair(1, 2);

// 2. Generics with construction
Pair&lt;Integer , Integer&gt; p = new Pair&lt;Integer , Integer&gt;(1, 2);

// 3. Generics with type inference and construction
var p = new Pair&lt;Integer , Integer&gt;(1, 2);

// 4. Generics with method invocation
Pair&lt;Integer , Integer&gt; p = o.getPair();

// 5. Type inference with method invocation
var p = o.getPair();
</pre>
<p>Compare (1) with (2). (2) is very long compared to the non-generic version and the repetition is annoying. I believe that type inference in (3) is very clean and easy to read. Now add to this that most Java types have really long names. Breaking simple variable declarations to multiple lines isn&#8217;t very easy to read.</p>
<p>Now let us consider (4) and (5). In (4) we do not have repetition of the type information and the declaration is easy to read and understand. In (5) we have type inference and the programmer does not immediately see the type of the variable <code>p</code>. I agree that since the programs are read more times than they are written and if the programmer does not use a modern IDE (like <a href="http://www.eclipse.org">Eclipse</a>, <a href="http://www.netbeans.org">Netbeans</a> or <a href="http://www.jetbrains.com/idea/">IDEA</a>) this makes the program harder to understand. But if the language supports type inference a programmer is free to use (4) instead of (5) and (3) instead of (2) to get the best of both worlds.</p>
<p>I will also like to add that code like this</p>
<pre>
HashMap&lt;Integer , LinkedList&lt;Integer&gt;&gt; map =
  new HashMap&lt;Integer , LinkedList&lt;Integer&gt;&gt;();
</pre>
<p>is acceptable only because we use sophisticated IDEs with code complete (we have to write <code>HashMap&lt;Integer, LinkedList&lt;Integer&gt;&gt;</code> twice). Why the same argument cannot be used to accept the code in example (5)?</p>
<p>My conclusion is that we do not lose in type safety and we improve productivity and code readability with just a minor syntactic change. I wonder why statically typed languages like <a href="http://java.sun.com">Java</a> and <a href="http://msdn.microsoft.com/vcsharp/future/">C#</a> did adopt type inference earlier or from the start like functional languages as <a href="http://www.smlnj.org/">SML</a> and <a href="http://haskell.org/">Haskell</a>.</p>
<p>The other feature that I like is <em>lambda expressions</em>. Lambda expressions (or if you prefer closures) are the basic building block of functional programming. Lambda expressions can be found in functional programming languages like <a href="http://www.lisp.org/">LISP,</a> <a href="http://haskell.org/">Haskell</a> and <a href="http://www.smlnj.org/">SML</a> and also in modern scripting languages like <a href="http://www.perl.org/">PERL</a>, <a href="http://www.python.org/">Python</a>, <a href="http://www.ruby-lang.org/">Ruby</a> and <a href="http://groovy.codehaus.org/">Groovy</a>.</p>
<p>Now I might be a little biased because I learned to program in <a href="http://www.schemers.org/">Scheme</a> and <a href="http://www.smlnj.org/">SML</a> but I do believe that functional programming provides very elegant solutions to a lot of problems. IMHO the existence of lambda expressions in languages like <a href="http://www.ruby-lang.org/">Ruby</a> and <a href="http://groovy.codehaus.org/">Groovy</a> is one of the reasons why so many <a href="http://java.sun.com">Java</a> developers are using them.</p>
<p>Consider the below code where I have implemented the equivalent code with the  <a href="http://msdn.microsoft.com/netframework/future/linq/">XLinq</a> example in <a href="http://java.sun.com">Java</a> with <a href="http://dom4j.org/">dom4j</a>. (<a href="http://dom4j.org/">Dom4j</a> is a great and fast <a href="http://www.w3.org/DOM/">DOM</a> API for <a href="http://www.w3.org/XML/">XML</a> processing in <a href="http://java.sun.com">Java</a>)</p>
<pre>
Document doc = ...
Document contacts = new Document();
for ( Iterator i = doc.elementIterator("contact"); i.hasNext(); ) {
  Element contact = (Element) i.next();
  if ("home".equals(contact.getAttribute().getValue()))
    contacts.add(contact);
}
</pre>
<p>In my humble opinion the <a href="http://msdn.microsoft.com/vcsharp/future/">C#</a> version is not only more short but more clean and elegant. <a href="http://martinfowler.com/">Martin Fowler</a> recently wrote a <a href="http://martinfowler.com/bliki/CollectionClosureMethod.html">post</a> on his <a href="http://martinfowler.com/bliki/">blog</a> about how closures (lambda expressions) and collections fit nicely together. I suggest that you read it since it provides a lot of examples (in <a href="http://ruby-lang.org">Ruby</a>) where the functional way of doing things is simpler and more elegant.</p>
<p>There are two RFEs at the<a href="http://bugs.sun.com/bugdatabase/index.jsp"> SUN&#8217;s Java bug database</a> about adding lambda expressions to the Java programming language. Both of the RFE&#8217;s are in “In progress, request for enhancement” state so we can vote to bring this feature to <a href="http://java.sun.com">Java</a>.</p>
<p>Also Graham Hamilton, Mark Reinhold and Gilad Bracha in their “Evolving the Java Language” talk this year at JavaOne said that they are considering to add closures/method references (lambda expressions) to the next version of Java. (Please note that I wasn&#8217;t at JavaOne and I have only seen this in their slides. I am eagerly waiting for the multimedia content of the presentations to become available online).</p>
<p>If you are a <a href="http://java.sun.com">Java</a> programmer who things that lambda expressions (closures) could be useful in the <a href="http://java.sun.com">Java</a> programming language take five minutes of your time and vote for both of the below RFEs.</p>
<p>Bug ID: <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5014235">5014235</a><br />
Synopsis: Closures support instead of annonymous inner classes<br />
Votes: 13<br />
Submit Date: 16-MAR-2004</p>
<p>Bug ID: <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4483171">4483171</a><br />
Synopsis: Adding support for delegates and method references<br />
Votes: 8<br />
Submit Date: 23-JUL-2001</p>
<p>BTW <a href="http://blogs.tedneward.com/">Ted Neward</a> has written a very good <a href="http://blogs.tedneward.com/CommentView,guid,a938fb77-c9ad-41b0-ba3b-28bd9eeddbc0.aspx">overview</a> of all the new C# 3.0 features.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/09/28/two-new-c-30-features-that-java-needs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://download.microsoft.com/download/4/7/0/4703eba2-78c4-4b09-8912-69f6c38d3a56/XLinq.wmv" length="3756970" type="video/x-ms-wmv" />
		</item>
	</channel>
</rss>

