Data Types
JARU is a dynamically typed language, meaning the data types (values) of variables and objects are not explicitly specified at compile time, but are determined at runtime. This means that the same variable can contain different data types throughout the program, without having to previously specify the data type it will store.
In JARU all values are first-class, meaning all of them can be assigned to variables, passed as arguments to functions, or returned by them. Values can not only be used to represent data, but can also be manipulated and used to build new values or complex programs.
| Type | Description | Example |
|---|---|---|
| bool | Data type for storing boolean values (true or false). | true, false |
| integer | Stores integer numbers. | 123 |
| float | Stores decimal numbers. | 0.456 |
| string | Data type for storing text strings. Strings can be enclosed in single or double quotes. | 'Goku', "Krilin" |
| list | Data type for storing an ordered collection of elements. | [1, 2, 3, 4] |
| map | Data type for storing a collection of key-value pairs. | {"key1": "value1", "key2": "value2"} |
| array | Data type for storing an ordered collection of elements with a numeric index. | {1, 2, 3, 4} |
Bool
In JARU, the "boolean" data type is used to represent logical values, i.e., true or false. A variable declared as "boolean" can store only two possible values: true or false.
To declare a variable as boolean type, use the keyword var followed by the variable name and the value true or false.
var isAdult = true
var hasPermission = false
In JARU, boolean variables are used in comparison and logical operations, such as in if-else, while, for control structures, etc.
var age = 18
if (age >= 18) then
print("Is an adult")
else
print("Is a minor")
end
In the example above, the variable "age" is compared with the value 18. If it's greater than or equal to 18, "Is an adult" is printed, otherwise "Is a minor" is printed.
Integer
The integer data type is used to store whole numeric values without decimals. The bit size used to represent integers in JARU is 32 bits, allowing storage of integer values in the range of -2,147,483,648 to 2,147,483,647. The integer data type is used to store whole numeric values without decimals and can be used in mathematical calculations, variable assignments, value comparisons, and decision-making through control structures.
var x = 10
In this case, the type of variable "x" is automatically determined as an integer, since it was assigned a whole numeric value.
Float
In JARU, the float data type is used to store double-precision floating-point numeric values, with a size of 64 bits in computer memory. The float data type in JARU has a size of 64 bits in computer memory and uses 52 bits to store the mantissa (fractional part) of the number and 11 bits to store the exponent. This allows representing numbers with a precision of up to 15 significant digits. Float data can be used in mathematical calculations, variable assignments, value comparisons, and decision-making through control structures.
var x = 3.14
In this case, the type of variable "x" is automatically determined as a float, since it was assigned a decimal numeric value.
String
The string data type is used to represent character strings or text. A variable declared as string can store any sequence of characters, such as words, phrases, or paragraphs.
To declare a variable as string type, use the keyword var followed by the variable name and the character string value in double quotes or single quotes.
var name = "John"
var phrase = 'Hello world!'
var paragraph = "This is an example paragraph with multiple lines of text."
In JARU, character strings are objects, meaning they have methods and properties that can be used to work with them. For example, you can use the size() method to determine the length of a character string or the find() method to search for a substring within the string.
// Get the length of a character string
var name = "John"
println(name.size()) // Prints 4
// Search for a substring within a string
var phrase = "Hello world!"
println(phrase.find("world")) // Prints 6
List
In JARU, the list data type is used to store a collection of values of the same type or different types.
To declare a list, use the keyword var followed by the variable name and the values you want to store between square brackets [ ].
var names = ["John", "Peter", "Michael"]
var ages = [25, 30, 35]
var mixed = ["John", 25, true]
In the first example, a list of names is created, in the second a list of ages, and in the third example a mixed list.
Lists in JARU are mutable objects, meaning they can be modified after being created. You can access list elements using the element's index within square brackets [ ].
print(names[0]) // prints "John"
names[1] = "Andrew" // changes the value at position 1 to "Andrew"
In the example above, the first element of the names list is accessed and the value at position 1 is changed to Andrew.
In JARU, lists have several methods available to perform common operations on them that we'll see later. An example of a list method is .append(value) which adds an element to the end of the list.
var names = ["John", "Peter"]
names.append("Michael") // the names list now contains ["John", "Peter", "Michael"]
Map
In JARU, maps are a data type that allows storing key-value pairs. They can be used to store information like a dictionary or an object in other programming languages.
To create a map, you can use the keyword map followed by a list of key-value pairs inside braces {}.
var ages = {
"John": 25,
"Peter": 30,
"Michael": 35
}
You can also create empty maps and add key-value pairs later:
var ages = {}
ages["John"] = 25
ages["Peter"] = 30
ages["Michael"] = 35
To access a value in a map, you can use bracket notation [] with the corresponding key:
var johnsAge = ages["John"] // johnsAge contains 25
Some common methods for working with maps include:
.size(): Returns the number of key-value pairs in the map..keys(): Returns a list with all the keys in the map..values(): Returns a list with all the values in the map..has(key): Returns true if the map has a key-value pair with the specified key, otherwise returns false..delete(key): Removes the key-value pair with the specified key from the map.
Array
Arrays are a data type that allows storing an ordered sequence of elements. They can be used to store a list of numbers, text strings, objects, etc. In JARU, arrays are fixed-size; once an array is created with a specific size, it cannot be expanded or reduced.
To create an array, put a list of elements separated by commas inside braces {}.
var numbers = {1, 2, 3, 4, 5}
var names = {"John", "Peter", "Michael"}
You can also create empty arrays and assign values later:
var names[10]
names[8] = "This is a demo"
To access an element in an array, you can use bracket notation [] with the corresponding index (starting at 0 for the first element):
var numbers = {1, 2, 3, 4, 5}
var secondNumber = numbers[1] // secondNumber contains 2
Some common methods for working with arrays include:
.size(): Returns the number of elements in the array..numdim(): Returns the number of dimensions of the array..dim(): Returns a list with the dimensions of the array..fill(value): Initializes all array elements with the given value.
Multidimensional Arrays
In JARU, multidimensional arrays can be declared using the syntax var arrayName[10][20]. This creates an array with 10 elements, where each element is in turn an array with 20 elements. To access a specific element of the multidimensional array, use nested bracket notation, for example arrayName[i][j], where i is the index of the element at the first level of the array and j is the index of the element at the second level of the array.
var array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
// Access a specific element in the matrix
println(array[1][1]) // Output: 5
// Iterate through matrix elements
for (var i = 0; i < 3; i++)
for (var j = 0; j < 3; j++)
println(array[i][j])
end
end
Examples of array declarations with different numbers of dimensions:
// One-dimensional array
var array1D = {1, 2, 3, 4, 5}
// Two-dimensional array
var array2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
// Three-dimensional array
var array3D = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}}