Literals

As opposed to an identifier, which represents a pointer to a value, a Literal is an actual value specified within the code, such as the number 50 or the text string “Hello World!”.

Numeric values, including those containing decimals, can be written as they are, but string values must always be written between double quotation marks. The following shows some examples of this:

NumberValue = 42;

DecimalValue = 33.3;

StringValue = "This is a string";

In addition,True, False and Null are special literals reserved for evaluating the state of an expression, and will be discussed in more detail in later lessons.

The most common use for a literal is to assign an initial value to a variable or constant, but they can also be used in simple calculations as values that are relevant only in the context they appear.

For example, if you want an expression to halve a supplied value, it should look something like this:

MyValue / 2

There is no point using a variable in place of the literal 2 with an expression this simple, since halving a value will always require it to be divided by 2. Similarly, it is needlessly complicated to create a constant for this purpose.

 

<< Previous: Operator Precedence | Next: Lesson Summary >>