Documentation Comments
Use this form to comment on this topic. You can also provide any general observations about the Online Documentation, or request that additional information be added in a future release.
RealityV15.1Online Documentation (MoTW) Revision 7
Variable Typing and Casting (dbo_variable_typing_casting.htm)
All non object variables passed to the language server as method parameters or field assignments are passed as strings. However Java requires all variables to have a specific type. If there is no explicit type cast in the DataBasic code then an attempt will be made to auto type it. In general auto typing works well and there is little need for type casting.
Part of the auto type process involves the string being processed to see if it looks like a number.
If the string consists entirely of digits (0-9), it is considered likely to be an integer.
If the string consists of digits and a single decimal point it is considered likely to be a float.
String numbers can use the full Java floating point syntax - for example, 1.23e5f. However such numbers cannot be used in DataBasic arithmetic expressions.
Auto typing to Boolean will only occur if the string contains the lower case values "true" or "false", as these are the Java representations of boolean.
Generic DataBasic expressions can be cast to bool where an empty string or numeric zero will be considered "false", anything else will be considered "true".
Methods that return a boolean result will return numeric 0 for false, 1 for true, to enable the result to be used directly in a DataBasic expression. If the result is to be passed to another method as a parameter it will require a (bool) cast.
An explicit cast can be used to force a string to be converted to a specific type.
(type) expression
expression | Any valid DataBasic expression. | ||||||||||||||||||
type = | [ [ [int|INT] [8|16|32|64] | [char|CHAR] ] | [bool|BOOL] | [float|FLOAT] | [double|DOUBLE] | [unicode|UNICODE] | [string|STRING] | [object|OBJECT] ] Where:
|
(int8) -128
Largest negative eight bit integer.
(int16) 32767
Largest positive sixteen bit integer.
(int32) -2147483648
Largest negative thirty-two bit integer.
(int64) 9223372036854775807
Largest positive sixty-four bit integer.
(char) "z"
Force a string to be a character.
(bool) 0
Force a numeric zero to be Boolean false.
(bool) "my string"
Force a non empty string to be Boolean true.
(float) "123"
Force an integer to be a float.
(string) "123"
Force a number to be a string.
(object) 0
A null object.
MyStr = %New(String, "Hello world")
IF MyStr->endsWith("ld") THEN CRT "True" ELSE CRT "False"
Uses the Boolean type returned by the endsWith method directly in a DataBasic expression and prints True.