What is a literal?

1

In this link of the MSDN site says that a literal is:

  

A literal is a value that is expressed as itself instead of as a variable value or the result of an expression, such as the number 3 or the "Hello" string.

I still do not understand what a literal is. In the same link you have an example:

Option Strict On

Public Class Sample
    Public Const MyByte As Byte = 2
End Class

Now my doubts are:

  • What is a literal?
  • For what purpose does it exist?
  • Where has a literal in the example above?
asked by anonymous 22.02.2016 / 01:12

2 answers

2

A literal is a value that is literally literally in the middle of your code.

In your example, 2 is a literal.

Public Const MyByte As Byte = 2

In the example below (in C #), my text is a literal

var algumaString = "meu texto"; 

Literals are not variables, nor constants

You can set a literal value for a variable

var texto = "meu texto";

for a constant

const string Nome = "João";

or even show a literal in a MessageBox

MessageBox.Show("Meu literal");
    
22.02.2016 / 01:20
4

In the above example the literal is 2.

The term literal is not something of .Net or even computing, it is a basic mathematical term.

Literal can be said, roughly speaking, since it is difficult to understand, such as a fixed value .

It exists to determine values. The most common is that these values are numbers. Many of these numbers may have a suffix indicated by their type, or they may have prefixes indicating whether the notation is different from the decimal (hexadecimal, binary, and octal are common). Another literal as well is what represents a text, a string , or even just a character. true and false are usually boolean type literals.

Some languages have other literals. Some even allow you to create a literal, albeit rare.

The literal is always constant, but a constant does not have to be represented by a literal . Many people use the term improperly for the literal. Although the literal is constant, it can be assigned to variables, the literal will never change, but the value of the variable may change.

    
22.02.2016 / 01:25