Data Types
Primitive Data Types
Java contains eight primitive data types, listed in the table below.
Integer between -231 and 231-1
Integer between -263 and 263-1
Other data types such as strings are classes, which are also referred to as composite data types.
In methods, you can use any5 of the primitive or composite data types available in Java and the Java libraries. Many of the Application Builder built-in methods make use of primitive or composite data types. For example, the timeStamp() method provides a long integer as its output.
Assignments and Literals
A few examples of using literals in assignments are:
int i=5; // initialize i and assign the value 5
double d=5.0; // initialize d and assign the value 5.0
boolean b=true; // initialize b and assign the value true
 
The constants 5, 5.0, and true are literals. Java distinguishes between the literals 5 and 5.0, where 5 is an integer and 5.0 is a double (or float).
Unary and Binary Operators in Methods (Java Syntax)
You can perform calculations and operations using primitive data types just like with many other programming languages. The table below describes some of the most common unary and binary operators used in Java code.
Type Conversions and Type Casting
When programming in Java, conversion between data types is automatic in many cases. For example, the following lines convert from an integer to a double:
int i; // initialize i
double d; //initialize d
i=41;
d=i; // the integer i is assigned to the double d and d is 41.0
However, the opposite will not work automatically (you will get a compilation error). Instead you can use explicit type casting as follows:
int i; // initialize i
double d; //initialize d
d=41.0;
i=(int) d; // the double d is assigned to the integer i and i is 41
You can convert between integers and doubles within arithmetic statements in various ways, however you will need to keep track of when the automatic type conversions are made. For example:
int i; // initialize i
double d; //initialize d
i=41;
d=14/i; // d is 0
In the last line, 14 is seen as an integer literal and the automatic conversion to a double is happening after the integer division 14/41, which results in 0.
Compare with:
int i; // initialize i
double d; //initialize d
i=41;
d=14.0/i; // d is 0.3414...
In the last line, 14.0 is seen as a double literal and the automatic conversion to a double is happening before the division and is equivalent to 14.0/41.0.
You can take charge over the type conversions with explicit casting by using the syntax (int) or (double):
int i; // initialize i
double d,e; //initialize d and e
i=41;
d=((int) 14.0)/i; // d is 0
e=14/((double) i); // e is 0.3414...
Strings and Java Objects
The String data type is a Java object. This is an example of how to declare a string variable:
String a="string A";
When declaring a string variable, the first letter of the data type is capitalized. This is a convention for composite data types (or object-oriented classes).
After you have declared a string variable, a number of methods are automatically made available that can operate on the string in various ways. Two such methods are concat and equals as described below, but there are many more methods available in the String class. See the online Java documentation for more information.
Concatenating Strings
To concatenate strings, you can use the method concat as follows:
String a = "string A";
String b = " and string B";
a.concat(b);
The resulting string a is "string A and string B". From an object-oriented perspective, the variable a is an instance of an object of the class String. The method concat is defined in the String class and available using the a.concat() syntax.
Alternatively, you can use the + operator as follows:
a = a + b;
which is equivalent to:
a = "string A" + " and string B";
and equivalent to:
a = "string A" + " " + "and string B";
where the middle string is a string with a single whitespace character.
Comparing Strings
Comparing string values in Java is done with the equals method and not with the == operator. This is due to the fact that the == operator compares whether the strings are the same when viewed as class objects and does not consider their values. The code below demonstrates string comparisons:
boolean streq = false;
String a = "string A";
String b = "string B";
streq = a.equals(b);
// In this case streq == false
 
streq = (a == b);
// In this case streq == false
 
b = "string A";
streq = a.equals(b);
// In this case streq == true
Special Characters
If you would like to store, for example, a double quotation mark or a new line character in a string you need to use special character syntax preceded by a backslash (\). The table below summarizes some of the most important special characters.
Note that in Windows the new line character is the composite \r\n whereas in Linux and macOS \n is used.
The example below shows how to create a string in Windows that you later on intend to write to file and that consists of several lines.
String contents = "# Created by me\r\n"
  +"# Version 1.0 of this file format \r\n"
  +"# Body follows\r\n"
  +"0 1 \r\n"
  +"2 3\r\n"
  +"4 5\r\n";
The string is here broken up into several lines in the code for readability. However, the above is equivalent to the following:
String contents = "# Created by me\r\n# Version 1.0 of this file format \r\n# Body follows\r\n0 1 \r\n2 3\r\n4 5\r\n";
which is clearly less readable.
Arrays
In the application tree, the Declarations node directly supports 1D and 2D arrays of type string (String), integer (int), Boolean (boolean), or double (double). A 1D array may be referred to as a vector and a 2D array referred to as a matrix, provided that the array is rectangular. A nonrectangular array is called jagged or ragged. In methods, you can define higher-dimensional arrays as well as arrays of data types other than string, integer, Boolean, or double.
1D Arrays
If you choose not to use the Declarations node to declare an array, then you can use the following syntax in a method:
double dv[] = new double[12];
This declares a double array of length 12.
The previous line is equivalent to the following two lines:
double dv[];
dv = new double[12];
When a double vector has been declared in this way, the value of each element in the array will be zero.
To access elements in an array you use the following syntax:
double e;
e = dv[3]; // e is 0.0
Arrays are indexed starting from 0. This means that dv[0] is the first element of the array in the examples above, and dv[11] is the last element.
You can simultaneously declare and initialize the values of an array by using curly braces:
double dv[] = {4.1, 3.2, 2.93, 1.3, 1.52};
In a similar way you can create an array of strings as follows:
String sv[] = {"Alice", "Bob", "Charles", "David", "Emma"};
2D Arrays
2D rectangular arrays can be declared as follows:
double dm[][] = new double[2][3];
This corresponds to a matrix of doubles with 2 rows and 3 columns. The row index comes first.
You can simultaneously declare and initialize a 2D array as follows:
double dm[][] = {{1.32, 2.11, 3.43},{4.14, 5.16, 6.12}};
where the value of, for example, dm[1][0] is 4.14. This array is a matrix since it is rectangular (it has same number of columns for each row). You can declare a ragged array as follows:
double dm[][] = {{1.32, 2.11}, {4.14, 5.16, 6.12, 3.43}};
where the value of, for example, dm[1][3] is 3.43.
Copying Arrays
For copying arrays, the following code:
for (int i1 = 0; i1 <= 11; i1++) {
  for (int i2 = 0; i2 <= 2; i2++) {
    input_array[i1][i2] = init_input_array[i1][i2];
  }
}
is not equivalent to the line:
input_array = init_input_array;
since the last line will only copy by reference.
Instead, you can use the copy method as follows:
input_table = copy(init_input_table);
which allocates a new array and then copies the values.