Sage-Code Laboratory


Page description. To be replaced!


When you wish to declare a variable in Julia you use Duck typing. For this you use a type literal to provide enough information to the compiler to derive the type. Once the type is established you can use the variable in expressions. In Julia evening is an expression.

message = "Hello World"

The message is a string variable having value "Hello World". Observe that we do not use keyword "var" like in JavaScript or other languages. Also in Julia a variable can be re-purpose. The type can be changed when the variable is assigned using "=" symbol. A new value of a different type can be assigned.

Arithmetic Operators

The following operators are supported on all primitive numeric types:

Expression Name Description
+x unary plus the identity operation
-x unary minus maps values to their additive inverses
x + y binary plus performs addition
x – y binary minus performs subtraction
x * y times performs multiplication
x / y divide performs division
x \ y inverse divide equivalent to y / x
x ^ y power raises x to the y power
x % y remainder equivalent to rem(x,y)

Bitwise Operators

These operators are performing fast low-level operations on bits on native integer types.

Expression Name
~x bitwise not
x & y bitwise and
x | y bitwise or
x $ y bitwise xor exclusive or: (deprecated)
x >>> y logical shift right
x >> y arithmetic shift right
x << y logical/arithmetic shift left


Every binary arithmetic and bitwise operator also has an updating version that assigns the result of the operation back into its left operand. The updating version of the binary operator is formed by placing equal symbol "=" immediately after the operator like this:

+=, -=, *=, /=, ^=, %=, ~=, &=, $=, >>=, <<=

Note: The update operator is not in-place operator like it is in C. What it means is the variable on the left side can change its type and value in the same time. So these operators are actually version of assign operator.

x *= 2 #is equivalent to x = x * 2

Comparison Operators

A numeric comparison can be used into a conditional expression. It has a result of type Boolean that is true or false.

Operator Name
== equality
!=, ≠ inequality (disjunctive)
< less than
<=, ≤ less than or equal to
> greater than
>=, ≥ greater than or equal to

Logical Operators

Logical operators and comparison operators can be combined to create logical expressions that evaluate to true or false.

Operator Name
&& AND
|| OR

Table of Truth

In Julia Boolean values are: true, false. We cant use 1 and 0 like in other languages. Next is a combination of possible values for A, B and the result of several basic expressions. A&&B meaning A and B, A||B meaning A or B then !A meaning negation of A an then !B meaning negation of B. If you are not familiar with these expressions then you need to look into Boolean algebra.

A B A&&B A||B !A !B
false false false false true true
false true false true true 0
true false false true false true
true true true true false false


In the next table we present a mingle of symbols used by Julia for different purposes. Some are operators but some are just conventions.

symbol meaning
@m invoke macro m; followed by space-separated expressions
! prefix "not" operator
a!( ) at the end of a function name, ! indicates that a function modifies its argument(s)
# begin single line comment
#= begin multi-line comment (these are nestable)
=# end multi-line comment
$ string and expression interpolation
% remainder operator
^ exponent operator
& bitwise and
&& short-circuiting Boolean "and"
| bitwise or
|| short-circuiting Boolean "or"
bitwise xor operator (use to be $ but I think it was changed)
* number multiplication, matrix multiplication, string concatenation
() the empty tuple
~ bitwise "not" operator
\ backslash operator
complex transpose operator Aᴴ
a[] array indexing
[,] vertical concatenation
[;] also vertical concatenation
[ ] with space-separated expressions, horizontal concatenation
T{ } parametric type instantiation
; statement separator
, separate function arguments or tuple components
? 3-argument conditional operator (conditional ? if_true : if_false)
"" delimit string literals
" delimit character literals
` ` delimit external process (command) specifications
splice arguments into a function call or declare a varargs function or type
. access named fields in objects/modules
a:b range a, a+1, a+2, …, b
a:s:b range a, a+s, a+2s, …, b
: index an entire dimension (1:end)
:: type annotation, depending on context
: ( ) quoted expression
:a symbol a
<: sub-type operator
>: supertype operator (reverse of subtype operator)
=== equal comparison operator

Read next: Control Flow