Many articles on the internet refer to these verbs, regardless of the programming language. But sometimes they are all confused or permuted, which causes a lot of confusion.
What does "instantiate", "initialize" and "declare" a variable mean?
Many articles on the internet refer to these verbs, regardless of the programming language. But sometimes they are all confused or permuted, which causes a lot of confusion.
What does "instantiate", "initialize" and "declare" a variable mean?
The definition of "declare" can be seen in What is the difference between declaration and definition? .
"Initialize" we can say that it is synonymous to assign a first value, not necessarily in the declaration.
"Instantiate" is to create an object, it is to put in memory a value for this object.
Object has nothing to do with object-oriented programming, objects exist in all paradigms .
You can instantiate an object:
An element of an array , collection, or a member of another object is also a variable.
When you do
var x = 1;
You are declaring a variable, initializing it with an instantiated value. This value is an instance of type number
in JS, or int
in C #. A type string
could be:
var a = "teste";
Some instantiations have no language literals and need to use constructors:
var i = new Classe(10, "texto");
The variable i
will store an instance (an object) of type Classe
) actually a reference to this instance).
Some objects are by others value by reference , this changes the memory location that will be stored .
Being very simple and objective to your question ...
What does "instantiate", "initialize" and "declare" a variable mean?
Instantiate refers to a class. Then you will instantiate in a variable the class.
var pessoa = new Pessoa()
Initialize is nothing else you assign a value to a variable. For example, you can not at times work with attributes of an object if you do not say before that variable is an object. In this case, we initialize the variable before using it. We just make it exist first.
var variavel = null;
var pessoa_object = {};
Declare is similar to initializing, however you do not necessarily have to assign a value.
int idade;