Julia Intro
Julia is a high-level, dynamic programming language designed for numerical and scientific computing. It reads like Python or MATLAB, yet code compiled by Julia's just-in-time (JIT) compiler often runs as fast as C. Julia was first released publicly in 2012 by Jeff Bezanson, Stefan Karpinski, Viral B. Shah and Alan Edelman, and reached version 1.0 in 2018. This course uses Julia 1.11.
The creators summed up their goal as solving the "two-language problem": scientists used to prototype in a slow, friendly language and then rewrite the hot parts in C or Fortran. Julia aims to be both the friendly language and the fast one.
println("Hello from Julia!")
println("2 + 3 = ", 2 + 3)
println("Running Julia ", VERSION.major, ".", VERSION.minor)Hello from Julia!
2 + 3 = 5
Running Julia 1.11
What makes Julia different?
| Feature | What it means for you |
|---|---|
| JIT compilation | Each function is compiled to native machine code the first time it is called with a given set of argument types |
| Dynamic typing | You don't have to write types, but you can annotate them when it helps |
| Multiple dispatch | A function can have many methods; Julia picks one based on the types of all arguments |
| Built-in numerics | Arbitrary-precision integers, rationals, complex numbers, matrices and linear algebra out of the box |
| Unicode-friendly | Variables like α, σ² or θ are valid names, and maths looks like maths |
| 1-based indexing | The first element of an array is a[1], as in Fortran, MATLAB and R |
Here is a small taste: a function with no type annotations works on integers, floats and fractions, because Julia compiles a specialised version for each argument type.
square(x) = x * x
println(square(7))
println(square(1.5))
println(square(2 // 3))
println(square(1 + 2im))49
2.25
4//9
-3 + 4im
Where is Julia used?
- Scientific computing: climate models, differential equations, physics simulations.
- Data science and machine learning: packages such as DataFrames.jl, Flux.jl and MLJ.jl.
- Optimisation and operations research: the JuMP modelling language.
- High-performance computing: Julia runs on supercomputers and GPUs.
- Finance and pharma: risk models and pharmacometrics.
Julia in this course
Each lesson explains a topic and lets you run real Julia code. Press ▶ Run on any example: the code is sent to the public Compiler Explorer service, which runs Julia 1.11 and returns the output. Edit the code and run it again to experiment.
The playgrounds have the standard library available — Statistics, LinearAlgebra, Printf, Random and Dates — but not third-party packages like DataFrames or Plots. When a lesson shows package code it is marked as not runnable.
using Statistics
temperatures = [18.5, 21.0, 19.5, 23.0]
println("Mean temperature: ", mean(temperatures))
println("Warmest day: ", maximum(temperatures))Mean temperature: 20.5
Warmest day: 23.0
Note: The first run of an example may take several seconds, because Julia compiles code before running it. That compile time is the price for Julia's fast execution, and you'll learn how to work with it in the Performance Tips lesson.
Test yourself
What does Julia's JIT compiler do?
- Interprets each line one at a time
- Compiles functions to native machine code when they are first called with given argument types
- Converts Julia code into Python
- Compiles the whole program ahead of time into an .exe
What is the index of the first element of a Julia array?
- 0
- 1
- -1
- It depends on the array
Which feature lets Julia choose a method based on the types of all arguments?
- Inheritance
- Operator overloading
- Multiple dispatch
- Duck typing