What's the difference in instantiating, initializing and declaring a variable?

10

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?

    
asked by anonymous 09.02.2017 / 00:01

2 answers

11

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:

  • and put a value during variable declaration,
  • and initialize a variable at another point after the declaration,
  • and assign to a previously initialized variable, thus changing its value,
  • and pass as argument to a parameter ,
  • without storing in variable .

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 .

    
09.02.2017 / 00:27
0

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;
    
17.10.2017 / 23:52