What is a literal?

Asked

Viewed 2,117 times

1

In that link from the MSDN website says that a literal is:

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

Even so I did not understand what a literal is. In the same link has 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 it exists?
  • Where there is a literal in the example above?

2 answers

4

In the above example the literal is 2.

The literal term is not something from . NET or even computation, it is a basic mathematical term.

Literal can be said, roughly speaking, since it is difficult to understand, 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 decimal (hexadecimal, binary, and octal are common). Another very common literal is what represents a text, a string or even just a character. true and false are usually Boolean literals.

Some languages have other literals. Some even allow creating a literal, although rare because it is confusing.

The literal is always constant, but a constant need not be represented by a literal. Many people use the term constant inappropriately 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 can change.

Literal is exactly as it is placed there, it will not be manipulated, it does not fit interpretation, it is not something representative, it is that and only.

  • I: "In short, it’s the same thing as a constant?" mustache says: "The literal is always constant", jbueno says: "No. Constants have literal values defined and cannot have value changed during execution."

  • 1

    It is written in the answer that.

  • I edited the comment.

  • 1

    A literal is constant. What I meant is that you do not declare a constant because of a literal value.

3


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

In his 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 neither variable nor constant

You can set a literal value for a variable

var texto = "meu texto";

to a constant

const string Nome = "João";

or even show a literal in a Messagebox

MessageBox.Show("Meu literal");
  • In short, it’s the same thing as a constant?

  • No. Constants have defined literal values and may not have changed value during execution. You can set a literal to a variable and change the values later.

  • So are literals variable? Literal is the value I give to the member, and then during execution the literal becomes a variable?

  • 1

    Almost that. You set a literal value for a variable.

  • I edited the examples @RZ-8121

Browser other questions tagged

You are not signed in. Login or sign up in order to post.