Creating Data Items and Objects
Types:
All variables in Java have a "type" whether a primitive type such as
integer or string or an object type. A primitive type variable actually
contains data of the variable type while an object type variable contains
a "reference" or "handle" which the JVM can use to locate the assoicated
object as needed. The type of a variable must be declared before it can
be used, although you can declare a variable and assign it an initial value
in the same statement. Here are some examples:
Declaring and initializing variables in Java and NetRexx
|
Java
|
NetRexx
|
int i;
|
i = int
|
String s;
|
s = String
|
int j = 99;
|
j = 99
|
String s = "Hello there";
|
s = "Hello there"
|
Clown bozo;
|
bozo = Clown
|
Clown otherclown = bozo
|
otherclown = Clown bozo
|
Note that it is not necessary to declare a type for primitive variables
in NetRexx because NetRexx has it's own default primitive variable type
called a "Rexx" variable, which can hold all primitive types of data as
well as strings.
The "new" keyword:
To actually create an object from a class, there is a special "new" keyword
in Java:
bozo = new Clown();
In NetRexx the "new" keyword is not used:
bozo = Clown()
The parens indicate a call to the class "constructor" to create an object
of the class type.
Constructors:
What really happens when you create a new object?
-
The variables defined in the class are created and initialized.
-
A method in the class with the same name as the class is called for complex
object setup and initialization. (If you did not provide one, Java inserts
it for you!)
The above is a very simplified look at creating data and objects in Java
- the details come later.