Literal (computer programming)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 4 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computer science, a literal is a textual representation (notation) of a value as it is written in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for Booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects. An anonymous function is a literal for the function type. In contrast to literals, variables or constants are symbols that can take on one of a class of fixed values, the constant being constrained not to change. Literals are often used to initialize variables; for example, in the following, 1 is an integer literal and the three letter string in "cat" is a string literal: int a = 1; string s = "cat"; In lexical analysis, literals of a given type are generally a token type, with a grammar rule, like "a string of digits" for an integer literal. Some literals are specific keywords, like true for the Boolean literal "true". In some object-oriented languages (like ECMAScript), objects can also be represented by literals. Methods of this object can be specified in the object literal using function literals. The brace notation below, which is also used for array literals, is typical for object literals: {"cat", "dog"} {name: "cat", length: 57}

In computer science, a literal is a textual representation (notation) of a value as it is written in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for Booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects. An anonymous function is a literal for the function type. In contrast to literals, variables or constants are symbols that can take on one of a class of fixed values, the constant being constrained not to change. Literals are often used to initialize variables; for example, in the following, 1 is an integer literal and the three letter string in "cat" is a string literal: int a = 1; string s = "cat"; In lexical analysis, literals of a given type are generally a token type, with a grammar rule, like "a string of digits" for an integer literal. Some literals are specific keywords, like true for the Boolean literal "true". In some object-oriented languages (like ECMAScript), objects can also be represented by literals. Methods of this object can be specified in the object literal using function literals. The brace notation below, which is also used for array literals, is typical for object literals: {"cat", "dog"} {name: "cat", length: 57}

Literals of objects

In ECMAScript (as well as its implementations JavaScript or ActionScript), an object with methods can be written using the object literal like this: var newobj = { var1: true, var2: "very interesting", method1: function () { alert(this.var1) }, method2: function () { alert(this.var2) } }; newobj.method1(); newobj.method2(); These object literals are similar to anonymous classes in other languages like Java. The JSON data interchange format is based on a subset of the JavaScript object literal syntax, with some additional restrictions (among them requiring all keys to be quoted, and disallowing functions and everything else except data literals). Because of this, almost every valid JSON document (except for some subtleties with escaping) is also valid JavaScript code, a fact exploited in the JSONP technique.

See also

Character literal Function literal Here document – a file literal or stream literal Hexadecimal floating-point literal Integer literal String literal

Text derived from https://en.wikipedia.org/w/index.php?oldid=1378460643 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001378460643 · captured 1791124717.0 · version history

Cite this page as: encyclopegia.org, “Literal (computer programming)”, snapshot enwiki-001378460643. https://encyclopegia.org/a/literal-computer-programming

Sources cited in this revision

Extracted from the cited revision's reference blocks; absent citations are not invented.