Data types and variable definitions in Cava
Cava supports two types of variable definitions: local variables and member variables of a class. This page covers both, along with the built-in primitive types and how type conversion works.
Cava does not support class variables. Defining a class variable causes a compilation error. For details, see Cava modifiers.
Variable definitions
Local variables
Numeric variables
Declare a local variable a of type int, then assign the value 10:
class Example {
// ...
int test() {
int a; // <==
a = 10;
return a + 1;
}
// ...
}Declare and initialize a in a single statement:
class Example {
// ...
int test() {
int a = 10; // <==
return a + 1;
}
// ...
}Object variables
Always use the new keyword to create the object after declaring an object variable. Without new, the variable holds null, and accessing it throws a null pointer exception.
class Example {
public double PI;
Example(double customPI) {
this.PI = customPI;
}
static int main() {
Example a = new Example(3.1415926); // <==
return 0;
}
}Array variables
Fixed-size array — declare an int array of 10 elements and populate it with values 0 through 9:
class Example {
// ...
int test() {
int[] a = new int[10];
for (int i = 0; i < a.length; ++i) {
a[i] = i;
}
return a[0];
}
// ...
}Initializer-list array — declare an int array with an explicit list of initial values. The compiler infers the element count automatically:
class Example {
// ...
int test() {
int[] a = new int[]{0,1,2,3,4,5,6,7,8,9};
return a[0];
}
// ...
}Object array — after declaring an object array, create an object for each element individually:
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;
}
}Member variables of a class
For how to define and use member variables, see Classes and objects.
Built-in data types
Cava provides the following primitive types:
| Type | Size |
|---|---|
boolean | 1 byte |
byte | 1 byte |
char | 2 bytes |
short | 2 bytes |
int | 4 bytes |
long | 8 bytes |
float | 4 bytes |
double | 8 bytes |
The char type uses a fixed-length 2-byte encoding similar to Java's UTF-16 format — not the variable-length variant.
Type conversion
Automatic conversion
The compiler automatically converts a source type to a destination type when both of the following conditions are true:
The destination type's value range encompasses the source type's range (applies primarily to integer types).
No precision is lost in the conversion (applies primarily to floating-point types).
Examples of safe, automatically converted assignments:
float a = 3.14f;
double b = a; // float → double: safe, no precision loss
short c = -1;
int d = c; // short → int: safe, range is widerConversion rules follow the same conventions as Java and C++.
Explicit type casting
When the destination type has a smaller range or lower precision than the source type, the compiler returns an error for an implicit assignment:
double a = ...;
float b = a; // error, risk of losing precisionTo perform the conversion intentionally, use an explicit cast. Note that the fractional part is discarded (truncation):
float a = 3.14f;
int b = (int)a; // The fractional part is discarded. Only the integer part is kept, which truncates the value.Custom data types
Cava lets you define classes and use objects. For details, see Cava classes and objects.