Java: Define Terms Initialization, Declaration and Assignment

Java: define terms initialization, declaration and assignment

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.

Initializing Variables in Java

While declaring variables, compiler knows that somewhere in memory this variable exists and you can call this variable later.

Initializing variable means that you need to assign value before using this variable

What is the difference between declaration and definition in Java?

The conceptual difference is simple:

  • Declaration: You are declaring that something exists, such as a class, function or variable. You don't say anything about what that class or function looks like, you just say that it exists.

  • Definition: You define how something is implemented, such as a class, function or variable, i.e. you say what it actually is.

In Java, there is little difference between the two, and formally speaking, a declaration includes not only the identifier, but also it's definition. Here is how I personally interpret the terms in detail:

  • Classes: Java doesn't really separate declarations and definitions as C++ does (in header and cpp files). You define them at the point where you declare them.

  • Functions: When you're writing an interface (or an abstract class), you could say that you're declaring a function, without defining it. Ordinary functions however, are always defined right where they are declared. See the body of the function as its definition if you like.

  • Variables: A variable declaration could look like this:

      int x;

(you're declaring that a variable x exists and has type int) either if it's a local variable or member field. In Java, there's no information left about x to define, except possible what values it shall hold, which is determined by the assignments to it.

Here's a rough summary of how I use the terms:

abstract class SomeClass {                // class decl.
// \
int x; // variable decl. |
// |
public abstract void someMethod(); // function decl. |
// |
public int someOtherMethod() { // function decl. |
// | class
if (Math.random() > .5) // \ | def.
return x; // | function definition |
else // | |
return -x; // / |
// |
} // |
} // /

Declaring vs Defining - Java

String str; //declaraing
str = "My String"; //defining

Meanings of declaring, instantiating, initializing and assigning an object

Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name.

int a; //a is declared

Instantiate - Instantiating a class means to create a new instance of the class. Source.

MyObject x = new MyObject(); //we are making a new instance of the class MyObject

Initialize - To initialize a variable means to assign it an initial value.

int a; //a is declared
int a = 0; //a is declared AND initialized to 0
MyObject x = new MyObject(); //x is declared and initialized with an instance of MyObject

Assigning - Assigning to a variable means to provide the variable with a value.

int a = 0; //we are assigning to a; yes, initializing a variable means you assign it a value, so they do overlap!
a = 1; //we are assigning to a

Splitting Declaration and Assignment = Good Practice?

There's no reason to split the declaration and the assignment if you're just going to have them on consecutive lines. I'd only split them if the assignment were conditional, or if it needed to go in a separate code block (like a try/catch, or if the assignment goes in a constructor, etc.).

Difference between field definition and initialization?

Initializing Fields

public class Example {

// initialize to false
private boolean full = false;

// initialize to 2
public static int a= 2;

}

Field Definiton:

public class Employee {
String name;
String position;
int salary;
Date hiredDate;
}

Declaring vs Initializing a variable?

Edit: @ThisClark said something in the comments, and I went to prove him wrong, and upon reading the spec some more I learned something:

Here's an informative except from the specification:

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

Based on my reading of this, the following points describe the behavior (and "correct-ish" usage of the terms) you asked about in your question:

  • A variable declaration (e.g., var foo) causes that variable to be created as soon as the "lexical environment" is instantiated. For example, if that variable were defined within a function body, that function is the "lexical environment", and so variable creation coincides with the instantiation of the function itself.

  • Obviously, variable declarations may or may not be created with an initializer (i.e., the right-hand expression that resolves to the variable's initial value). That's a pretty non-specification usage of the term "initial value", though... let's dig into that a bit more:

  • Technically, per the specification notes here, all variables are initialzed with the value undefined:

    variables are created [...] and are initialized to undefined

    Emphasis on "technically" there, as this is largely academic if an initializer was also provided.

  • Based on what we've already covered, it should be understood that the statement var foo; could exist anywhere within a function body, and moving it anywhere else within the same function would have no effect on the runtime semantics of the function itself (regardless of if/where any actual assignments or other references to foo take place). If that's still confusing, re-read the previous points.

  • The last bit of behavior is the most intuitive part, and that's the assignment of the initializer. This assignment takes place when that line of code is actually executed (which, again, isn't the same point in time when the variable technically got created).

So, to quickly recap:

  • All variable declarations (that use var) are always initialized with undefined upon the initialization of their lexical environment.
  • This initialization probably doesn't count as an assignment, in the technical sense (ha, @ThisClark, you were wrong!!). :)
  • Assignments are the easy part, as they behave the way (and at the point in time) that you expect.

Hope that helps (and that I didn't terribly misinterpret the spec!).



Related Topics



Leave a reply



Submit