-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Changes documentation on data types and variables. * feat: Changes documentation on loops, expressions and functions.
- Loading branch information
1 parent
dfbd3dc
commit 7eba670
Showing
5 changed files
with
193 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,66 @@ | ||
# Variáveis | ||
|
||
* [Val](#val) | ||
* [Var](#var) | ||
* [Escopos](#scopes) | ||
|
||
Em Kotlin, as variáveis podem ser declaradas usando dois tipos de palavras-chave, `val` ou `var`. | ||
|
||
<div id='val'></div> | ||
|
||
## Val | ||
|
||
Quando você precisar de variáveis em um escopo local, e que a atribuição de valor seja feita uma única vez, | ||
então você utiliza a palavra-chave `val`: | ||
Quando você precisar de variáveis em que a atribuição de valor seja feita uma única vez, então você utiliza a | ||
palavra-chave `val`: | ||
|
||
```kotlin | ||
val name: String = "kotlin" | ||
``` | ||
|
||
## Val | ||
<div id='var'></div> | ||
|
||
Quando você precisar de variáveis em um escopo global, ou, que possam receber uma atribuição de valor mais de uma vez, | ||
então você utiliza a palavra-chave `var`: | ||
## Var | ||
|
||
Quando você precisar de variáveis que possam receber uma atribuição de valor mais de uma vez, então você utiliza a | ||
palavra-chave `var`: | ||
|
||
```kotlin | ||
var name: String = "kotlin" | ||
name = "kotlin 123" | ||
``` | ||
|
||
<div id='scopes'></div> | ||
|
||
## Escopos | ||
|
||
As variáveis em Kotlin podem ser declaradas em escopos diferentes, isto é, podem ser variáveis locais ou com um escopo | ||
global. | ||
|
||
Vejamos um exemplo em uma [classe](CLASS.md): | ||
|
||
```kotlin | ||
class Language { | ||
val name = "Kotlin" | ||
|
||
fun register() { | ||
val template = "Language: %s." | ||
println(template.format(name)) | ||
} | ||
} | ||
``` | ||
|
||
Nesse exemplo, a variável `name` está no nível mais elevado/global, e a variável `template` é local porque está na | ||
função `register`. A variável de nível elevado `name` pode ser utilizada em qualquer lugar, inclusive em outros | ||
arquivos, enquanto a variável local `template` pode ser utilizada somente na função onde foi declarada: | ||
|
||
```kotlin | ||
fun main() { | ||
val language = Language() | ||
language.register() // Imprime "Language: Kotlin.". | ||
println(language.name) // Eu posso acessar a variável name, pois ela é global. Imprime "Kotlin". | ||
} | ||
``` | ||
|
||
_Você pode testar esse código [online](https://pl.kotl.in/2EZqH2QuF)._ | ||
|
||
Ir para [tipos de dados](TYPES.md). |