Giulio Crisanti
Giulio Crisanti

Reputation: 380

Differences between variable type declarations?

Is there any difference between:

var a:Int

and:

var a = Int()

To me they look pretty much the same, but is there an obvious difference that should be known when staring out with programming?

Upvotes: 0

Views: 36

Answers (1)

Peyman
Peyman

Reputation: 3087

Here is the difference:

In the first one:

var a: Int

You're declaring a variable of type Int but there is no actual value assigned to it yet.

Whereas, in the second one:

var a = Int()

You're declaring variable a where you don't explicitly give it a type, but since you're setting it to Int() which is number zero, compiler can guess the type. So from the assigned value, compiler will infer the type.

To conclude, in the first one, there is no value assigned but in the second one, value of a is zero.

Upvotes: 2

Related Questions