SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
Kotlin vs. Java
What Java has that Kotlin does not
What Java has that Kotlin does not
— Checked exceptions
— Primitive types that are not classes
— Static members
— Non-private fields
— Wildcard-types
Checked exceptions
Exception Classes
All exception classes in Kotlin are descendants of the class Throwable
throw MyException("Hi There!")
try {
// some code
}
catch (e: SomeException) {
// handler
}
finally {
// optional finally block
}
Try is an expression
val a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }
Checked Exceptions
Kotlin does not have checked exceptions
class StringBulder
Appendable append(CharSequence csq) throws IOException;
try {
log.append(message)
}
catch (IOException e) {
// Must be safe
}
The Nothing type
val s = person.name ?: throw IllegalArgumentException("Name required")
In your own code, you can use Nothing to mark a function that never returns:
fun fail(message: String): Nothing {
throw IllegalArgumentException(message)
}
val s = person.name ?: fail("Name required")
println(s) // 's' is known to be initialized at this point
val x = null // 'x' has type `Nothing?`
val l = listOf(null) // 'l' has type `List<Nothing?>
Primitive types that are not classes
Basic Types
In Kotlin, everything is an object in the sense that we can call member functions
and properties on any variable
Some of the types can have a special internal representation - for example,
numbers, characters and booleans can be represented as primitive values at
runtime - but to the user they look like ordinary classes
Numbers
— no implicit widening conversions
for numbers
— literals are slightly different in
some cases
characters are not numbers in Kotlin
Type Bit width
Double 64
Float 32
Long 64
Int 32
Short 16
Byte 8
Literal Constants
— Decimals: 123
– Longs L: 123L
— Hexadecimals: 0x0F
— Binaries: 0b00001011
Octal literals are not supported
— Doubles by default: 123.5,
123.5e10
— Floats are tagged by f or F:
123.5f
Underscores in numeric literals (since 1.1)
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_10010010
Representation
val a: Int = 10000
print(a === a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA === anotherBoxedA) // !!!Prints
'false'!!!
val a: Int = 10000
print(a == a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA == anotherBoxedA) // Prints
'true'
Explicit Conversions
// Hypothetical code, does not actually compile:
val a: Int? = 1 // A boxed Int (java.lang.Integer)
val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long)
print(a == b) // Surprise! This prints "false" as Long's equals() check for other part to be Long
as well
val b: Byte = 1 // OK, literals are checked statically
val i: Int = b // ERROR
val i: Int = b.toInt() // OK: explicitly widened
Number type supports the following conversions:
— toByte(): Byte
— toShort(): Short
— toInt(): Int
— toLong(): Long
— toFloat(): Float
— toDouble(): Double
— toChar(): Char
Characters
Characters are represented by the type Char
They can not be treated directly as numbers
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}
Character literals go in single quotes: '1'
Booleans
The type Boolean represents booleans, and has two values: true and false
Built-in operations on booleans include
— || – lazy disjunction
— && – lazy conjunction
— ! - negation
Arrays
Arrays in Kotlin are represented by
the Array class, that has get and
set functions (that turn into [] by
operator overloading conventions),
and size property, along with a few
other useful member functions:
class Array<T> private constructor() {
val size: Int
operator fun get(index: Int): T
operator fun set(index: Int, value: T): Unit
operator fun iterator(): Iterator<T>
// ...
}
Arrays
Note: unlike Java, arrays in Kotlin are
invariant. This means that Kotlin does
not let us assign an Array<String>
to an Array<Any>, which prevents a
possible runtime failure (but you can
use Array<out Any>
Kotlin also has specialized classes to
represent arrays of primitive types
without boxing overhead: ByteArray,
ShortArray, IntArray and so on.
Strings
Strings are represented by the type String
Strings are immutable
Elements of a string are characters that can be accessed by the indexing
operation: s[i]
A string can be iterated over with a for-loop:
for (c in str) {
println(c)
}
String Literals
val s = "Hello, world!n"
val text = """
for (c in "foo")
print(c)
"""
val text = """
|Tell me and I forget.
|Teach me and I remember.
|Involve me and I learn.
|(Benjamin Franklin)
""".trimMargin()
String Templates
val i = 10
val s = "i = $i" // evaluates to "i = 10"
val s = "abc"
val str = "$s.length is ${s.length}" // evaluates to "abc.length is 3"
val price = """
${'$'}9.99
"""
Static members
Companion Objects
In Kotlin, unlike Java or C#, classes do not have static methods
In most cases, it's recommended to simply use package-level functions instead
class MyClass {
companion object {
}
}
val x = MyClass.Companion
Object declarations
If you need to write a function that can be called without having a class instance
but needs access to the internals of a class (for example, a factory method), you
can write it as a member of an object declaration inside that class
object DataProviderManager {
fun registerDataProvider(provider: DataProvider) {
// ...
}
val allDataProviders: Collection<DataProvider>
get() = // ...
}
Non-private fields
Declaring Properties
Classes in Kotlin can have properties
These can be declared:
— as mutable, using the var
keyword
— read-only using the val keyword
class Address {
var name: String = ...
var street: String = ...
var state: String? = ...
var zip: String = ...
}
fun copyAddress(address: Address): Address {
val result = Address()
result.name = address.name
result.street = address.street
// ...
return result
}
Getters and Setters
var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
The initializer, getter and setter are optional
Property type is optional if it can be inferred from the initializer (or from the getter
return type, as shown below)
it starts with val instead of var
does not allow a setter
val simple: Int? // has type Int, default getter, must be initialized in constructor
val inferredType = 1 // has type Int and a default getter
val isEmpty: Boolean
get() = this.size == 0
var stringRepresentation: String
get() = this.toString()
set(value) {
setDataFromString(value) // parses the string and assigns values to other properties
}
Read-only property
Backing Fields
Classes in Kotlin cannot have fields
var counter = 0 // the initializer value is written directly to the backing field
set(value) {
if (value >= 0) field = value
}
The field identifier can only be used in the accessors of the property
For example, in the following case there will be no backing field:
val isEmpty: Boolean
get() = this.size == 0
Backing Properties
If you want to do something that does not fit into this "implicit backing field"
scheme, you can always fall back to having a backing property:
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null) {
_table = HashMap() // Type parameters are inferred
}
return _table ?: throw AssertionError("Set to null by another thread")
}
In all respects, this is just the same as in Java since access to private properties
with default getters and setters is optimized so that no function call overhead is
introduced
Compile-Time Constants
Properties the value of which is known at compile time can be marked as compile
time constants using the const modifier. Such properties need to fulfil the
following requirements:
— Top-level or member of an object
— Initialized with a value of type String or a primitive type
— No custom getter
Such properties can be used in annotations:
const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"
@Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
Late-Initialized Properties
To handle this case, you can mark
the property with the lateinit
modifier:
public class MyTest {
lateinit var subject: TestSubject
@SetUp fun setup() {
subject = TestSubject()
}
@Test fun test() {
subject.method() // dereference
directly
}
}
The modifier can only be used on var
properties declared inside the body of
a class (not in the primary
constructor), and only when the
property does not have a custom
getter or setter
The type of the property must be
non-null, and it must not be a
primitive type
Wildcard-types
Generics
class Box<T>(t: T) {
var value = t
}
To create an instance of such a class, we need to provide the type arguments:
val box: Box<Int> = Box<Int>(1)
Variance
One of the most tricky parts of Java's type system is wildcard types
And Kotlin doesn't have any
Instead, it has two other things:
● declaration-site variance
● type projections
Declaration-site variance (variance annotation out)
interface Source<T> {
T nextT();
}
void demo(Source<String> strs) {
Source<Object> objects = strs; // !!! Not
allowed in Java
// ...
}
Source<? extends Object>
abstract class Source<out T> {
abstract fun nextT(): T
}
fun demo(strs: Source<String>) {
val objects: Source<Any> = strs // This
is OK, since T is an out-parameter
// ...
}
Declaration-site variance (variance annotation in)
abstract class Comparable<in T> {
abstract fun compareTo(other: T): Int
}
fun demo(x: Comparable<Number>) {
x.compareTo(1.0) // 1.0 has type Double, which is a subtype of Number
// Thus, we can assign x to a variable of type Comparable<Double>
val y: Comparable<Double> = x // OK!
}
Use-site variance: Type projections
class Array<T>(val size: Int) {
fun get(index: Int): T { /* ... */ }
fun set(index: Int, value: T) { /* ... */ }
}
fun copy(from: Array<Any>, to: Array<Any>) {
assert(from.size == to.size)
for (i in from.indices)
to[i] = from[i]
}
val ints: Array<Int> = arrayOf(1, 2, 3)
val any = Array<Any>(3) { "" }
copy(ints, any) // Error: expects (Array<Any>,
Array<Any>).
fun copy(from: Array<out Any>, to: Array<Any>) {
// ...
}
Generic functions
Not only classes can have type parameters. Functions can, too:
fun <T> singletonList(item: T): List<T> {
// ...
}
fun <T> T.basicToString() : String { // extension function
// ...
}
To call a generic function, specify the type arguments at the call site after the
name of the function:
val l = singletonList<Int>(1)

