Cava type and variable definitions

更新时间:
复制 MD 格式

Cava supports the following two types of definitions.

Variable definitions

Cava supports the following two type definitions.

  • Local variables

  • Class member variables

Note: Cava does not support class variables. Defining a class variable causes a compilation error. For more information, see Cava modifiers.

Local variables

Numeric type variables

  • Define a local variable a of the int type and assign it the value 10.

class Example {
    // ...
    int test() {
        int a; // <==
        a = 10;
        return a + 1;
    }
    // ...
}
  • Define a local variable a with the 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 statement to create the corresponding object. Otherwise, the value of the variable is null. Accessing a null variable causes a NullPointerException.

class Example {
    public double PI;
    Example(double customPI) {
        this.PI = customPI;
    }
    static int main() {
        Example a = new Example(3.1415926); // <==
        return 0;
    }
}

Array variables

  • Define an int array a with 10 elements, initialized to [0..9].

class Example {
    // ...
    int test() {
        int[] a = new int[10]; 
        for (int i = 0; i < a.length; ++i) {
            a[i] = i; 
        }
        return a[0];
    }
    // ...
}
  • You can use an initialization list to define and initialize an int array with the values [0..9]. The compiler automatically infers the array length.

class Example {
    // ...
    int test() {
        int[] a = new int[]{0,1,2,3,4,5,6,7,8,9}; 
        return a[0];
    }
    // ...
}
  • Create an array of objects. Note: After creating the array, you must also create an object for each of its elements.

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 each object in the array.
        }
        return 0;
    }
}

Class member variables

For more information about defining and using class member variables, see Cava classes and objects.

Built-in types

Cava provides the following built-in types. The value in parentheses indicates the memory size of the type.

  • boolean (1 byte)

  • byte (1 byte)

  • char (2 bytes)

  • short (2 bytes)

  • int (4 bytes)

  • long (8 bytes)

  • float (4 bytes)

  • double (8 bytes)

Note that the `char` type uses a fixed-length 2 byte internal encoding. This encoding is similar to Java's UTF-16 encoding format, but the `char` type itself is not variable-length.

Type conversions

As in most languages, built-in numeric types can be converted. The compiler inserts code to perform conversions between numeric types when appropriate.

Automatic conversion of compatible types

When the source and target types have the following relationship:

  • The numeric range of the target type includes the numeric range of the source type. This applies primarily to integer types.

  • The conversion from the source type to the target type does not cause a loss of numeric precision. This applies primarily to floating-point types.

Certain conversions, such as from short to int or from float to double, are safe. The compiler automatically inserts code to perform these conversions:

float a = 3.14f;
double b = a;

short c = -1;
int d = c;

Cava follows the same type conversion rules as other common programming languages, such as Java and C++.

Explicit Type Casting

Some numeric type conversions can cause a loss of precision or a range mismatch. In such cases, the compiler reports an error. For example:

double a = ...;
float b = a; // error, risk of precision loss

The compiler does not automatically perform these conversions because they are generally unsafe. However, if you understand the risks involved, you can use an explicit type cast in your code to prevent a compiler error.

float a = 3.14f;
int b = (int)a; // The fractional part is discarded. Only the integer part is kept. This truncates the number.

Custom types

Cava supports defining classes and using objects. For more information, see Cava classes and objects.