<?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>Cameron McKay &#187; generics</title>
	<atom:link href="http://cdmckay.org/blog/tag/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Exploring Java and VB.Net Syntax, Part 1</title>
		<link>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/</link>
		<comments>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 07:48:10 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 1.1]]></category>
		<category><![CDATA[arraylist]]></category>
		<category><![CDATA[generic programming]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vb6]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=93</guid>
		<description><![CDATA[The syntax of a programming language is a large part of how a programmer interacts with a language. Being somewhat of a programming language enthusiast, I&#8217;m always curious about how different programming languages stack up syntactically. Today, I&#8217;m going to take a look at an variety of programming tasks and show how they are done [...]]]></description>
			<content:encoded><![CDATA[<p>The syntax of a programming language is a large part of how a programmer interacts with a language.  Being somewhat of a programming language enthusiast, I&#8217;m always curious about how different programming languages stack up syntactically.  </p>
<p>Today, I&#8217;m going to take a look at an variety of programming tasks and show how they are done in both Java and VB.NET (the .NET 1.1 version).  Since both these languages have differing feature sets, I&#8217;ll try to only use features that both the languages have, in order to compare them fairly.  I know this might misrepresent the complexity of the languages, and I&#8217;m cool with that.  The purpose of this post is merely to explore and discuss the syntax of each language.</p>
<p><span id="more-93"></span></p>
<h3>Collections</h3>
<p>Creating and manipulating collections is a common task in <a href="http://en.wikipedia.org/wiki/Procedural_programming">procedural programming</a> languages, so let&#8217;s look at that first.  Consider arrays.  To make an array of 10 String objects in Java, I would do this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ARRAY_LENGTH <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span>ARRAY_LENGTH<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In VB.NET, things get a bit more complicated, but for the most part, you&#8217;d do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Const</span> ARRAY_LENGTH <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span>
<span style="color: #0600FF;">Dim</span> arr<span style="color: #000000;">&#40;</span>ARRAY_LENGTH <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span></pre></div></div>

<p>Woah, what?  Why is it ARRAY_LENGTH &#8211; 1?  That&#8217;s because VB.Net is an evolution of VB6, and in VB6, for reasons unknown, the value in the parenthesis is not the number of elements but instead the maximum index.  There&#8217;s a second way to create arrays in VB.NET using anonymous arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Dim</span> arr<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #808080;">&quot;foo&quot;</span>, <span style="color: #808080;">&quot;bar&quot;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>You may also create arrays using anonymous arrays in Java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> arr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;foo&quot;</span>, <span style="color: #0000ff;">&quot;bar&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now let&#8217;s look at something a bit more powerful: the ArrayList.  Both Java and VB have a version of this class with more or less the same syntax.  Let&#8217;s use them in a real world situation.  Consider this task: We have an ArrayList with a bunch of Strings and we want to join them together using a comma.  Here&#8217;s the code to do that in VB without using generics:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">' Option Strict is On (as it should be).</span>
<span style="color: #0600FF;">Dim</span> list <span style="color: #FF8000;">As</span> ArrayList <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> ArrayList
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;a&quot;</span><span style="color: #000000;">&#41;</span>
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;b&quot;</span><span style="color: #000000;">&#41;</span>
list.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;c&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the comma-joined string.</span>
<span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">str</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">String</span>.<span style="color: #0600FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;,&quot;</span>, _ 
    DirectCast<span style="color: #000000;">&#40;</span>list.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #804040;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF8000;">String</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>That&#8217;s quite a jumble at the end there.  Let&#8217;s see we would do it in Java (again, without generics):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the comma-joined string.  Let's assume that we've </span>
<span style="color: #666666; font-style: italic;">// created a method called join(String str, String[] strList) </span>
<span style="color: #666666; font-style: italic;">// that acts the same as .NET's String.Join().</span>
<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> join<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span>, <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>(Aside: Anyone familiar with generics is probably beating their head against their desk.  This example is just screaming for it.  I&#8217;ll include the generified version of the Java example at the end for the curious.)</em></p>
<p>Same problem in Java.  Converting from an ArrayList (or any Collection for that matter) does not look pretty (and is somewhat complicated for a novice programmer), even though it doesn&#8217;t seem like it&#8217;s a lot of ask.  We have a collection of things, and we want to put them into an array, which is a very basic type of a collection.  What&#8217;s the problem?</p>
<p>Basically, it has to do with the programming language not knowing what&#8217;s in the ArrayList.  If we just wanted to convert to an array of objects, that&#8217;s no problem:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Imagine we still have the &quot;list&quot; object </span>
<span style="color: #666666; font-style: italic;">// from the previous example.</span>
<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> objArray <span style="color: #339933;">=</span> list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works because the programming language can be sure that all of the items in the ArrayList are at least Objects.  Unfortunately, in most cases, that&#8217;s not especially helpful.  Fortunately, both these languages have a solution to the problem and that&#8217;s telling the programming language that the ArrayList is an ArrayList of Strings.  The technology that does that is called <a href="http://en.wikipedia.org/wiki/Generic_programming">generic programming</a>.</p>
<p>That&#8217;s it for today.  In part 2, we&#8217;ll delve deeper into the wordiness of VB.NET and how it drives you nuts if you program with it on a regular basis.  Here&#8217;s the generified version of the Java program for the keeners:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the comma-joined string.  Let's assume that we've </span>
<span style="color: #666666; font-style: italic;">// created a method called join(String str, String[] strList) </span>
<span style="color: #666666; font-style: italic;">// that acts the same as .NET's String.Join().</span>
<span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> join<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span>, list.<span style="color: #006633;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/04/exploring-java-and-vbnet-syntax-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

