# Variable definition like C++

**URL:** <https://discourse.julialang.org/t/variable-definition-like-c/123038>\
**Category:** New to Julia\
**Created:** [November 25, 2024, 7:05am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038 "2024-11-25T07:05:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 25, 2024, 7:05am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/1 "2024-11-25T07:05:58Z")

</div>

Hello,  
In the C++ programming language, the definition of a variable is as follows:

```julia
int res = 1;

```

How is this done in Julia?

Thank you.

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [November 25, 2024, 7:13am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/2 "2024-11-25T07:13:40Z")

</div>

In Julia, you just set the value to something.

So, in this case…

```julia
res = 1

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [November 25, 2024, 7:14am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/3 "2024-11-25T07:14:42Z")

</div>

Hi,  
How about:

```julia
a = Int(1)

s = String("Hello")

```

Are these wrong?

---

<div class="post-metadata">

**Author:** ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)\
**Post date:** [November 25, 2024, 7:15am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/4 "2024-11-25T07:15:51Z")

</div>

They work, but they’re redundant.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 25, 2024, 7:45am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/5 "2024-11-25T07:45:57Z")

</div>

Minor addition: `int` in C/C++ corresponds to `Int32` in Julia. So, technically, you could write `res = Int32(1)` to get the same value as in your C++ example. However, unless you really care about the difference between `Int32` and `Int64` (the default in Julia), you shouldn’t care about this.

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [November 25, 2024, 8:00am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/6 "2024-11-25T08:00:00Z")

</div>

You can alternatively specify the type as part of the assignment:

```Julia
a::Int64 = 1

```

I hesitate to mention this, though, because it has different semantics to normal variable assignment and is rarely used. In this case the variable `a` cannot be reassigned a different type later on: all assignments to `a` will attempt a (safe) conversion or else will throw an error.

I would recommend not to use this form unless you know why you need it (perhaps, for example, for type stability reasons).

In any case, the documentation for assignments is here: [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Type-Declarations)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 25, 2024, 8:18am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/7 "2024-11-25T08:18:55Z")

</div>

> [@hack3rcon](#):
>
> `s = String("Hello")`

I’m not completely sure about `String`, but in general it’s not a good idea to explicitly call a constructor on something that’s already of the same type, as you risk creating the value twice, needlessly.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [November 25, 2024, 8:53am UTC](https://discourse.julialang.org/t/variable-definition-like-c/123038/8 "2024-11-25T08:53:28Z")

</div>

Seconding @DNF. These do work but they _are_ wrong because `Typename(object)` by convention returns a freshly created object ([Conversion and Promotion · The Julia Language](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Conversion-vs.-Construction)). It does not matter for `isbits` types but it causes copying for containers (e.g., `Vector([1, 2, 3])` would create two copies of the array immediately discarding one of them).

If you want to ensure the variable type, use

```julia
a::Int = 1

```

or

```julia
a = 1::Int

```

The first fixes the type of the variable, so that if rhp is of different type, then it’s `convert`ed to a given type before writing into `a`.  
The second asserts the type of `rhp`, so that if it’s of a different type, you’ll get `AssertionError`.

In practice, I’d prefer the former, but for self-teaching you may want to try the latter to see which types to expect from different expressions and build some understanding of the type system.
