Julia Basic Operators
Julia provides a rich set of operators, including arithmetic, comparison, logical, and bitwise operators.
Arithmetic Operators
Basic Arithmetic
julia
a = 10
b = 3
# Addition
println(a + b) # 13
# Subtraction
println(a - b) # 7
# Multiplication
println(a * b) # 30
# Division
println(a / b) # 3.333... (returns float)
# Integer division (floor)
println(a ÷ b) # 3 (same as div(a, b))
# Remainder (modulo)
println(a % b) # 1 (same as rem(a, b))
# Power
println(a ^ 2) # 100
# Negation
println(-a) # -10Division Variants
julia
a = 10
b = 3
# Standard division (always returns float)
println(10 / 3) # 3.333...
# Integer division (floor, ÷ or div)
println(10 ÷ 3) # 3
println(div(10, 3)) # 3
# Remainder
println(10 % 3) # 1
println(rem(10, 3)) # 1
# Modulo (different from rem for negatives)
println(mod(10, 3)) # 1
println(rem(-10, 3)) # -1
println(mod(-10, 3)) # 2
# Get quotient and remainder together
q, r = divrem(10, 3)
println("Quotient: $q, Remainder: $r") # Quotient: 3, Remainder: 1
# Float division rounding
println(fld(10, 3)) # 3 (floor toward -∞)
println(cld(10, 3)) # 4 (ceiling toward +∞)Compound Assignment Operators
julia
x = 10
x += 5 # x = x + 5
println(x) # 15
x -= 3 # x = x - 3
println(x) # 12
x *= 2 # x = x * 2
println(x) # 24
x /= 4 # x = x / 4
println(x) # 6.0
x ^= 2 # x = x ^ 2
println(x) # 36.0
# Integer division assignment
x = 10
x ÷= 3
println(x) # 3
# Remainder assignment
x = 10
x %= 3
println(x) # 1Comparison Operators
Basic Comparison
julia
a = 5
b = 3
# Equal
println(a == b) # false
# Not equal
println(a != b) # true
println(a ≠ b) # true (Unicode)
# Greater than
println(a > b) # true
# Less than
println(a < b) # false
# Greater or equal
println(a >= b) # true
println(a ≥ b) # true (Unicode)
# Less or equal
println(a <= b) # false
println(a ≤ b) # false (Unicode)Chained Comparison
Julia supports mathematical-style chained comparison:
julia
x = 5
# Chained comparison
println(1 < x < 10) # true
println(1 ≤ x ≤ 10) # true
println(1 < 5 < 3) # false
# Equivalent to
println(1 < x && x < 10) # true
# Multiple chained
println(1 < 2 < 3 < 4) # trueApproximate Comparison
julia
# Float comparison (considering precision)
a = 0.1 + 0.2
b = 0.3
println(a == b) # false (float precision issue)
println(isapprox(a, b)) # true
println(a ≈ b) # true (≈ is infix form of isapprox)
# Specify tolerance
println(isapprox(1.0, 1.001, atol=0.01)) # true (absolute tolerance)
println(isapprox(1.0, 1.001, rtol=0.01)) # true (relative tolerance)Object Comparison
julia
# === checks object identity
a = [1, 2, 3]
b = [1, 2, 3]
c = a
println(a == b) # true (values equal)
println(a === b) # false (not same object)
println(a === c) # true (same object)
# For immutable types
x = 5
y = 5
println(x === y) # true (immutable with equal values)Logical Operators
Basic Logical Operations
julia
a = true
b = false
# Logical AND
println(a && b) # false
println(a & b) # false (bitwise, same for booleans)
# Logical OR
println(a || b) # true
println(a | b) # true (bitwise)
# Logical NOT
println(!a) # false
println(!b) # trueShort-Circuit Evaluation
julia
# && short-circuit: if first is false, skip second
x = 5
x > 0 && println("x is positive") # will print
x < 0 && println("x is negative") # won't print
# || short-circuit: if first is true, skip second
x > 0 || println("x is not positive") # won't print
x < 0 || println("x is not negative") # will print
# Common pattern
function validate(x)
x === nothing && return false
x < 0 && return false
return true
endTernary Operator
julia
x = 10
# condition ? value_if_true : value_if_false
result = x > 5 ? "greater than 5" : "5 or less"
println(result) # greater than 5
# Can be nested
grade = 85
level = grade >= 90 ? "A" : grade >= 80 ? "B" : grade >= 70 ? "C" : "D"
println(level) # BBitwise Operators
Bitwise Operations
julia
a = 0b1010 # 10 in binary
b = 0b1100 # 12 in binary
# Bitwise AND
println(a & b) # 8 (0b1000)
# Bitwise OR
println(a | b) # 14 (0b1110)
# Bitwise XOR
println(a ⊻ b) # 6 (0b0110)
println(xor(a, b)) # same
# Bitwise NOT
println(~a) # -11 (inversion)
# View binary representation
println(bitstring(UInt8(10))) # "00001010"
println(bitstring(UInt8(12))) # "00001100"Shift Operations
julia
a = 8 # 0b1000
# Left shift
println(a << 1) # 16 (0b10000)
println(a << 2) # 32 (0b100000)
# Right shift (arithmetic, preserves sign)
println(a >> 1) # 4 (0b100)
println(a >> 2) # 2 (0b10)
# Unsigned right shift
println(a >>> 1) # 4Special Operators
Range Operator
julia
# Using : to create range
r = 1:10
println(collect(r)) # [1, 2, 3, ..., 10]
# Range with step
r = 1:2:10
println(collect(r)) # [1, 3, 5, 7, 9]
# Using range function
r = range(0, 1, length=5)
println(collect(r)) # [0.0, 0.25, 0.5, 0.75, 1.0]String Operators
julia
# String concatenation
s1 = "Hello"
s2 = "World"
println(s1 * " " * s2) # "Hello World"
# String repetition
println("ab" ^ 3) # "ababab"
# String interpolation
name = "Julia"
println("Hello, $name!") # "Hello, Julia!"Dot Operator (Vectorization)
julia
# Dot operator for element-wise operations
a = [1, 2, 3, 4, 5]
# Element-wise operations
println(a .+ 10) # [11, 12, 13, 14, 15]
println(a .* 2) # [2, 4, 6, 8, 10]
println(a .^ 2) # [1, 4, 9, 16, 25]
# Element-wise comparison
println(a .> 3) # [false, false, false, true, true]
# Function vectorization
println(sqrt.(a)) # [1.0, 1.414..., ...]
println(sin.(a))
# @. macro for auto-vectorization
result = @. sin(a) + cos(a)Membership Operator
julia
# in operator
arr = [1, 2, 3, 4, 5]
println(3 in arr) # true
println(3 ∈ arr) # true (Unicode)
println(6 in arr) # false
println(6 ∉ arr) # true (not in)
# For strings
println('a' in "hello") # false
println('e' in "hello") # true
# For dictionaries
d = Dict("a" => 1, "b" => 2)
println("a" in keys(d)) # truePipe Operator
julia
# |> pipe operator
result = [1, 2, 3, 4, 5] |> sum |> sqrt
println(result) # 3.872...
# Equivalent to
result = sqrt(sum([1, 2, 3, 4, 5]))
# Chained operations
[1, 2, 3, 4, 5] |>
x -> filter(n -> n > 2, x) |>
sum |>
println # 12Operator Precedence
From highest to lowest:
^Power- Unary operators
+ - ! ~ * / ÷ % &Multiplication, division, bitwise AND+ - | ⊻Addition, subtraction, bitwise OR: ..Range< > <= >= == != ===Comparison&&Logical AND||Logical OR? :Conditional= += -= *= /=Assignment
julia
# Precedence examples
println(2 + 3 * 4) # 14 (multiplication first)
println((2 + 3) * 4) # 20
println(2 ^ 3 ^ 2) # 512 (power is right-associative)
println((2 ^ 3) ^ 2) # 64
println(true || false && false) # true (&& before ||)Custom Operators
Julia allows defining custom operators:
julia
# Define infix operator
⊕(a, b) = a + b + 1
println(3 ⊕ 4) # 8
# Using Unicode symbols
∥(a, b) = a * b / (a + b) # Parallel resistance
println(100 ∥ 100) # 50.0Next Steps
After learning operators, continue with:
- Math Functions - Built-in mathematical functions
- Control Flow - Conditionals and loops
- Functions - Defining your own functions