Variables

0
(0)

C

C++

C#

Common Lisp

Dart

F#

Go

Java

JavaScript

Kotlin

Python

Rust

C

1
2
3
4
5
6
7
8
// one variable
int number;
// several variables of the same type
int number1, number2, number3;
//one variable with initialization
int number = 1;
//  defining multiple variables with initialization
int number1 = 1, number2 = 2, number3 = 3;

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// one variable
int number;
// several variables of the same type
int number1, number2, number3;
// Initialization
int number = 1;     // assignment notation
int number {1};     // braced intialization - initialization in curly braces
int number (1);     // functional notation
// defining multiple variables with initialization
int age1 {22}, age2 (23), age3 = 24;
// implicitly typed variable definition
auto number {1123};

C#

1
2
3
4
5
6
7
8
9
10
// one variable
int number;
// several variables of the same type
int number1, number2, number3;
// one variable with initialization
int number4 = 1;
// defining multiple variables with typing
int number5 = 1, number6 = 2, number7 = 3;
//implicitly typed variable definition
var number8 = 1123;

Common Lisp

In Clisp, variables can be local or global.

Local variables:

1
2
3
4
5
6
7
8
9
10
11
12
; one variable without initialization
(let ((number1))
    (format t "~a" number1))
;several variables without initialization
(let ((number1) (number2))
    (format t "number1: ~a   number2: ~a" number1 number2))
; one variable with initialization
(let ((number1 10))
    (format t "~a" number1))    ; 10
; defining multiple variables with initialization
(let ((number1 10) (number2 20))
    (format t "number1: ~a   number2: ~a" number1 number2)) ; number1: 10   number2: 20

Global variables

1
2
3
4
; sets the initial value if the variable is not yet defined
(defvar *number1* 1)
; sets the initial value even if the variable already exists
(defparameter *number1* 1)

Dart

1
2
3
4
5
6
// one variable with initialization
int number = 1;
//implicitly typed variable definition
var number1 = 1123;
// defining a variable whose type can be changed
dynamic number2 = 1123;

F#

1
2
3
4
// implicitly typed variable definition
let mutable number = 0;
// explicitly typed variable definition
let mutable number2 : int = 0;

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import "fmt"
 
func main() {
    // variable without initialization
    var number1 int32
    //several variables without initialization
    var number2, number3 int32
    var (
        number4 int32
        number5 int64
    )
    //variable with initialization
    var number6 int32 = 6
    number7 := 7
    //multiple variables with initialization
    number8, number9 := 8, 9
    var (
        number10 int32  = 10
        number11 int64 = 11
    )
    var (
        number12 = 12
        number13 = 13
    )
    fmt.Println("number1: ", number1)
    fmt.Println("number2: ", number2)
    fmt.Println("number3: ", number3)
    fmt.Println("number4: ", number4)
    fmt.Println("number5: ", number5)
    fmt.Println("number6: ", number6)
    fmt.Println("number7: ", number7)
    fmt.Println("number8: ", number8)
    fmt.Println("number9: ", number9)
    fmt.Println("number10: ", number10)
    fmt.Println("number11: ", number11)
    fmt.Println("number12: ", number12)
    fmt.Println("number13: ", number13)
}

Java

1
2
3
4
5
6
7
8
9
10
// one variable
int number;
// several variables of the same type
int number1, number2, number3;
// one variable with initialization
int number4 = 1;
// defining multiple variables with typing
int number5 = 1, number6 = 2, number7 = 3;
// implicitly typed variable definition
var number8 = 1123;

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//variable without initialization
let number1;
var number2;
// several variables without initialization
let number4, number5;
var number6, number7;
// variable with initialization
let number8 = 8;
var number9 = 9;
// multiple variables with initialization
let number10 = 10, number11 = 11, number12 = 12;
var number13 = 13, number14 = 14, number15 = 15;
// defining a global variable when strict mode is not used
number16 = 112233;

Kotlin

In Kotlin, variables can be mutable (declared with var) and immutable, read-only variables (declared with val).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//explicitly typed read-only variable
val number: Int = 1
// implicitly typed read-only variable
val number1  = 1
// mutable variable with explicit typing
val number2: Int = 1
//mutable variable with implicit typing
val number3  = 1
//read-only variable without initialization
val number: Int
number = 2
//mutable variable without initialization
var number1 : Int
number1 = 5

Python

1
2
3
4
#one variable with initialization
number = 1;
# defining multiple variables with initialization
number1 = 1, number2 = 2, number3 = 3;

Rust

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main(){
// explicitly typed read-only variable
let number: u32 = 1;
// implicitly typed read-only variable
let number1  = 1;
// mutable variable with explicit typing
let mut  number2: u32 = 1;
// mutable variable with implicit typing
let mut  number3  = 1;
// read-only variable without initialization
let number4: u32;
number4 = 2;
//mutable variable without initialization
let mut  number5 : u32;
number5 = 5;
println!("number4 = {}", number4);
println!("number5 = {}", number5);
}

 

Next

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: Constants →

Leave a Reply

Your email address will not be published. Required fields are marked *

A simple, neat ad bar featuring a few text ads appears at the bottom of every website within the network.