Julia Basic Syntax

Variables

Variables originate from mathematics and are abstract concepts in computer languages that can store calculation results or represent values.

Variables can be accessed through variable names.

Julia variable names consist of letters, digits, underscores_and the first character cannot be a digit.

Variable names are case-sensitive.

Using variables in Julia is very simple; just assign a value, as in the following example:

Example

# Assign 10 to variable x
x = 10

# Use the value of x for calculation
x + 1
11

# Assign a string to variable site_name
site_name = "EXAMPLE"

# Floating-point data
marks_math = 9.5

The output under the interactive command is as follows:

From the example, we can 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 side of the equals sign.

Naming Conventions

It is recommended to follow these naming conventions:
  • Variable names use lowercase.
  • Use underscores_to separate words in variable names.
  • TypesTypeand modulesModuleare named with an uppercase letter at the beginning, and use uppercase letters rather than underscores to separate words.
  • Functions (function) and macros (macro) should be named using lowercase and do not use underscores.
  • Functions that modify their input parameters should use!`!` at the end. These functions are sometimes called "mutating" or "in-place" functions, because when called, they modify the contents of their input parameters rather than just returning a value.

Comments

In Julia, comments include single-line comments and multi-line comments.

In Julia, single-line comments start with#`#`, for example:

Example

# This is a line of comment
# This is another line of comment
println("Hello World!")

Multi-line comments use#=and=#`#= =#` to enclose the comments, for example:

Example

#=
1、This is a line of comment
2、This is another line of comment
=#
println("Hello World!")
Other Extensions