Julia Basic Syntax
Variables originate from mathematics; they are an abstract concept in computer languages that can store computation results or represent values.
Variables can be accessed through their variable names.
Julia variables are simple to use: just assign a value, no need to declare a type in advance:
Example
Assign 10 to variable x
julia
x =
10
10
Use the value of x in a calculation
julia
x +
1
11
Assign a string to variable site_name
julia
site_name =
"RUNOOB"
"RUNOOB"
Floating-point data
julia
marks_math =
9.5
9.5
From the examples we see that, unlike other programming languages such as C++ and Java, Julia does not require specifying variable types; it can automatically infer the type of the object on the right-hand side of the equal sign.
Note:The examples on this page are all interactive input in the REPL; lines starting with julia> are user input, and the lines below are results printed by the REPL. If you write code into a .jl file and run it with the julia command, ordinary expressions do not print results automatically; you need functions like println() to output them.
Variable naming rules
Julia variable names are case-sensitive; the naming rules are as follows:
- Variable names must start with a letter (A-Z, a-z), an underscore _, or certain Unicode characters; the first character cannot be a digit.
- Subsequent characters may include letters, digits, underscores, exclamation marks !, etc.
- Variable names are case-sensitive; Runoob and runoob are two different variables.
- Julia variable names can even use Unicode characters, such as Chinese or Greek letters:
Example
Greek letters can be used as variable names
julia
δ =
0.00001
1.0e-5
Chinese can also be used as variable names
julia
course =
"Julia Tutorial"
"Julia Tutorial"
In the REPL and most editors, you can enter these symbols by first typing a LaTeX command and then pressing Tab, e.g. \delta + Tab yields δ.
Names that cannot be used
Julia's reserved keywords cannot be used as variable names, e.g. else, try, function, end, etc.:
Example
julia
else
=
false
ERROR: ParseError:
Error @ none:1:1
else
=
false
└──┘ ── invalid identifier
Assignment expression
The assignment x = value is itself an expression whose value is the right-hand value, so assignments can be chained or embedded in other expressions:
Example
b = 2 + 2 has value 4, and the whole expression then adds 4 plus 3 and assigns it to a
julia
a =
(
b =
2
2
)
3
7
julia
a
7
julia
b
4
The difference between assignment and modification
This is one of the concepts most easily confused by Julia beginners: **assignmentgives a value a new name, whilemodification (mutation)** changes the content of the object itself.
For immutable types such as integers, a = 2; a = 3 merely makes the name a point to the new value 3; the number 2 itself does not change at all.
For mutable types such as arrays, the difference becomes apparent:
Example
julia
a =
[
1
,
2
,
3
]
Creating an Array
3
-element
Vector
{
Int64
}
:
1
2
3
julia
b = a
b = a does not copy the array; b and a point to the same array!
julia
a
[
1
]
=
42
Modify the first element
42
julia
b
The modification is visible through b as well
3
-element
Vector
{
Int64
}
:
42
2
3
To get an independent copy, use the copy() function; to recursively copy all nested elements use deepcopy().
Naming Convention
Julia has no mandatory naming requirements, but the community has established the following conventions:
| object | Convention | Example |
|---|---|---|
| Variables | lowercase, words may be separated by underscores (overuse discouraged) | runoob_count |
| Types Type | UpperCamelCase | Int64, Vector |
| Modules Module | UpperCamelCase | SparseArrays |
| Functions, macros | lowercase, no underscores | isempty, startswith |
| Functions that modify arguments | Starts with ! End |
push!, sort! |
Functions ending with an exclamation mark ! are called "mutating" or "in-place" functions; they modify the content of their input arguments rather than just returning a new value. For example sort(v) returns a new sorted array, while sort!(v) modifies array v in place.
Notes
Comments in Julia include single-line comments and multi-line comments.
Single-line comments in Julia start with #, for example:
Example
This is a line comment
This is another line comment
println
(
"Hello World!"
)
Multi-line comments are enclosed with #= and =# and can be nested:
Example
#=
1
, This is a line comment
2
, this is another line comment
=
println
(
"Hello World!"
)
Note:Unlike C, Julia has no slash-asterisk style block comments
/* ... */, multi-line comments must use#= ... =#.