Variable Declarations, Typing, and Initialization

 

Python

Java

 

Form:

 

<variable> = <expression>

 

Examples:

 

x = 1

x = x + 3.14

 

A variable is introduced and set to an initial value by means of an assignment statement. 

 

Any variable can name any object and can be reset to any object.

 

A variable picks up the type of the object to which it is currently bound.

 

Type checking and checking for references to uninitialized variables are done at run time.

 

Forms:

 

<type> <variable>, , <variable>;

 

<type> <variable> = <expression>;

 

Examples:

 

int x, y;

x = 1;

y = 2;

int z = 3;

 

A variable has a type, which is specified when the variable is declared.  A variable can only be assigned a value that is compatible with its type.  Type incompatibilities are caught at compile time.

 

All instance and class variables are given default values when they are declared.  However, the compiler requires the programmer to assign temporary variables within methods a value before they can be referenced.  Thus, all variables are guaranteed to have a value at run time.

 

 

Previous

Index

Next