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'm always curious about how different programming languages stack up syntactically.

Today, I'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'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'm cool with that. The purpose of this post is merely to explore and discuss the syntax of each language.

Collections

Creating and manipulating collections is a common task in procedural programming languages, so let's look at that first. Consider arrays. To make an array of 10 String objects in Java, I would do this:

final int ARRAY_LENGTH = 10;
String[] arr = new String[ARRAY_LENGTH];

In VB.NET, things get a bit more complicated, but for the most part, you'd do something like this:

Const ARRAY_LENGTH As Integer = 10
Dim arr(ARRAY_LENGTH - 1) As String

Woah, what? Why is it ARRAY_LENGTH - 1? That'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's a second way to create arrays in VB.NET using anonymous arrays:

Dim arr() As String = New String() { "foo", "bar" }

You may also create arrays using anonymous arrays in Java:

String[] arr = new String[] { "foo", "bar" };

Now let'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'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's the code to do that in VB without using generics:

' Option Strict is On (as it should be).
Dim list As ArrayList = New ArrayList
list.Add("a")
list.Add("b")
list.Add("c")

' Create the comma-joined string.
Dim str As String = String.Join(",", _ 
    DirectCast(list.ToArray(GetType(String)), String()))

That's quite a jumble at the end there. Let's see we would do it in Java (again, without generics):

List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");

// Create the comma-joined string.  Let's assume that we've 
// created a method called join(String str, String[] strList) 
// that acts the same as .NET's String.Join().
String str = join(",", (String[]) list.toArray(new String[0]));

(Aside: Anyone familiar with generics is probably beating their head against their desk. This example is just screaming for it. I'll include the generified version of the Java example at the end for the curious.)

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't seem like it'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's the problem?

Basically, it has to do with the programming language not knowing what's in the ArrayList. If we just wanted to convert to an array of objects, that's no problem:

// Imagine we still have the "list" object 
// from the previous example.
Object[] objArray = list.toArray();

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's not especially helpful. Fortunately, both these languages have a solution to the problem and that's telling the programming language that the ArrayList is an ArrayList of Strings. The technology that does that is called generic programming.

That's it for today. In part 2, we'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's the generified version of the Java program for the keeners:

List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");

// Create the comma-joined string.  Let's assume that we've 
// created a method called join(String str, String[] strList) 
// that acts the same as .NET's String.Join().
String str = join(",", list.toArray());