Literals

In Java, literals are used to represent constant values ​​in a convenient form. For example, the number 100 is a literal. Literals are often called constants. As a rule, the structure of literals and their use are intuitive. They have already met in the previously reviewed examples of programs, and now it is time to give them a formal definition. Java provides literals for all simple types. The way the literal is represented depends on the data type. As explained earlier, constants corresponding to characters are enclosed in single quotes. For example, both 'a' and '%' are character constants. Integer constants are written as numbers without a fractional part. For example, integer constants are 10 and -100. When forming a constant with a floating point, it is necessary to specify the decimal point, after which the fractional part follows. For example, 11. 123 is a floating point constant. Java also supports the so-called exponential format for floating-point numbers. By default, integer literals are of type int. If you want to define a literal of type long, after the number you should specify the letter l or L. For example, 12 is a constant of type int, and 121 is a constant of type long. By default, floating-point literals are of the douXe type. And in order to specify a float literal, you must specify the letter f or F after the number. For example, the literal 10 .19F refers to the float type. Although default integer literals are created as values ​​of type int, they can be assigned to variables of type char, byte, short and long. The assigned value is cast to the target type. A long variable can also be assigned any value represented by an integer literal.

Questions:
Are literals variables?
Why are literals often called constants? If the constant is a fixed value that should not change.

  • And the literal does not change - when assigning a=3 value of the receiver variable changes - MBo
  • Give an example, how do you change the literal? - Grundy
  • "indicate the letter 1" - it seems to me, or is there really a unit instead of a lowercase letter L ? - Regent
  • @Regent, it doesn't seem :) - Grundy
  • Corrected, just there in the book it is like L (l). - Petrovchenko Ivan

2 answers 2

From Wikipedia:

Literals are constants that are included directly in the text of the program.

I.e

 int someVariable = 1; ^ ^ переменная целочисленный литерал String anotherVariable = "Привет!"; ^ ^ переменная строковый литерал 

You can assign any of these variables a different value, but you cannot change the literal itself, which was originally assigned to it.

However, this is not always and not quite true. For example, you can change values ​​in an array created using a literal

 int[] someArray = { 1, 2, 3 }; ^ ^ массив литерал массива, состоящий из целочисленных литералов someArray[1] = 42; 

    The question is straightforward philosophical.

    A literal is primarily a syntactic construct. Those. The literal is an element of the TEXT program.

    Literal is not a variable value. Literal can be used to set the value of a variable.

    It’s hard to say why, but nobody says "assign a literal to a variable" or "pass a literal to a function". Instead, they say "assign a constant to a variable", "pass a constant to a function." Although from the point of view of the text of the program the first option is more accurate. From here and the expression "literals are often called constants."