ArticleslgStudy

computer science

Initialization (computer programming)

Initialization (computer programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Initialization (computer programming) rather than just read about it. In short: In computer programming, initialization or initialisation is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized.

Key takeaways

  • Initialization (computer programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Initialization (computer programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Initialization (computer programming) from memory before moving on to harder problems.

Reference excerpt

In computer programming, initialization or initialisation is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically called initializers and initializer lists. Initialization is distinct from (and preceded by) declaration, although the two can sometimes be conflated in practice. The complement of initialization is finalization, which is primarily used for objects, but not variables. Initialization is done either by statically embedding the value at compile time, or else by assignment at run time. A section of code that performs such initialization is generally known as "initialization code" and may include other, one-time-only, functions such as opening files; in object-oriented programming, initialization code may be part of a constructor (class method) or an initializer (instance method). Setting a memory location to hexadecimal zeroes is also sometimes known as "clearing" and is often performed by an exclusive or instruction (both operands specifying the same variable), at machine code level, since it requires no additional memory access.

C family of languages

Initializer In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces). The latter list is sometimes called the "initializer list" or "initialization list" (although the term "initializer list" is formally reserved for initialization of class/struct members in C++; see below). A declaration which creates a data object, instead of merely describing its existence, is commonly called a definition. Many find it convenient to draw a distinction between the terms "declaration" and "definition", as in the commonly seen phrase "the distinction between a declaration and definition...", implying that a declaration merely designates a data object (or function). In fact, according to the C++ standard, a definition is a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common. Although all definitions are declarations, not all declarations are definitions. C examples:

C++ examples:

Member initializer list In C++, a constructor of a class/struct can have an member initializer list (MIL) within the definition but prior to the constructor body. When an initialization list is used, the values are not assigned to the variable, they are initialized. In the below example, re and im are initialized to the constructor parameters: Example:

Here, the construct re{re}, im{im} is the initializer list. Sometimes the term "initializer list" is also used to refer to the list of expressions in the array or struct initializer. C++11 provides for a more powerful concept of initializer lists, by means of a template, called std::initializer_list<T>. If an exception is thrown during an initialization in the member initialization list, it cannot be caught by a try/catch block inside the constructor body; instead, one must use a "function try block":

Default initialization Data initialization may occur without explicit syntax in a program to do so. For example, if static variables are declared without an initializer, then those of primitive data types are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with their default constructors.

See also Declaration (computer programming) Finalizer Dispose pattern Object lifetime

References

Worked examples

Example 1 — a first encounter with Initialization (computer programming)

Start with the simplest possible case. Write down what Initialization (computer programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Initialization (computer programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Initialization (computer programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Initialization (computer programming)

In research
Initialization (computer programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Initialization (computer programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Initialization (computer programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming constructs, Variable (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Initialization (computer programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Initialization (computer programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Initialization (computer programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Initialization (computer programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Initialization (computer programming) in simple terms?

In computer programming, initialization or initialisation is the assignment of an initial value for a data object or variable. The manner in which initialization is performed depends on the programming language, as well as the type, storage class, etc., of an object to be initialized.

Why does Initialization (computer programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Initialization (computer programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Initialization (computer programming).

Tags

  • Programming constructs
  • Variable (computer science)

Keep exploring