Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

KotlinVariables


Kotlin Variables

Variables are containers for storing data values.

To create a variable, usevar orval, and assign a value to it with the equal sign (=):

Syntax

varvariableName =valuevalvariableName =value

Example

var name = "John"val birthyear = 1975println(name)          // Print the value of nameprintln(birthyear)     // Print the value of birthyear
Try it Yourself »

The difference betweenvar andval is that variables declared with thevar keywordcan be changed/modified, whileval variablescannot.


Variable Type

Unlike many other programming languages, variables in Kotlin do not need to be declared with a specifiedtype (like "String" for text or "Int" for numbers, if you are familiar with those).

To create a variable in Kotlin that should store text and another that should store a number, look at the following example:

Example

var name = "John"      // String (text)val birthyear = 1975   // Int (number)println(name)          // Print the value of nameprintln(birthyear)     // Print the value of birthyear
Try it Yourself »

Kotlin is smart enough to understand that"John" is aString (text), and that1975 is anInt (number) variable.

However, it is possible to specify the type if you insist:

Example

var name: String = "John" // Stringval birthyear: Int = 1975 // Intprintln(name)println(birthyear)
Try it Yourself »

You can also declare a variable without assigning the value, and assign the value later.However, this is only possible when you specify the type:

Example

This works fine:

var name: Stringname = "John"println(name)
Try it Yourself »

Example

This will generate an error:

var namename = "John"println(name)
Try it Yourself »

Note: You will learn more aboutData Types in the next chapter.



Notes onval

When you create a variable with theval keyword, the valuecannot be changed/reassigned.

The following example will generate an error:

Example

val name = "John"name = "Robert"  // Error (Val cannot be reassigned)println(name)
Try it Yourself »

When usingvar, you can change the value whenever you want:

Example

var name = "John"name = "Robert"println(name)
Try it Yourself »

So When To Useval?

Theval keyword is useful when you want a variable to always store the same value, like PI (3.14159...):

Example

val pi = 3.14159265359println(pi)
Try it Yourself »

Display Variables

Like you have seen with the examples above, theprintln() method is often used to display variables.

To combine both text and a variable, use the+ character:

Example

val name = "John"println("Hello " + name)
Try it Yourself »

You can also use the+ character to add a variable to another variable:

Example

val firstName = "John "val lastName = "Doe"val fullName = firstName + lastNameprintln(fullName)
Try it Yourself »

For numeric values, the+ character works as a mathematical operator:

Example

val x = 5val y = 6println(x + y) // Print the value of x + y
Try it Yourself »

From the example above, you can expect:

  • x stores the value 5
  • y stores the value 6
  • Then we use theprintln() method to display the value of x + y, which is11

Variable Names

A variable can have a short name (like x and y) or more descriptive names (age, sum, totalVolume).

The general rule for Kotlin variables are:

  • Names can contain letters, digits, underscores, and dollar signs
  • Names should start with a letter
  • Names can also begin with $ and _ (but we will not use it in this tutorial)
  • Names are case sensitive ("myVar" and "myvar" are different variables)
  • Names should start with a lowercase letter and it cannot contain whitespace
  • Reserved words (like Kotlin keywords, such as var orString) cannot be used as names

camelCase variables

You might notice that we usedfirstName andlastName as variable names in the example above, instead of firstname and lastname. This is called "camelCase", and it is considered as good practice as it makes it easier to read when you have a variable name with different words in it, for example "myFavoriteFood", "rateActionMovies" etc.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp