liveBook · Manning

Literal

As explained in Julia as a Second Language

A number literal is a source-code notation that directly represents a numeric value with digits rather than a variable name.

A number literal writes a numeric value directly in source code with digits instead of referring to a named variable. Julia accepts decimal, binary, and hexadecimal forms, while decimal notation is usually the practical default. section 2.7

Literal spelling also helps determine the value’s type. Ordinary integers default to Int64, decimal floating-point values to Float64, and suffixes such as f0 select Float32. Other types can be produced with conversions such as Int8(42) or Float16(3.5). section 2.4 section 2.3

Common literal forms include:

  • 42 is an Int64 integer literal, 0x42 is an unsigned hexadecimal literal, 0.42 is Float64, 0.42f0 is Float32, and 3//4 is Rational{Int64}. section 11.4.4
  • Use an explicit conversion when a desired type lacks a convenient literal form, such as Float16(3.5) or Int8(42). section 2.4 section 11.4.4
  • Write 34.23f0 rather than 34.23f: Julia interprets a trailing f without 0 as multiplication by a variable named f. section 2.4

Inspecting literal types

julia> typeof(42) Int64 julia> typeof(0.42) Float64 julia> typeof(0.42f0) Float32 julia> typeof(3//4) Rational{Int64} section 11.4.4

Julia can extend literal syntax with string macros: big"42" creates a BigInt during parsing, and a custom int8"42" form can create an Int8 value. This reaches parse-time behavior without developing the broader macro system. section 11.4.4

How Julia as a Second Language covers it

How deep it goes
Defines numeric notation, demonstrates inferred types and suffixes, and briefly extends literals through macros without teaching macro internals.
Examples
Uses decimal, hexadecimal, rational, Float32, Float16, BigInt, and custom Int8 literal examples inspected in the REPL.
What it assumes
Assumes basic Julia expressions and variables while establishing numeric types, formats, and direct source notation from the beginning.
What you can do after
Apply it to write numeric values, inspect their types, choose conversions, and avoid confusing floating-point suffixes with multiplication.
How it connects
Connects literals with numeric types, type conversion, floating-point precision, hexadecimal notation, BigInt, and macros.

Also explained in

Compare how 6 books explain literal →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.