Learn/Julia/2. Values and variables
Lesson 2 of 8
Sign in

Julia, lesson 2

Values and variables

A variable is a name bound to a value. You create one with =:

Assigning again binds the name to a new value. There is no keyword to declare a variable; the first assignment creates it.

Basic types

Every value in Julia has a type, and typeof tells you which:

ExampletypeofWhat it is
42Int64a whole number (64-bit on normal computers)
4.2Float64

2 exercises to try what you just read.

Exercise 1

Recipe card

Press Ctrl Enter or Check to test your code.

Exercise 2

Type detective

Press Ctrl Enter or Check to test your code.

a number with a decimal point
"tea"Stringtext
't'Chara single character, in single quotes
true, falseBoolyes/no values

Julia treats 42 and 42.0 as different types. Dividing with / always produces a Float64, so 10 / 2 is 5.0, not 5. You will see why that matters when printing results.

Integers have a fixed size. Going past the largest Int64 (about 9.2 quintillion) silently wraps around to a negative number instead of raising an error. That is rare in everyday programs, but worth knowing; big(10)^30 gives you an arbitrarily large integer when you need one.

String interpolation

Instead of passing many arguments to println, you can put values straight into a string with $. Use $name for a variable and $(expression) for anything more complex:

Interpolation is the most common way to build output in Julia, and the code reads like the result.

Naming

Names are case-sensitive and may contain letters, digits, underscores and even Unicode letters such as δ. They cannot start with a digit. The Julia style is lowercase names, with an underscore only when it helps readability: price, total_cost. Type names use UpperCamelCase, like Float64.

Constants

Mark a global value that should never change with const:

Trying to assign a new value to VAT_RATE later is an error, and const also helps Julia generate faster code, because it knows the value's type cannot change.

Converting

string(x) turns any value into text. To go the other way, from text to a number, you use parse, which arrives in the next lesson with input. To change between number types, call the type: Float64(3) is 3.0, and Int(4.0) is 4 (but Int(4.5) is an error, because the value would change; use round(Int, 4.6) for that). round(x, digits=1) keeps a decimal number but rounds it to one decimal place, which tidies results like 0.30000000000000004.

Further reading

  • Julia manual: Variables
  • Julia manual: Integers and floating-point numbers
  • Julia manual: Strings, interpolation
  • Julia manual: Scope and constants

The starter defines some variables for a pancake recipe. Create a variable total_flour holding flour_per_person * people, then print exactly these lines using string interpolation:

Milk is 0.3 litres per person; compute it with $(...) inside the string, rounded with round(..., digits=1).

Print the type of each of these five values, one per line, using typeof: 42, 4.2, "42", '4' and true. Expected output:

city = "Lisbon"
temperature = 21.5
println(city, " is ", temperature, " degrees")
item = "Notebook"
price = 2.5
qty = 4
println("$qty x $item = $(price * qty)")   # 4 x Notebook = 10.0
const VAT_RATE = 0.2
Pancakes for 4 people
Flour: 480 g
Milk: 1.2 litres
Int64
Float64
String
Char
Bool