Data Types Variables and Values in Julia
This tutorial discusses the Data Types Variables and Values in Julia programming language with the help of simple programming examples.
Julia programming language has rich support for data types, variables, and values.
A variable is a name associated with a value. The variable names in Julia are case sensitive, that is the variable name a is different from variable A.
A value is a constant assigned to a variable name, for example, 10, 10.2, “Julia” etc. A variable name does not have a data type, its value has. Data Types specify the type of content.
Like Python, Julia is a dynamically typed programming language. Hence, the value assigned to a variable decided the type of a variable. For example, x = 10, means x is an integer variable. Dynamic typing of Julia makes writing code with Julia more flexible and fast.
Data Types in Julia
Julia has rich support for data types. The below figure show the data types and their hierarchy.
The figure shows Number can be of two types, one is Complex number and the other is a real number. Similarly, the Real number has four possibilities they are abstract (float), Integer, Irrational, and Rational. These types, in turn, have subtypes. Please go through the above figure. The terminal or leaf nodes represent the actual data types. The terminal or leaf nodes are instantiated by assigning value to a variable. But the type at non-terminal nodes are abstract in nature, they cannot be instantiated.
The programmer need not remember the data type hierarchy. The Julia provides function called supertype() and subtype(). The supertypes() function gives the supertype or parent type of the current type. Similarly, subtype() function gives the subtypes or children types of the current type.
For Example.
supertype(Float64) # supertype of Float64 are displayed
subtypes(Integer) # subtypes of Integer are displayed
One can check the type of variable using either type() or eltype() functions.
a = 10
typeof(a) #displays the type of variable a,
#in this case type of a in Integer
eltype(a) #displays the type of elements in collection a
Also, you can assign type to a variable at the time of defining a variable using :: operator.
For Example
a::Float64 # fixes type of variable a to Float to generate type-stable code
b::Int = 10 # fixes type of variable b to Int and assigns a value 10 to it.
The sizeof() function returns the variable size in Julia. Similarly, varinfo() function returns the memory state of the system.
Example:
a = 10
sizeof(a) # returns 8 which means the size of interger type in 8 bytes.
varinfo() # displays the memory state information.
Julia does not clear its workspace by default. To clear the workspace, one can either set the variable to 0 or use the GC.gc() function.
a = 0
OR
GC.gc() #garbage collector which clears the workspace
Variable names with Unicode symbols in Julia
The programming languages like python, C, C++, Java, etc. do not allow us to use special characters and Unicode symbols as a variable name. In Julia, you can create a variable name with special characters such as α, β, π, etc. To create variable names with these Unicode symbols, you can use a greek letter as a variable by typing its LATEX’s name plus pressing tab. Once you press the tab button, the equivalent Unicode variable is generated. The following fragment of code demonstrates the use of Unicode symbols as variable names in Julia programming language.
\alpha (+ press Tab) = 10 #Creates a variable called α with value 10
\beta(+ press Tab) = 3 #Creates a variable called βwith value 3
Generated equivalant code is:
α = 10
β = 3
Now, you can perorme any operation on theses variables like,
Sum = α + β # Calculates the sum of α & β
println(Sum) # Displays the value of Sum variable
# that 13 in this case
Irrational variables in Julia
Julia Supports the irrational variable, which is unique among the programming languages. The irrational type variables are created using // operator. The numerator() function of Julia returns the numerator of a variable. Similarly, denominator() function returns the denominator of a variable.
The following fragment of code demonstarte the use of irrational numbers in Julia programming language.
a = 1//2
b = 3//7
println("The Value of a is ", a)
println("The Value of b is ", b)
c = a+b
println("The Value of c is ", c)
println("The Value of numerator of c is ", numerator(c))
println("The Value of numerator of c is ", denominator(c))
Output:
The Value of a is 1//2
The Value of b is 3//7
The Value of c is 13//14
The Value of numerator of c is 13
The Value of numerator of c is 14
With the help of a fragment of programming example, this tutorial discusses the values, variables, and data types in Julia. In the coming blog posts, we will discuss the Julia programming language in detail. Stay tuned and happy Julia learning. If you like do share with your friends and subscribe to the YouTube channel for more videos.