TExcelTAD
Home › Julia › Julia Variables

Julia Variables

A variable is a name bound to a value. You create one with =; there is no keyword like let or var, and you never declare a type unless you want to.

julia · try it yourself
city = "Lisbon"
population = 545_000          # underscores make big numbers readable
area_km2 = 100.05
is_capital = true

println(city, " has ", population, " people")
println(typeof(city), " ", typeof(population), " ", typeof(area_km2), " ", typeof(is_capital))
Lisbon has 545000 people
String Int64 Float64 Bool

Variables are names, not boxes

Assignment binds a name to a value. A variable can later be rebound to a value of a completely different type:

julia · try it yourself
x = 42
println(x, " :: ", typeof(x))
x = "forty-two"
println(x, " :: ", typeof(x))
x = [4, 2]
println(x, " :: ", typeof(x))
42 :: Int64
forty-two :: String
[4, 2] :: Vector{Int64}

Because names only refer to values, two names can refer to the same mutable object. Changing the object through one name is visible through the other:

julia · try it yourself
a = [1, 2, 3]
b = a            # b and a are the same array
push!(b, 4)
println(a)
println(a === b)

c = copy(a)      # an independent copy
push!(c, 5)
println(a)
println(c)
[1, 2, 3, 4]
true
[1, 2, 3, 4]
[1, 2, 3, 4, 5]

Naming rules

  • Names start with a letter (including most Unicode letters) or an underscore, followed by letters, digits, _ or !.
  • Names are case-sensitive.
  • Keywords such as if, end, function, for, struct cannot be used as names.
Style Used for Example
lowercase / snake_case variables, functions max_speed, total
CamelCase types and modules Point, LinearAlgebra
UPPER_CASE constants (common convention) MAX_RETRIES
ends in ! functions that mutate an argument push!, sort!

Unicode names

Julia lets you use Greek letters, subscripts and other symbols. In the REPL or VS Code type a LaTeX-like name such as \alpha and press Tab.

julia · try it yourself
α = 0.5
θ₀ = 30
σ² = 4.0
println(α * θ₀)
println(sqrt(σ²))
println(float(π))
15.0
2.0
3.141592653589793

Multiple assignment and swapping

Several variables can be assigned at once from a tuple; this also gives an elegant swap:

julia · try it yourself
width, height = 1920, 1080
println(width, "x", height)

width, height = height, width
println(width, "x", height)

i = j = 0     # chained assignment
i += 5        # updating operators: +=, -=, *=, /=, ^= ...
println(i, " ", j)
1920x1080
1080x1920
5 0

Constants

const declares a global whose value should never be rebound. Besides documenting intent, it helps performance, because the compiler knows the type of a constant global.

julia
const GRAVITY = 9.81
GRAVITY = 10      # error (Julia 1.11): invalid redefinition of constant

Tip: Untyped, non-constant global variables are slow in Julia because their type can change at any time. Put work inside functions or use const. The Performance Tips lesson explains why.

Test yourself

After a = [1, 2]; b = a; push!(b, 3), what is a?

  1. [1, 2]
  2. [1, 2, 3]
  3. [3]
  4. An error

What is the type of x after x = 7?

  1. Int32
  2. Int64
  3. Number
  4. Integer

Which statement swaps the values of p and q?

  1. swap(p, q)
  2. p = q; q = p
  3. p, q = q, p
  4. p <=> q