Kotlin Basics: Functions, Nullability, In Keyword, Type Checks, and When Expressions
This article introduces core Kotlin language features—including function definitions, nullable types and null‑checking, the use of the in keyword for ranges and collections, type checking with is, and the when expression—providing code examples and explanations for Android developers.
Introduction Before diving into the examples, note that Kotlin allows omitting semicolons, uses the fun keyword to declare functions, requires override for overridden methods, places parameters as name: Type , and does not enforce a one‑to‑one mapping between source files and package directories, although Android manifest entries must match the actual package path.
Defining Functions
Example 1 shows a function with two Int parameters returning an Int value:
fun sum(a: Int, b: Int): Int {
return a + b
}Example 2 demonstrates a single‑expression function where the return type is inferred:
fun sum(a: Int, b: Int) = a + bExample 3 requires an explicit return type for a public function:
public fun sum(a: Int, b: Int): Int = a + bExample 4 returns a meaningless value similar to Java's void ( Unit in Kotlin). The Unit type can be omitted:
fun printSum(a: Int, b: Int): Unit {
print(a + b)
}
// or simply
public fun printSum(a: Int, b: Int) {
print(a + b)
}Using Nullable Types and Null Checks
When a reference or function return may be null , the type must be marked nullable with a trailing ? ; the non‑null assertion operator !! forces a non‑null view.
fun main(args: Array
) {
if (args.size < 2) {
print("Two integers expected")
return
}
val x = parseInt(args[0])
val y = parseInt(args[1])
// Must check because x or y may be null
if (x != null && y != null) {
// x and y are automatically cast to non‑null types here
print(x * y)
}
}
/**
* Returns null if the string cannot be converted to Int
*/
fun parseInt(str: String): Int? {
// implementation omitted
}Using the in Keyword
The in keyword checks whether a value lies within a range or collection:
// Print "OK" if x is between 1 and y‑1
if (x in 1..y-1) print("OK")
// Print "Out" if x is not in the array's index range
if (x !in 0..array.lastIndex) print("Out")
// Print numbers 1 to 5
for (x in 1..5) print(x)
// Iterate over a collection (similar to Java's enhanced for)
for (name in names) println(name)
// Print "yes" if the collection contains the object
if (text in names) print("yes")Type Checking and Automatic Casting
The is keyword works like Java's instanceof and automatically casts the variable after a successful check.
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// obj is automatically treated as String here
return obj.length
}
// !is can be used for the opposite check
return null
}
fun getStringLength(obj: Any): Int? {
// After the is‑check, obj can be used as String on the right side of &&
if (obj is String && obj.length > 0) {
return obj.length
}
return null
}When Expression
The when expression is Kotlin's version of switch :
fun cases(obj: Any) {
when (obj) {
1 -> print("第一项")
"hello" -> print("这个是字符串hello")
is Long -> print("这是一个Long类型数据")
!is String -> print("这不是String类型的数据")
else -> print("else类似于Java中的default")
}
}For deeper Kotlin learning, refer to the official Kotlin documentation or the Chinese translation project linked below.
KotlinDoc‑cn
Hujiang Technology
We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.