49

I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them?

The Circular Definitions

initialization: to initialize a variable. It can be done at the time of declaration.

assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.

declaration: to declare value to a variable.

[update, trying to understand the topic with lambda calc]

D(x type) = (λx.x is declared with type) 
A(y D(x type)) = (λy.y is assigned to D(x type))

%Then after some beta reductions we get initialization.
D(x type) me human                  // "me" declared with type "human"
A(y (D(x type) me human)) asking    // "asking" assigned to the last declaration

%if the last two statemets are valid, an initialization exists. Right?
1

8 Answers 8

91

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.

Sign up to request clarification or add additional context in comments.

4 Comments

SUMMARY? Initialization is a change from a starting value. Declaration is labeling with name and type. Assignment is a more general change in value, initialization a special type of assignment.
correct. Initialization is special just because it's the first assignment of a variable
I don't think that primitives hold 0 or false values pre-initialization, given that fact that when you try to print an uninitialized variable, it produces a compile time error saying that the variable might not have been initialized, rather than printing their value.
In Java, fields of an object are automatically initialized to "empty" values (null, zero, or false) if there is no explicit initializer clause. But local variables in a block of code are not automatically initialized, and any attempt to read the value of a local without writing a value to it first will result in a compile-time error. For more information, see chapter 16 of the Java Language Specification.
72
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

Comments

11

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.

2 Comments

"int[] arr = { 1, 2, 3 };" is arr-declared and {1,2,3}-assignment. It implies an initialization. Right?
Yes, that is an example of the special array initializer syntax being used, so it is initialization.
5

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.

Comments

3

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

5 Comments

I cannot understand the difference btw declaration and initialization.
initialization = declaration + assignment ?
it's more like initialization = first assignment (can be implicit for class fields, but has to be explicit for local variables)
Initalization is not dependent upon assignment. Unreferenced objects can be initialized. It just so happens that programmers often assign at the point of initialization. Initialization is the creation of a new Object.
@Finbarr: I think your confusing initialization with instantiation
1

I come from a C/C++ background, but the ideas should be the same.

Declaration - When a variable is declared, it is telling the compiler to set aside a piece of memory and associate a name (and a variable type) with it. In C/C++ it could look like this:

int x;

The compiler sees this and sets aside an address location for x and knows what methods it should use to perform operations on x (different variable types will use different access operations). This way, when the compiler runs into the line

x = 3 + 5;

It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.

Assignment - This is when you stuff a value into the previously declared variable. Assignment is associated with the 'equals sign'. In the previous example, the variable 'x' was assigned the value 8.

Initialization - This is when a variable is preset with a value. There is no guarantee that a variable will every be set to some default value during variable declaration (unless you explicitly make it so). It can be argued that initialization is the first assignment of a variable, but this isn't entirely true, as I will explain shortly. A typical initialization is a blend of the variable declaration with an assignment as follows:

int x = 6;

The distinction between initialization and assignment becomes more important when dealing with constants, such as this...

const int c = 15;

When dealing with constants, you only get to assign their value at the time of declaration/initialization. Otherwise, they can't be touched. This is because constants are often located in program memory vs data memory, and their actual assignment is occurring at compile time vs run time.

Comments

1

Step 1: Declaration : int a;

Step 2: Initialization : a = 5;

Step 3: Assignment: a = b; (ex: int b = 10 ; now a becomes 10)

Comments

0
Declaration
When we first create a variable of any Data Type is call Declaration. Example: 

int obj;
String str;

Initialization
When (Declaration is completed) or variable is created the assigned any value to this variable is call Initialization. Example 
    int obj = 10;
    String str = "Azam Khan";

Assignment
Reassign value to that variable that is already initialized is call Assignment. Example
int obj = 15;
obj = 20;       // it print **20** value.
String str = "simple"
str = "Azam Khan"  // it's result or print **Azam Khan** value...

Simple program

class ABC{
public static void main(String args[]){
int obj; // this is **Declaration:**
int  testValue= 12;   // After the **Declaration**, 
testValue is **Initialize**. 
testValue = 25;
System.out.println(textValue);
}
} 
the output result is 25.

it saves the value of 25.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.