Assignment Statements

 

Python

Java

 

Form:

 

<variable> = <expression>

 

Example:

 

x = 1

x = x + 3.14

 

Variables themselves are typeless.  Any variable can name any thing and be reset to any thing.  The object to which a variable refers has a type.

 

Form:

 

<variable> = <expression>;

 

Example:

 

int x;

x = 1;

x += 1;

 

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.

 

Values of less inclusive types can be assigned to variables of more inclusive types.  Reversing this order requires explicit type conversion before assignment.

 

Example:

 

double d;

d = 34;

int i;

i = (int)3.14

 

Previous

Index

Next