Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: define terms initialization, declaration and assignment

People also ask

What is initialization and assignment?

Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.

What is initialization in Java?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers.

What is declaration in Java?

One kind of Java statement is a declaration statement, which is used to declare a variable by specifying its data type and name. Below are some examples of declaration statements. A variable, in relation to Java programming, is a container that holds values used in a Java program.

What is difference between initialization declaration and definition?

For a variable, a definition is a declaration which allocates storage for that variable. Initialization is the specification of the initial value to be stored in an object, which is not necessarily the same as the first time you explicitly assign a value to it.


assignment: throwing away the old value of a variable and replacing it with a new one

initialization: it's a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.

declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.


String declaration;
String initialization = "initialization";
declaration = "initialization"; //late initialization - will initialize the variable.
    // Without this, for example, in java, you will get a compile-time error if you try 
    // to use this variable.

declaration = "assignment"; // Normal assignment. 
    // Can be done any number of times for a non-final variable

Declaration is not to declare "value" to a variable; it's to declare the type of the variable.

Assignment is simply the storing of a value to a variable.

Initialization is the assignment of a value to a variable at the time of declaration.

These definitions also applies to fields.

int i;  // simple declaration
i = 42  // simple assignment

int[] arr = { 1, 2, 3 };
// declaration with initialization, allows special shorthand syntax for arrays

arr = { 4, 5, 6 }; // doesn't compile, special initializer syntax invalid here
arr = new int[] { 4, 5, 6 }; // simple assignment, compiles fine

However, it should be mentioned that "initialization" also has a more relaxed definition of "the first assignment to a variable", regardless of where it happens.

int i; // local variable declaration
if (something) i = 42;
System.out.println(i);
  // compile time error: The local variable i may not have been initialized

This, however, compiles:

int i; // the following also compiles if i were declared final
if (something) i = 42;
else i = 666;
System.out.println(i);

Here i can be "initialized" from two possible locations, by simple assignments. Because of that, if i was an array, you can't use the special array initializer shorthand syntax with this construct.

So basically "initialization" has two possible definitions, depending on context:

  • In its narrowest form, it's when an assignment is comboed with declaration.
    • It allows, among other things, special array shorthand initializer syntax
  • More generally, it's when an assignment is first made to a variable.
    • It allows, among other things, assignments to a final variable at multiple places.
      • The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the final variable

There's also JVM-context class and instance initialization, OOP-context object initialization, etc.


Here is a short explanation with some examples.

Declaration: Declaration is when you declare a variable with a name, and a variable can be declared only once.

Example: int x;, String myName;, Boolean myCondition;

Initialization: Initialization is when we put a value in a variable, this happens while we declare a variable.

Example: int x = 7;, String myName = "Emi";, Boolean myCondition = false;

Assignment: Assignment is when we already declared or initialized a variable, and we are changing the value. You can change value of the variable as many time you want or you need.

Example:

int x = 7; x = 12; .......We just changed the value.

String myName = "Emi"; myName = "John" .......We just changed the value.

Boolean myCondition = false; myCondition = true; .......We just changed the value.

Note: In memory will be saved the last value that we put.


declaration: whenever you define a new variable with its type

assignment: whenever you change the value of a variable by giving it a new value

initialization: an assignment that is done together with the declaration, or in any case the first assignment that is done with a variable, usually it's a constructor call for an object or a plain assignment for a variable