Variables and parameters
Local variables
Local
variables can be defined at the beginning of the J
"block" (method body or anonymous methods). Their scope and life span are limited to their method
definition. Their definition syntax takes the following form:
class_name variable_name; // or
class_name variable_name =initial_value;
A variable
defined without any explicit initial value is initialized
according to its type.
|
Type |
Initial value |
|
int |
0 |
|
float |
0.0 |
|
boolean |
false |
|
String |
"" |
|
inStream, outStream |
a stream in the closed state |
|
other (non primitive) |
empty |
An explicit
initial value can be a literal value, a variable already defined (local
variable, attribute parameter) or an expression:
int i = 5;
int j = i;
String s = i.toString();
String n = Name;
Class c = this;
Feature[] som = PartFeature;
Predefined variables
Predefined
variables are as follows:
|
The ... variable |
represents ... |
|
this |
the current object. |
|
StdOut, StdFile |
the "standard screen" file for normal messages |
|
StdErr |
the "error standard exit" file. |
|
NL |
(New Line) String representing the passing to a new line. |
|
Tab |
(Tabulation) String representing the tabulation character. |
Method parameters
Methods can
have parameters.
Parameters behave in the same way as
local variables, and are defined as follows:
return_class class_name:method_name (in class1_name Parameter1_name,
inout class2_name
parameter2_name);
In general,
their form is:
Passing_mode Class_name parameter_name
Passing modes
Passing
modes indicate whether the caller (the emitter
of messages) should provide parameter values or whether he must wait for the
resulting value.
|
The ... mode |
defines a parameter as ... |
|
in |
input (provided by the caller). |
|
inout |
input/output (received by the caller). |
Note: Techniques such as Java "wrappers" are not necessary with J.
Actual parameter types
Passing
modes determine the actual parameter types that are compatible with those of formal
parameters.
|
With the ... mode |
the type of actual parameter must ... |
|
in |
be compatible (like the simple assignment) with the type of the formal parameter |
|
inout |
be identical to the formal parameter type. |
Return instruction
As in Java,
"return" exits the current method, with a value as parameter.
boolean Package :
Generate()
{
boolean Result;//false by default
...
return Result;
"return" is also used to exit a method which returns nothing. In this case, there is no expression after "return".