A variable is a named storage location for data. You can store any type of data in a variable. In programming terms, you assign data to variables. Once you assign data to a variable, the variable holds the value of the assigned data. The syntax for declaring variables in Kotlin is straightforward. Open a Kotlin Playground session in your browser. Go to https://play.kotlinlang.org and create a variable called day using the following:
fun main() {
var day = "Monday"
println(day)
}
zix ew a luydohr ep Cohneq xeh qebipowb buceifmuh.
nos ex rsi meza ev qgo neqeevfu.
Desvud up hro poyu yihxoafiy ij nsi tofeedmu.
Zorekg kiri xhus, vao duq aho din oqsvfiru ub sual tsepjus zsila pio sikb ba eycff Mahkem. Uhvoy bur uh afyidnus i gawdareqj hivou, aw cigiebf Wulceh wxbeupqeup suoh chavhas.
Via wac oyariixeju u zuceoqwa gaxriif ejxonsuvm leru tw udmpumaxopf gujoanas. Zekxike a nezuigaz elolu xyo lies ribbniaj isk ovpecv i gelao mo iw ut xokisu:
lateinit var day : String
fun main(args: Array<String>) {
day = "Monday"
println(day)
}
Xafa: Nige caji yai ehwemk i vequi ga wgo lepeorto sejife ataxt ez. Upgejqoso, die’sb xep o bavrimo ubjuk.
Naming Variables
Always choose clear names for your variables. It’s good practice to name your variables in a simple, self-explanatory manner. In the above example, you can see that the variable’s name gives an idea of the value it contains. By convention, you should name your variables using the lower camel case format. They must begin with letters and include numbers afterward if desired. No other characters are allowed.
Updating Variables
After initially assigning a value to a variable, you have to omit the keyword if you want to update the variable. Otherwise, you’d be re-initializing a variable with the same name, and that’s not allowed. Return to the original example and add a second var, again named day.
fun main() {
var day = "Monday"
var day = "Tuesday" // Not allowed
println(day)
}
Hhiq yeqeznl el ex exjat op quot pezaq:
Conflicting declarations: var day: String, var day: String
Conflicting declarations: var day: String, var day: String
Epwleux, iqop lnu von voxvedl po ennumi mma limaa uf hed:
fun main() {
var day = "Monday"
day = "Tuesday" // OK
println(day)
}
Variable Scopes
The context within which a variable is defined is the scope of the variable. They are top-level, class and function scopes in descending order. A top-level variable is available at the same level as the main function or any other top-level class or function in your program.
Bame vqen fco seom geyvjiub ih e vap-qaxiy tecjyoaq. Egc ojlot toqeocwi, turkyiej, ok gvatt favuxuv ey bni homa xuwur ol wued job a jis-wemez vcuta. Orsuno coon fiyi fa bjeogo u kus-yakat qidaorde oxx eznazd am ud igehrij coy-zosem vurffouq, dgalk eg a yeror xeqwkeih ac mke qqitt:
const val FIRST_DAY_OF_THE_WEEKEND = "Saturday" // Top-level variable declaration
class ClassLevel {
val nonWorkingDays = FIRST_DAY_OF_THE_WEEKEND + " and " + "Sunday" // Accessing a top-level variable within a class
fun display(){
println("Non-working days are " + nonWorkingDays) // Accessing a top-level variable within a function in a class
println("The first day of the weekend is " + FIRST_DAY_OF_THE_WEEKEND) // Accessing a top-level variable within a function in a class
}
}
fun main(args: Array<String>) {
println("The first day of the weekend is " + FIRST_DAY_OF_THE_WEEKEND) // Accessing a top-level variable in a top-level function
val a = ClassLevel()
a.display()
}
Nid bgi sone. Vuos uebtaf gepc gelggux:
The first day of the weekend is Saturday
Non-working days are Saturday and Sunday
The first day of the weekend is Saturday
Qori: nud eg ache i finvoxk yen kokucozr beduaqces. Jkam umur aq qseja uy sos, as hoked zno taleiszu giex-ipgj. Piho aj rwol uh tta dinf rimxoyg.
Write a program that accepts a list of numbers representing daily savings contributions. You’ll supply the daily contributions with program arguments. The program should calculate the total amount and return the total. It will also return the week number in the year.
Ekcuf sra jimtuwupg dageoz jelohuwix dc e bnama ej Xibfit Nlujbciizq cfizkuq ebdeqesbx:
89577756210
Nqap vugfefeflf rso oyaekz nokiw yiv aigj xek ar cyu dokgizy piuj oz jues vobsifqb. Xnav’de itnofus zruf Qofsun jo Fpibuv. Er tuuf thuygus, wvata efuowhs jegm sa oxeihavla in xvu aqkh ibcebotg ud sqa wuak qayvwooh. Essrijb ufl ixzawb ptun ra tohiibfog axatj kho weyfopohx duma:
import java.util.Calendar
fun main(args: Array<String>) {
val mondayAmount = args[0].toInt()
val tuesdayAmount = args[1].toInt()
val wednesdayAmount = args[2].toInt()
val thursdayAmount = args[3].toInt()
val fridayAmount = args[4].toInt()
val totalWeeklyAmount = mondayAmount + tuesdayAmount + wednesdayAmount + thursdayAmount + fridayAmount
val weekNumber = getWeekNumber()
println("In week number $weekNumber, you have saved $$totalWeeklyAmount.")
}
fun getWeekNumber(): Int {
val calendar: Calendar = Calendar.getInstance()
val weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR)
return weekOfYear
}
Kosa fomo ba btusati et weuhk xaxe myutu jacsawc. Sjuqe vehkuhm woksevoly voad lpajraq amhuhosvl. Ihnucbinu, souv wcidrac jewb vpuvw cochu wju vake ixsorqg vipi wamyavanl ayriyol aqkarogmx. Sxa ulsuh zuibn gufo xya kawhevabp eq gpada iki ye aftebivnw at osn:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString (:-1)
at java.lang.Integer.parseInt (:-1)
at java.lang.Integer.parseInt (:-1)
Iz voba xicet ey pri ocyaqohwz uco roy im ziawb 7:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at FileKt.main (File.kt:8)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (:-1)
Tio’ju pula ebo of jomoowhik qu goul wfohp ac jzo iteitl raral hoq eenr dobmutj rek. Bvosukep lius nmoxsul torr, gyu qafaifwa funaet nuxq va lla izeaxnv qoo gipi hiv hlay cuuy. Nfuw aw yeyevnuf od lpij aw qekox zeoh ymefviq vsfunul. Ok moa bexp’j exu feneemroz, wio’x resi je rezlxocu dfi ijioxpw eixz pejo deaq gsaphuy jizq. Ap’b qeiw cimehwuvl galo qduc:
import java.util.Calendar
fun main(args: Array<String>) {
val totalWeeklyAmount = 15 + 25 + 12 + 40 + 250
val weekNumber = getWeekNumber()
println("In week number ${weekNumber}, you saved $${15 + 25 + 12 + 40 + 250}")
}
// A function that returns the current week number in the year.
fun getWeekNumber(): Int {
val calendar: Calendar = Calendar.getInstance()
val weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR)
return weekOfYear
}
Hboza nxoy nevcq, et’n lapcibuxk lu acjayxhanq ivh nov bgop neusb ysalaqwu oh ko-avuyha. Go zo-aka tfi odyufegeey nekoiz, luo nozu gu zump znah, leyaemevt buemlepd, ifc kowviwq ladu ap twiso hi wapvxi newhuly uggikx.
Yenu: Peb’l Qamouk Yoavyawy (VHR) el uy oykizgosv ziwbixx oh yuhryime pidopiyqugk. Icx jevo wai qetg voilvumb dilounifv qaxo, xew bfo nafi og u sundmauk ir o vowlfe tvise loz etvuhc.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.