Sorry, but I don't see the difference between
Dim s As String
and
Dim a As ArrayList
In neither case did you actually *create* anything. Both of those are just variables that have the *potential* to reference an object of the given type.
The fact that you can then do
s = "Foo"
whereas with the ArrayList you must do
a = New ArrayList
has nothing to do with the *declarations*, per se.
It just so happens that
VB.NET (and, indeed, most languages) has a convenient way to specify a literal value for a String but there's no similarly easy way to create a "literal ArrayList".
In point of fact,
s = "foo"
is really a sort of shorthand for
s = New String("foo")
because don't forget that you *could* do
Dim ca() As Char = {Chr(&h66), Chr(&H6f), Chr(&H6f)
s = New String(ca)
(and yes, if you write that variable
s, you will see "foo")
And the same is true of
Dim sa(4) As String
versus
Dim aa(4) As ArrayList
Again, neither of those has *created* anything at all except an array of empty object references. You still need to do
sa(0) = "first"
sa(1) = "second"
...
aa(0) = New ArrayList
aa(1) = New ArrayList
****************
In summary, I think that making a major distinction between String and any other class is a mistake. Instead, just emphasize that the language(s) treat a string literal (something in quotes) as a special case of a pre-constructed string object.
****************
Now...
The situation is much different when it comes to the primitive types: Integer, Boolean, Double, etc.
All primitive types really *are* initialized when you DIM them.
Dim i As Integer
Dim ia(17) As Integer
And all the primitive types are initialized to a value of zero (or the equivalent for the given type: i.e., 0.0 for Double, False for Boolean.)
Primitive types don't have a constructor and so you can't possibly use NEW with them.
************
"Wait," you say, "What about creating dynamic arrays?"
Dim ai() As Integer
Dim x As Integer
x = 98
ai = New Integer(x) ' a dynamic array
Strictly speaking, you don't need this capability in
VB. You *could* instead code that as
Dim ai() As Integer
Dim x As Integer
x = 98
ReDim ai(x)
But the New form is more conventional in other languages and
VB has implemented it. In any case, the effect is the same. Until you define a *size* for an array, the variable's value (the value of
ai, above) is Nothing. All you have really done is deferred defining the *size*, and thus creating the array-of-values-all-initialized-to-zero, until you decide what size you need.
************
This is all a bit simplistic. There are some pretty subtle differences between declaring a fixed size array and a dynamic array, but they are mostly (or maybe all?) holdovers from prior versions of
VB.
We'll see whether and how much Brian Wren agrees with me.
***************************
***************************
ADDENDUM:
The more I think of it, the more I'm sure that the right way to explain string literals is by saying that
"foo"
is just language shorthand for
New String( {Chr(&h66), Chr(&H6f), Chr(&H6f)} )
Because, under the covers, this is exactly what the compiler does. It creates an array of Char (characters) and, from that, creates an instance of a String object. The fact that this is done at compile time, to avoid the overhead of a run-time NEW operation, is "syntactic sugar": a convenience for the user.