© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
T. LaversLearn to Program with Kotlinhttps://doi.org/10.1007/978-1-4842-6815-5_6

6. Values and Variables

Tim Lavers1  
(1)
Woonona, NSW, Australia
 

When we are writing a program, we may have a piece of information that will need to be referred to from other places in our program. Values and variables are used for this. They are defined using the keywords val and var, respectively. We can think of vals and vars as boxes containing items of data. A var is a box from which the data may be removed and replaced with some other data item. With a val, however, the data item is set once and can never be replaced.

6.1 Using vars

One common use of vars is to store the ...

Get Learn to Program with Kotlin: From the Basics to Projects with Text and Image Processing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.