This topic describes the data types and variable types that Cava supports.
Variable types
Cava allows you to define variables of two categories.
Local variables
Member variables of a class
Note: Cava does not support class variables. If you define class variables, a compilation error may occur. For more information, see Modifiers.
Local variables
Numeric variables
The following sample code provides an example on how to define a local variable named
aof theINTtype and assign10to the variable:
class Example {
// ...
int test() {
int a; // <==
a = 10;
return a + 1;
}
// ...
}The following sample code provides an example on how to define a local variable named
awith an initial value of10:
class Example {
// ...
int test() {
int a = 10; // <==
return a + 1;
}
// ...
}Object variables
After you define an object variable, you must use the new keyword to create a relevant object. Otherwise, the content of the object variable becomes null and a null pointer exception is thrown when you access the object variable.
class Example {
public double PI;
Example(double customPI) {
this.PI = customPI;
}
static int main() {
Example a = new Example(3.1415926); // <==
return 0;
}
}Array variables
The following sample code provides an example on how to define an array variable named
athat consists of10elements of theINTtype. The initial values of these elements are set to[0,1,2,3,4,5,6,7,8,9].
class Example {
// ...
int test() {
int[] a = new int[10];
for (int i = 0; i < a.length; ++i) {
a[i] = i;
}
return a[0];
}
// ...
}The following sample code provides an example on how to define an array variable that consists of elements of the
INTtype by using an initial value list. The initial values of these elements are set to[0,1,2,3,4,5,6,7,8,9]. Note: The compiler automatically infers the number of elements in the array.
class Example {
// ...
int test() {
int[] a = new int[]{0,1,2,3,4,5,6,7,8,9};
return a[0];
}
// ...
}The following sample code provides an example on how to define an object array. Note: After you define an object array, you must create an object for each element in the array.
class Example {
public double PI;
Example(double customPI) {
this.PI = customPI;
}
static int main() {
Example[] a = new Example[10]; // Create an object array.
for (int i = 0; i < a.length; ++i) {
a[i] = new Example(3.1415926); // Initialize the object for each element in the array.
}
return 0;
}
}Member variables of a class
For more information about how to define and use member variables of a class, see Classes and objects.
Built-in data types
Cava supports the following regular data types. The content in the parentheses () indicates the size of memory that a value of the specified type occupies.
BOOLEAN (1 byte)
BYTE (1 byte)
CHAR (2 bytes)
SHORT (2 bytes)
INT (4 bytes)
LONG (8 bytes)
FLOAT (4 bytes)
DOUBLE (8 bytes)
A value of the CHAR type is encoded in a fixed length of two bytes. This encoding format is similar to the UTF-16 encoding format in Java. The length of bytes in an encoded value is fixed, rather than variable.
Data type conversion
Similar to most programming languages, Cava can convert data types between each other. In specific cases, the compiler inserts code to automatically convert the source data type into the destination one.
Automatic conversion between compatible data types
The source and destination data types may have the following relationships:
For integer data types, the value range of the destination data type includes that of the source data type.
For floating-point data types, the precision of the destination data type is higher than or equal to that of the source data type.
In the preceding cases, the conversion is safe. For example, the conversion from the SHORT data type to the INT data type or that from the FLOAT data type to the DOUBLE data type is safe. The following examples show the code that the compiler may insert to implement the conversion:
float a = 3.14f;
double b = a;
short c = -1;
int d = c;The conversion between the data types in Cava is the same as that in other regular programming languages, such as Java and C++.
Explicit conversion
The precision of the destination data type may be lower than that of the source data type and the value range of the destination data type may not include that of the source data type. In such cases, the compiler checks the code and returns an error. Example:
double a = ...;
float b = a; // An error occurs. This conversion may cause the loss of precision.The conversion of these two types is not safe and the compiler forbids such automatic conversion. However, if you are aware of the potential risks, you can implement explicit conversion in the code to prevent errors that are reported by the compiler. Example:
float a = 3.14f;
int b = (int)a; // The fractional part is discarded. The value is rounded to an integer.Custom data types
Cava allows you to define classes and create objects. For more information, see Classes and objects.