Difference between revisions of "Literals"
(Created page with "== Description == A literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, fl...") |
|||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{DISPLAYTITLE:Literals}} | ||
== Description == | == Description == | ||
A literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, booleans and characters. | A literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, booleans and characters. | ||
− | Literals are often used to initialize variables, for example, in the following, '1' is an IntegerLiteral which is assigned to a new created variable 'a' of data type [[Integer]]. And the three letter string in "cat" is a StringLiteral, which is assigned to a new created variable 's' of data type [[String]]. | + | Literals are often used to initialize variables, for example, in the following, '1' is an IntegerLiteral which is assigned to a new created variable 'a' of data type [[Core.DataTypes.SimpleDataType.Integer|Integer]]. And the three letter string in "cat" is a StringLiteral, which is assigned to a new created variable 's' of data type [[Core.DataTypes.SimpleDataType.String|String]]. |
<syntaxhighlight lang="java" enclose="div" style="font-size: medium;"> | <syntaxhighlight lang="java" enclose="div" style="font-size: medium;"> | ||
Line 9: | Line 10: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==Types== | |
+ | The following types of Literals exists | ||
+ | |||
+ | * [[Core.DataTypes.SimpleDataType|Simple Data Type]] | ||
+ | * [[Core.DataTypes.ComplexDataType.ContainerDataType|Container Data Type]] | ||
+ | * [[Core.DataTypes.ComplexDataType|Complex Data Type]] | ||
+ | |||
+ | ===Simple Data Type=== | ||
+ | The following table lists all available literals for [[Core.DataTypes.SimpleDataType|Simple Data Type]]s: | ||
+ | {| {{TableHeader}} | ||
+ | |- {{TableHeaderRow}} | ||
+ | | '''Data Type''' || '''Literal''' || '''Sample''' | ||
+ | |- {{TableRow1}} | ||
+ | | '''[[Core.DataTypes.SimpleDataType.Boolean|Boolean]]''' || <tt><nowiki>{True|False}</nowiki></tt> || <tt>Boolean flag = True;</tt> | ||
+ | |- {{TableRow2}} | ||
+ | | '''[[Core.DataTypes.SimpleDataType.Integer|Integer]]''' || Integer number || <tt>Integer n = 1;<br/>m = n - 1234;</tt> | ||
+ | |- {{TableRow1}} | ||
+ | | '''[[Core.DataTypes.SimpleDataType.Float|Float]]''' || Floating-point number || <tt>Float n = 1.2;<br/>m = n + 12.345;</tt> | ||
+ | |- {{TableRow2}} | ||
+ | | '''[[Core.DataTypes.SimpleDataType.String|String]]''' || Quoted sequence of characters || <tt>String str = "This is a text"<br/>str = StringConcatenate(str, " definded as a literal in OTX.");</tt> | ||
+ | |- {{TableRow1}} | ||
+ | | '''[[Core.DataTypes.ComplexDataType.ByteField|ByteField]]''' || Sequence of bytes in hex started with a & || <tt>ByteField bytes1 = &12 34 56 78 90 AB CD EF<br/>ByteField bytes2 = &1234567890ABCDE; // equals &01 23 45 67 89 0A BC DE</tt> | ||
+ | |} | ||
+ | |||
+ | ===Container Data Type=== | ||
+ | The following table lists all available literals for [[Core.DataTypes.ComplexDataType.ContainerDataType|Container Data Type]]s: | ||
+ | {| {{TableHeader}} | ||
+ | |- {{TableHeaderRow}} | ||
+ | | '''Data Type''' || '''Literal''' || '''Sample''' | ||
+ | |- {{TableRow2}} | ||
+ | | '''[[Core.DataTypes.ComplexDataType.ContainerDataType.List|List]]''' || List one or more elements in braces seperated with comma || <tt><nowiki>List<Integer> integerList = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };</nowiki><br/><nowiki>List<List<String>> stringList = { { "Text11", "Text12" }, { "Text21", "Text22", "Text23" }, { "Text31" } };</nowiki></tt> | ||
+ | |- {{TableRow1}} | ||
+ | | '''[[Core.DataTypes.ComplexDataType.ContainerDataType.Map|Map]]''' || List one or more key value pairs in braces seperated with comma; in each pair of key value, the key connects with the value by the equal sign || <tt><nowiki>Map<String, Integer> myMap1 = { "Key1"=1234, "Key2"=567, "Key3"=89 };</nowiki><br/><nowiki>Map<String, List<Integer>> myMap2 = { "Key1"={ 1, 2, 3, 4 }, "Key2"={ 5, 6, 7 }, "Key3"={ 8, 9 } };</nowiki></tt> | ||
+ | |} | ||
+ | |||
+ | ===Complex Data Type=== | ||
+ | Literals for [[Core.DataTypes.ComplexDataType|Complex Data Type]] have the following syntax: | ||
+ | |||
+ | <syntaxhighlight lang="java" enclose="div" style="font-size: medium;"> | ||
+ | @DataTypeName:MemberValue1/MemberValue2/../MemberValueN | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Where '''DataTypeName''' is the name of the related data type and '''MemberValue''' the value of the corresponding value. Sample: | ||
+ | |||
+ | <syntaxhighlight lang="java" enclose="div" style="font-size: medium;"> | ||
+ | // ResultStateLiteral for enumeration value POSITIVE | ||
+ | @ResultState:POSITIVE; | ||
+ | |||
+ | // UserExceptionLiteral to throw an user defined exception | ||
+ | @UserException:"FATAL_ERROR"/"There is a fatal error occured!"; | ||
+ | |||
+ | // QuantityLiteral for a value of 1.2 % | ||
+ | @Quantity:1.2/"%"/1; | ||
+ | </syntaxhighlight> |
Latest revision as of 08:28, 12 July 2017
Description
A literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, booleans and characters.
Literals are often used to initialize variables, for example, in the following, '1' is an IntegerLiteral which is assigned to a new created variable 'a' of data type Integer. And the three letter string in "cat" is a StringLiteral, which is assigned to a new created variable 's' of data type String.
Integer a = 1;
String s = "cat";
Types
The following types of Literals exists
Simple Data Type
The following table lists all available literals for Simple Data Types:
Data Type | Literal | Sample |
Boolean | {True|False} | Boolean flag = True; |
Integer | Integer number | Integer n = 1; m = n - 1234; |
Float | Floating-point number | Float n = 1.2; m = n + 12.345; |
String | Quoted sequence of characters | String str = "This is a text" str = StringConcatenate(str, " definded as a literal in OTX."); |
ByteField | Sequence of bytes in hex started with a & | ByteField bytes1 = &12 34 56 78 90 AB CD EF ByteField bytes2 = &1234567890ABCDE; // equals &01 23 45 67 89 0A BC DE |
Container Data Type
The following table lists all available literals for Container Data Types:
Data Type | Literal | Sample |
List | List one or more elements in braces seperated with comma | List<Integer> integerList = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List<List<String>> stringList = { { "Text11", "Text12" }, { "Text21", "Text22", "Text23" }, { "Text31" } }; |
Map | List one or more key value pairs in braces seperated with comma; in each pair of key value, the key connects with the value by the equal sign | Map<String, Integer> myMap1 = { "Key1"=1234, "Key2"=567, "Key3"=89 }; Map<String, List<Integer>> myMap2 = { "Key1"={ 1, 2, 3, 4 }, "Key2"={ 5, 6, 7 }, "Key3"={ 8, 9 } }; |
Complex Data Type
Literals for Complex Data Type have the following syntax:
@DataTypeName:MemberValue1/MemberValue2/../MemberValueN
Where DataTypeName is the name of the related data type and MemberValue the value of the corresponding value. Sample:
// ResultStateLiteral for enumeration value POSITIVE
@ResultState:POSITIVE;
// UserExceptionLiteral to throw an user defined exception
@UserException:"FATAL_ERROR"/"There is a fatal error occured!";
// QuantityLiteral for a value of 1.2 %
@Quantity:1.2/"%"/1;