, Java:
package my.demo
import java.util.*
// ...
Java, , : .
Int
Int
:
fun sum(a: Int, b: Int): Int {
return a + b
}
:
fun sum(a: Int, b: Int) = a + b
, (void Java):
fun printSum(a: Int, b: Int): Unit {
print(a + b)
}
Unit
:
fun printSum(a: Int, b: Int) {
print(a + b)
}
( ) :
val a: Int = 1
val b = 1 // `Int`
val c: Int // ,
c = 1 //
:
var x = 5 // `Int`
x += 1
, Java JavaScript, Kotlin .
//
/*
. */
Java, .
$name
${name}
:
fun main(args: Array<String>) {
if (args.size == 0) return
print("First argument: ${args[0]}")
}
fun max(a: Int, b: Int): Int {
if (a > b)
return a
else
return b
}
if (. . if ... else ):
fun max(a: Int, b: Int) = if (a > b) a else b
. if.
nullable ( ?
) null.
null str
:
fun parseInt(str: String): Int? {
// ...
}
, null:
fun main(args: Array<String>) {
if (args.size < 2) {
print(" ")
return
}
val x = parseInt(args[0])
val y = parseInt(args[1])
// `x * y` , null
if (x != null && y != null) {
// x y -nullable null
print(x * y)
}
}
// ...
if (x == null) {
print(" '${args[0]}'")
return
}
if (y == null) {
print(" '${args[1]}'")
return
}
// x y -nullable null
print(x * y)
. Null-.
is , . , :
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// `obj` `String`
return obj.length
}
// `obj` `Any`
return null
}
fun getStringLength(obj: Any): Int? {
if (obj !is String)
return null
// `obj` `String`
return obj.length
}
fun getStringLength(obj: Any): Int? {
// `obj` `String` `&&`
if (obj is String && obj.length > 0)
return obj.length
return null
}
for
fun main(args: Array<String>) {
for (arg in args)
print(arg)
}
for (i in args.indices)
print(args[i])
. for.
while
fun main(args: Array<String>) {
var i = 0
while (i < args.size)
print(args[i++])
}
. while.
when
fun cases(obj: Any) {
when (obj) {
1 -> print("One")
"Hello" -> print("Greeting")
is Long -> print("Long")
!is String -> print("Not a string")
else -> print("Unknown")
}
}
. when.
in:
if (x in 1..y-1)
print("OK")
:
if (x !in 0..array.lastIndex)
print("Out")
:
for (x in 1..5)
print(x)
:
for (name in names)
println(name)
in:
if (text in names) // names.contains(text) is called
print("Yes")
- :
names
.filter { it.startsWith("A") }
.sortedBy { it }
.map { it.toUpperCase() }
.forEach { print(it) }
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |