user531461
user531461

Reputation:

Is there automatic initialization for attributes?

I was wondering if attributes were automatically set to nil during an object's initialization or they have random values?

Upvotes: 1

Views: 518

Answers (3)

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

All instance variables are guaranteed to be initialized to nil or zero. This goes for non-object iVars as well, (int, BOOL, float).

Upvotes: 4

David Weiser
David Weiser

Reputation: 5205

If this question is indicative of Objective-C, I'd say that you should initialize all values of a variable when you declare them.

Explicitly initializing variables when they're declared gives you two benefits:

  1. There is no ambiguity in what the value of the variable is.
  2. Readability for others who read your code.

Upvotes: 1

Programmer
Programmer

Reputation: 6753

In Java, the data members of a class are set their default values. I.e string = null ,int =0.

Upvotes: -1

Related Questions