All Products
Search
Document Center

OpenSearch:Data types and variable types

Last Updated:Feb 20, 2023

Cava allows you to define variables of two categories.

Define variables

Cava allows you to define variables of the following 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 a of the INT type and assign 10 to 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 a by using its initial value 10:

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 an 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 a that consists of 10 elements of the INT type. 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 10 elements of the INT type by initializing a 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 an 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]; // Define 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 classes, see Classes and objects.

Built-in data types

Cava supports the following common 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, the data types supported by Cava can be converted between each other. In specific cases, the compiler inserts new code to automatically convert the source data type into the destination one.

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 secure. 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 secure. 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 common 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 secure 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.

float a = 3.14f;
int b = (int)a; // The fractional part is discarded. The value is rounded to an integer.

Custom data type

Cava allows you to define classes and create objects. For more information, see Classes and objects.