Weitere ähnliche Inhalte

Was ist angesagt?

Orthogonal Functional Architecture
Orthogonal Functional ArchitectureOrthogonal Functional Architecture
Orthogonal Functional Architecture
John De Goes
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
Yandex
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 

Was ist angesagt? (20)

DIWE - Advanced PHP Concepts
DIWE - Advanced PHP ConceptsDIWE - Advanced PHP Concepts
DIWE - Advanced PHP Concepts
 
Python for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comPython for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.com
 
Orthogonal Functional Architecture
Orthogonal Functional ArchitectureOrthogonal Functional Architecture
Orthogonal Functional Architecture
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
C# programming
C# programming C# programming
C# programming
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Python The basics
Python   The basicsPython   The basics
Python The basics
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
O caml2014 leroy-slides
O caml2014 leroy-slidesO caml2014 leroy-slides
O caml2014 leroy-slides
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!
 
All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!All Aboard The Scala-to-PureScript Express!
All Aboard The Scala-to-PureScript Express!
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 

Ähnlich wie 2 kotlin vs. java: what java has that kotlin does not

ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 

Ähnlich wie 2 kotlin vs. java: what java has that kotlin does not (20)

Kotlin cheat sheet by ekito
Kotlin cheat sheet by ekitoKotlin cheat sheet by ekito
Kotlin cheat sheet by ekito
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Scala
ScalaScala
Scala
 
The Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaThe Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana Isakova
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»
 
Scala fundamentals
Scala fundamentalsScala fundamentals
Scala fundamentals
 
Kotlin for Android devs
Kotlin for Android devsKotlin for Android devs
Kotlin for Android devs
 
Kotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrainsKotlin: A pragmatic language by JetBrains
Kotlin: A pragmatic language by JetBrains
 
Railroading into Scala
Railroading into ScalaRailroading into Scala
Railroading into Scala
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to Kotlin
 
Kotlin- Basic to Advance
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
K is for Kotlin
K is for KotlinK is for Kotlin
K is for Kotlin
 
KotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptxKotlinForJavaDevelopers-UJUG.pptx
KotlinForJavaDevelopers-UJUG.pptx
 
Боремся с NPE вместе с Kotlin, Павел Шацких СберТех
Боремся с NPE вместе с Kotlin, Павел Шацких СберТехБоремся с NPE вместе с Kotlin, Павел Шацких СберТех
Боремся с NPE вместе с Kotlin, Павел Шацких СберТех
 

Kürzlich hochgeladen

CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
anilsa9823
 

Kürzlich hochgeladen (7)

9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 

2 kotlin vs. java: what java has that kotlin does not

  • 1. Kotlin vs. Java What Java has that Kotlin does not
  • 2. What Java has that Kotlin does not — Checked exceptions — Primitive types that are not classes — Static members — Non-private fields — Wildcard-types
  • 4. Exception Classes All exception classes in Kotlin are descendants of the class Throwable throw MyException("Hi There!") try { // some code } catch (e: SomeException) { // handler } finally { // optional finally block }
  • 5. Try is an expression val a: Int? = try { parseInt(input) } catch (e: NumberFormatException) { null }
  • 6. Checked Exceptions Kotlin does not have checked exceptions class StringBulder Appendable append(CharSequence csq) throws IOException; try { log.append(message) } catch (IOException e) { // Must be safe }
  • 7. The Nothing type val s = person.name ?: throw IllegalArgumentException("Name required") In your own code, you can use Nothing to mark a function that never returns: fun fail(message: String): Nothing { throw IllegalArgumentException(message) } val s = person.name ?: fail("Name required") println(s) // 's' is known to be initialized at this point val x = null // 'x' has type `Nothing?` val l = listOf(null) // 'l' has type `List<Nothing?>
  • 8. Primitive types that are not classes
  • 9. Basic Types In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable Some of the types can have a special internal representation - for example, numbers, characters and booleans can be represented as primitive values at runtime - but to the user they look like ordinary classes
  • 10. Numbers — no implicit widening conversions for numbers — literals are slightly different in some cases characters are not numbers in Kotlin Type Bit width Double 64 Float 32 Long 64 Int 32 Short 16 Byte 8
  • 11. Literal Constants — Decimals: 123 – Longs L: 123L — Hexadecimals: 0x0F — Binaries: 0b00001011 Octal literals are not supported — Doubles by default: 123.5, 123.5e10 — Floats are tagged by f or F: 123.5f
  • 12. Underscores in numeric literals (since 1.1) val oneMillion = 1_000_000 val creditCardNumber = 1234_5678_9012_3456L val socialSecurityNumber = 999_99_9999L val hexBytes = 0xFF_EC_DE_5E val bytes = 0b11010010_01101001_10010100_10010010
  • 13. Representation val a: Int = 10000 print(a === a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!Prints 'false'!!! val a: Int = 10000 print(a == a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA == anotherBoxedA) // Prints 'true'
  • 14. Explicit Conversions // Hypothetical code, does not actually compile: val a: Int? = 1 // A boxed Int (java.lang.Integer) val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long) print(a == b) // Surprise! This prints "false" as Long's equals() check for other part to be Long as well val b: Byte = 1 // OK, literals are checked statically val i: Int = b // ERROR val i: Int = b.toInt() // OK: explicitly widened
  • 15. Number type supports the following conversions: — toByte(): Byte — toShort(): Short — toInt(): Int — toLong(): Long — toFloat(): Float — toDouble(): Double — toChar(): Char
  • 16. Characters Characters are represented by the type Char They can not be treated directly as numbers fun check(c: Char) { if (c == 1) { // ERROR: incompatible types // ... } } Character literals go in single quotes: '1'
  • 17. Booleans The type Boolean represents booleans, and has two values: true and false Built-in operations on booleans include — || – lazy disjunction — && – lazy conjunction — ! - negation
  • 18. Arrays Arrays in Kotlin are represented by the Array class, that has get and set functions (that turn into [] by operator overloading conventions), and size property, along with a few other useful member functions: class Array<T> private constructor() { val size: Int operator fun get(index: Int): T operator fun set(index: Int, value: T): Unit operator fun iterator(): Iterator<T> // ... }
  • 19. Arrays Note: unlike Java, arrays in Kotlin are invariant. This means that Kotlin does not let us assign an Array<String> to an Array<Any>, which prevents a possible runtime failure (but you can use Array<out Any> Kotlin also has specialized classes to represent arrays of primitive types without boxing overhead: ByteArray, ShortArray, IntArray and so on.
  • 20. Strings Strings are represented by the type String Strings are immutable Elements of a string are characters that can be accessed by the indexing operation: s[i] A string can be iterated over with a for-loop: for (c in str) { println(c) }
  • 21. String Literals val s = "Hello, world!n" val text = """ for (c in "foo") print(c) """ val text = """ |Tell me and I forget. |Teach me and I remember. |Involve me and I learn. |(Benjamin Franklin) """.trimMargin()
  • 22. String Templates val i = 10 val s = "i = $i" // evaluates to "i = 10" val s = "abc" val str = "$s.length is ${s.length}" // evaluates to "abc.length is 3" val price = """ ${'$'}9.99 """
  • 24. Companion Objects In Kotlin, unlike Java or C#, classes do not have static methods In most cases, it's recommended to simply use package-level functions instead class MyClass { companion object { } } val x = MyClass.Companion
  • 25. Object declarations If you need to write a function that can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class object DataProviderManager { fun registerDataProvider(provider: DataProvider) { // ... } val allDataProviders: Collection<DataProvider> get() = // ... }
  • 27. Declaring Properties Classes in Kotlin can have properties These can be declared: — as mutable, using the var keyword — read-only using the val keyword class Address { var name: String = ... var street: String = ... var state: String? = ... var zip: String = ... } fun copyAddress(address: Address): Address { val result = Address() result.name = address.name result.street = address.street // ... return result }
  • 28. Getters and Setters var <propertyName>[: <PropertyType>] [= <property_initializer>] [<getter>] [<setter>] The initializer, getter and setter are optional Property type is optional if it can be inferred from the initializer (or from the getter return type, as shown below)
  • 29. it starts with val instead of var does not allow a setter val simple: Int? // has type Int, default getter, must be initialized in constructor val inferredType = 1 // has type Int and a default getter val isEmpty: Boolean get() = this.size == 0 var stringRepresentation: String get() = this.toString() set(value) { setDataFromString(value) // parses the string and assigns values to other properties } Read-only property
  • 30. Backing Fields Classes in Kotlin cannot have fields var counter = 0 // the initializer value is written directly to the backing field set(value) { if (value >= 0) field = value } The field identifier can only be used in the accessors of the property For example, in the following case there will be no backing field: val isEmpty: Boolean get() = this.size == 0
  • 31. Backing Properties If you want to do something that does not fit into this "implicit backing field" scheme, you can always fall back to having a backing property: private var _table: Map<String, Int>? = null public val table: Map<String, Int> get() { if (_table == null) { _table = HashMap() // Type parameters are inferred } return _table ?: throw AssertionError("Set to null by another thread") } In all respects, this is just the same as in Java since access to private properties with default getters and setters is optimized so that no function call overhead is introduced
  • 32. Compile-Time Constants Properties the value of which is known at compile time can be marked as compile time constants using the const modifier. Such properties need to fulfil the following requirements: — Top-level or member of an object — Initialized with a value of type String or a primitive type — No custom getter Such properties can be used in annotations: const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated" @Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
  • 33. Late-Initialized Properties To handle this case, you can mark the property with the lateinit modifier: public class MyTest { lateinit var subject: TestSubject @SetUp fun setup() { subject = TestSubject() } @Test fun test() { subject.method() // dereference directly } } The modifier can only be used on var properties declared inside the body of a class (not in the primary constructor), and only when the property does not have a custom getter or setter The type of the property must be non-null, and it must not be a primitive type
  • 35. Generics class Box<T>(t: T) { var value = t } To create an instance of such a class, we need to provide the type arguments: val box: Box<Int> = Box<Int>(1)
  • 36. Variance One of the most tricky parts of Java's type system is wildcard types And Kotlin doesn't have any Instead, it has two other things: ● declaration-site variance ● type projections
  • 37. Declaration-site variance (variance annotation out) interface Source<T> { T nextT(); } void demo(Source<String> strs) { Source<Object> objects = strs; // !!! Not allowed in Java // ... } Source<? extends Object> abstract class Source<out T> { abstract fun nextT(): T } fun demo(strs: Source<String>) { val objects: Source<Any> = strs // This is OK, since T is an out-parameter // ... }
  • 38. Declaration-site variance (variance annotation in) abstract class Comparable<in T> { abstract fun compareTo(other: T): Int } fun demo(x: Comparable<Number>) { x.compareTo(1.0) // 1.0 has type Double, which is a subtype of Number // Thus, we can assign x to a variable of type Comparable<Double> val y: Comparable<Double> = x // OK! }
  • 39. Use-site variance: Type projections class Array<T>(val size: Int) { fun get(index: Int): T { /* ... */ } fun set(index: Int, value: T) { /* ... */ } } fun copy(from: Array<Any>, to: Array<Any>) { assert(from.size == to.size) for (i in from.indices) to[i] = from[i] } val ints: Array<Int> = arrayOf(1, 2, 3) val any = Array<Any>(3) { "" } copy(ints, any) // Error: expects (Array<Any>, Array<Any>). fun copy(from: Array<out Any>, to: Array<Any>) { // ... }
  • 40. Generic functions Not only classes can have type parameters. Functions can, too: fun <T> singletonList(item: T): List<T> { // ... } fun <T> T.basicToString() : String { // extension function // ... } To call a generic function, specify the type arguments at the call site after the name of the function: val l = singletonList<Int>(1)