变量与常量 #
一、变量基础 #
1.1 变量定义 #
在Julia中,变量通过赋值来创建,不需要声明类型:
julia
x = 10
name = "Julia"
arr = [1, 2, 3]
1.2 变量类型 #
Julia会自动推断变量的类型:
julia
x = 10
typeof(x)
y = 3.14
typeof(y)
z = "Hello"
typeof(z)
1.3 类型注解 #
可以使用::操作符添加类型注解:
julia
x::Int = 10
y::Float64 = 3.14
name::String = "Julia"
类型注解可以用于类型转换:
julia
x = 10.5
y = x::Int
1.4 变量重新赋值 #
变量可以重新赋值为不同类型:
julia
x = 10
x = "Hello"
x = [1, 2, 3]
但如果使用了类型注解,则必须匹配类型:
julia
x::Int = 10
x = "Hello"
二、命名规则 #
2.1 合法标识符 #
Julia变量名必须以字母、下划线或Unicode字符开头:
julia
x = 1
_name = 2
变量 = 3
α = 4
δx = 5
2.2 命名约定 #
Julia有一套命名约定:
| 类型 | 约定 | 示例 |
|---|---|---|
| 变量 | 小写,下划线分隔 | my_variable |
| 常量 | 大写,下划线分隔 | MY_CONSTANT |
| 函数 | 小写,无下划线 | myfunction |
| 类型/模块 | 驼峰命名 | MyType |
| 宏 | 小写,无下划线 | @my_macro |
| 修改参数的函数 | 以!结尾 |
push! |
2.3 特殊字符 #
Julia支持Unicode字符作为变量名:
julia
α = 1
β = 2
γ = α + β
δx = 0.001
δy = 0.002
∑(arr) = sum(arr)
∏(arr) = prod(arr)
2.4 避免使用的名称 #
以下名称有特殊含义:
julia
ans
不要使用内置函数名作为变量名:
julia
sum = 10
sum([1, 2, 3])
三、常量 #
3.1 定义常量 #
使用const关键字定义常量:
julia
const PI = 3.14159
const MAX_SIZE = 100
const DEFAULT_NAME = "Julia"
3.2 常量的特点 #
常量在编译时会被优化,性能更好:
julia
const NUM = 1000000
function sum_const()
total = 0
for i in 1:NUM
total += i
end
return total
end
3.3 常量重新赋值 #
常量可以被重新赋值(会警告),但不能改变类型:
julia
const X = 10
X = 20
const Y = 10
Y = 10.5
3.4 全局常量 #
在模块级别定义的常量是全局常量:
julia
module MyModule
const GLOBAL_CONST = 100
end
3.5 常量与性能 #
使用常量可以显著提高性能:
julia
using BenchmarkTools
global_var = 100
const global_const = 100
function use_global()
s = 0
for i in 1:global_var
s += i
end
return s
end
function use_const()
s = 0
for i in 1:global_const
s += i
end
return s
end
@btime use_global()
@btime use_const()
四、作用域 #
4.1 全局作用域 #
在模块或主程序中定义的变量具有全局作用域:
julia
x = 10
function foo()
println(x)
end
foo()
4.2 局部作用域 #
函数内部定义的变量是局部变量:
julia
function bar()
y = 20
return y
end
bar()
4.3 软作用域 vs 硬作用域 #
Julia有两种局部作用域:
- 硬作用域:函数、结构体、let块
- 软作用域:for、while、try-catch、结构体
julia
x = 0
for i in 1:10
x = i
end
println(x)
function foo()
x = 100
end
foo()
println(x)
4.4 global关键字 #
在局部作用域中修改全局变量需要使用global:
julia
x = 0
function increment()
global x += 1
end
increment()
println(x)
4.5 local关键字 #
local关键字用于声明局部变量:
julia
x = 0
for i in 1:10
local x = i
end
println(x)
4.6 let块 #
let块创建新的局部作用域:
julia
x = 10
let x = 20
println("Inside: $x")
end
println("Outside: $x")
let块可以创建闭包:
julia
function make_counters()
let count = 0
() -> (count += 1), () -> count
end
end
inc, get = make_counters()
inc()
inc()
get()
五、变量类型检查 #
5.1 typeof #
typeof返回变量的类型:
julia
typeof(10)
typeof(3.14)
typeof("Hello")
typeof([1, 2, 3])
5.2 isa #
isa检查变量是否是某个类型:
julia
10 isa Int
3.14 isa Float64
"Hello" isa String
10 isa Number
5.3 typeof vs isa #
julia
typeof(10) == Int
typeof(10) <: Integer
10 isa Integer
5.4 类型层次 #
julia
supertype(Int)
supertype(Integer)
supertype(Real)
supertype(Number)
六、特殊变量 #
6.1 ans #
ans存储REPL中上一个表达式的结果:
julia
julia> 1 + 2
3
julia> ans
3
6.2 nothing #
nothing表示没有值:
julia
function print_nothing()
println("Hello")
return nothing
end
result = print_nothing()
result === nothing
6.3 missing #
missing表示缺失值:
julia
using Missings
x = missing
x + 1
ismissing(x)
6.4 undef #
undef用于创建未初始化的数组:
julia
arr = Vector{Int}(undef, 5)
七、最佳实践 #
7.1 使用有意义的名称 #
julia
n = 10
num_iterations = 10
f = 3.14
frequency = 3.14
7.2 避免全局变量 #
全局变量会影响性能,尽量使用局部变量:
julia
global x = 10
function bad()
return x * 2
end
function good(x)
return x * 2
end
7.3 使用常量 #
对于不变的值,使用const:
julia
const MAX_ITERATIONS = 1000
const TOLERANCE = 1e-10
7.4 类型注解 #
在函数参数上使用类型注解可以提高性能:
julia
function process(x::Int)
return x * 2
end
function process(x::Float64)
return x * 2.0
end
7.5 避免类型不稳定 #
避免变量类型改变:
julia
function bad()
x = 1
x = 1.0
x = "1"
return x
end
function good()
x::Float64 = 1.0
return x
end
八、实践练习 #
8.1 练习1:变量命名 #
创建符合命名规范的变量:
julia
user_name = "Alice"
user_age = 25
MAX_LOGIN_ATTEMPTS = 3
DEFAULT_TIMEOUT = 30.0
8.2 练习2:作用域 #
理解作用域:
julia
x = 10
function scope_test()
local x = 20
println("Inside function: $x")
end
scope_test()
println("Outside function: $x")
for i in 1:3
global x += i
end
println("After loop: $x")
8.3 练习3:常量使用 #
使用常量优化性能:
julia
const N = 10^6
function compute_sum()
total = 0
for i in 1:N
total += i
end
return total
end
@time compute_sum()
8.4 练习4:类型检查 #
julia
function process_value(x)
if x isa Int
println("Integer: $x")
elseif x isa Float64
println("Float: $x")
elseif x isa String
println("String: $x")
else
println("Unknown type: $(typeof(x))")
end
end
process_value(10)
process_value(3.14)
process_value("Hello")
九、总结 #
本章我们学习了:
- 变量定义:自动类型推断和类型注解
- 命名规则:合法标识符和命名约定
- 常量:const关键字和性能优势
- 作用域:全局、局部、软硬作用域
- 特殊变量:ans、nothing、missing
- 最佳实践:命名、避免全局变量、类型稳定
接下来让我们学习Julia的数字类型!
最后更新:2026-03-27