6 Variables and properties
In Kotlin,
everything is an object. We don’t find primitive types as the ones we can use in Java.
That’s really helpful, because we have an homogeneous way to deal with all the available types.
6.1 Basic types
Of course, basic types such as integers, floats, characters or booleans still exist, but they all act as
an object. The name of the basic types and the way they work are very similar to Java, but there are
some differences you might take into account:
• There are no automatic conversions among numeric types. For instance, you cannot assign
an
Int
to a
Double
variable. An explicit conversion must be done, using one of the many
functions available:
1
val i: Int = 7
2
val d: Double = i.toDouble()
• Characters (
Char
) cannot directly be used as numbers. We can, however, convert them to a
number when we need it:
1
val c: Char = 'c'
2
val i: Int = c.toInt()
• Bitwise arithmetical operations are a bit different. In Android, we use bitwise
or
quite often
for flags, so I’ll stick to “
and
” and “
or
“ as an example:
1
// Java
2
Do'stlaringiz bilan baham: