Kotlin Types for Java Developers
Chandra Sekar
@iChanSek
Type System in Java
Type System in Java
• Primitive Types
Type System in Java
• Primitive Types
• Class Types
Primitive Types
Primitive Types - Java
Primitive Types - Java
• long
Primitive Types - Java
• long
• int
Primitive Types - Java
• long
• int
• short
Primitive Types - Java
• long
• int
• short
• byte
Primitive Types - Java
• long
• int
• short
• byte
• char
Primitive Types - Java
• long
• int
• short
• byte
• char
• double
Primitive Types - Java
• long
• int
• short
• byte
• char
• double
• float
Primitive Types - Java
• long
• int
• short
• byte
• char
• double
• float
• boolean
Primitive Types - Kotlin
Primitive Types - Kotlin
• Long
Primitive Types - Kotlin
• Long
• Int
Primitive Types - Kotlin
• Long
• Int
• Short
Primitive Types - Kotlin
• Long
• Int
• Short
• Byte
Primitive Types - Kotlin
• Long
• Int
• Short
• Byte
• Char
Primitive Types - Kotlin
• Long
• Int
• Short
• Byte
• Char
• Double
Primitive Types - Kotlin
• Long
• Int
• Short
• Byte
• Char
• Double
• Float
Primitive Types - Kotlin
• Long
• Int
• Short
• Byte
• Char
• Double
• Float
• Boolean
Primitive Types - Comparison
Primitive Types - Comparison
Java Kotlin
Primitive Types - Comparison
Java Kotlin
long = Long
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
byte = Byte
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
byte = Byte
char = Char
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
byte = Byte
char = Char
double = Double
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
byte = Byte
char = Char
double = Double
float = Float
Primitive Types - Comparison
Java Kotlin
long = Long
int = Int
short = Short
byte = Byte
char = Char
double = Double
float = Float
boolean = Boolean
Wrapper Types - Java
Wrapper Types - Java
• Long
Wrapper Types - Java
• Long
• Integer
Wrapper Types - Java
• Long
• Integer
• Short
Wrapper Types - Java
• Long
• Integer
• Short
• Byte
Wrapper Types - Java
• Long
• Integer
• Short
• Byte
• Character
Wrapper Types - Java
• Long
• Integer
• Short
• Byte
• Character
• Double
Wrapper Types - Java
• Long
• Integer
• Short
• Byte
• Character
• Double
• Float
Wrapper Types - Java
• Long
• Integer
• Short
• Byte
• Character
• Double
• Float
• Boolean
Primitive Types - Comparison
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
byte = Byte ~ Byte
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
byte = Byte ~ Byte
char = Char ~ Character
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
byte = Byte ~ Byte
char = Char ~ Character
double = Double ~ Double
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
byte = Byte ~ Byte
char = Char ~ Character
double = Double ~ Double
float = Float ~ Float
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long
int = Int ~ Integer
short = Short ~ Short
byte = Byte ~ Byte
char = Char ~ Character
double = Double ~ Double
float = Float ~ Float
boolean = Boolean ~ Boolean
Example - Primitive Type
Example - Primitive Type
int x = 10; // Java - Primitive int
Example - Primitive Type
int x = 10; // Java - Primitive int
// is Same as
val x: Int = 10 // Kotlin - Primitive int
Example - Primitive Type
int x = 10; // Java - Primitive int
// is Same as
val x: Int = 10 // Kotlin - Primitive int
// Even more simpler way
val x = 10 // Kotlin - Type inferred as Int
Example - Wrapper Type
Example - Wrapper Type
Integer x = new Integer(10); // Java - Wrapper Type
Integer y = 10; // Java - Wrapper Type (Auto Boxing)
Example - Wrapper Type
Integer x = new Integer(10); // Java - Wrapper Type
Integer y = 10; // Java - Wrapper Type (Auto Boxing)
// Lets assume they are same as
val x: Int = 10
val y = 10
Example - Wrapper Type
Integer x = new Integer(10); // Java - Wrapper Type
Integer y = 10; // Java - Wrapper Type (Auto Boxing)
// Lets assume they are same as
val x: Int = 10
val y = 10
// then, how to represent this in Kotlin?
Integer z = null;
Nullable Types
Nullable Types
Nullable Types
• Kotlin has separate type to handle null.
Nullable Types
• Kotlin has separate type to handle null.
• By default all types are non nullable.
Nullable Types
• Kotlin has separate type to handle null.
• By default all types are non nullable.
• If you want to assign null, then you have to
declare them as nullable.
Nullable Types
• Kotlin has separate type to handle null.
• By default all types are non nullable.
• If you want to assign null, then you have to
declare them as nullable.
• Syntax - postfix corresponding type with ‘?’
Nullable Types
• Kotlin has separate type to handle null.
• By default all types are non nullable.
• If you want to assign null, then you have to
declare them as nullable.
• Syntax - postfix corresponding type with ‘?’
• Example - Int?, String?, User?
Back to Previous Example
Back to Previous Example
// then, how to represent this in Kotlin?
Integer z = null;
Back to Previous Example
// then, how to represent this in Kotlin?
Integer z = null;
// And, the answer is
val z: Int? = null
Primitive Types - Comparison
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
byte = Byte ~ Byte? = Byte
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
byte = Byte ~ Byte? = Byte
char = Char ~ Char? = Character
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
byte = Byte ~ Byte? = Byte
char = Char ~ Char? = Character
double = Double ~ Double? = Double
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
byte = Byte ~ Byte? = Byte
char = Char ~ Char? = Character
double = Double ~ Double? = Double
float = Float ~ Float? = Float
Primitive Types - Comparison
Java Kotlin Java(Wrappers)
long = Long ~ Long? = Long
int = Int ~ Int? = Integer
short = Short ~ Short? = Short
byte = Byte ~ Byte? = Byte
char = Char ~ Char? = Character
double = Double ~ Double? = Double
float = Float ~ Float? = Float
boolean = Boolean ~ Boolean? = Boolean
Take Away - Primitive Types
Take Away - Primitive Types
• Kotlin doesn’t have Primitive types explicitly.
Take Away - Primitive Types
• Kotlin doesn’t have Primitive types explicitly.
• Kotlin compiler chooses Primitive type when
the type is mentioned as non nullable.
Take Away - Primitive Types
• Kotlin doesn’t have Primitive types explicitly.
• Kotlin compiler chooses Primitive type when
the type is mentioned as non nullable.
• If the type is mentioned as nullable, Kotlin
compiler treats it as a wrapper type.
Class Types
Class Types - Java
Class Types - Java
• class String
Class Types - Java
• class String
• interface CharSequence
Class Types - Java
• class String
• interface CharSequence
• enum Color
Class Types - Java
• class String
• interface CharSequence
• enum Color
• class User
Class Types - Java
• class String
• interface CharSequence
• enum Color
• class User
Class Types - Java
• class String
• interface CharSequence
• enum Color
• class User
Class Types - Java
• class String
• interface CharSequence
• enum Color
• class User
Object is common to all.
Supermost Type
Supermost Type
Object (in Java)
Supermost Type
Object (in Java)
is Same as
Supermost Type
Object (in Java)
is Same as
Any (in Kotlin)
Type Hierarchy - Java
Type Hierarchy - Java
Object
Type Hierarchy - Java
Object
String
Type Hierarchy - Java
Object
Integer String
Type Hierarchy - Java
Object
Integer String User
Type Hierarchy - Kotlin
Type Hierarchy - Kotlin
Any
Type Hierarchy - Kotlin
Any
String
Type Hierarchy - Kotlin
Any
Int String
Type Hierarchy - Kotlin
Any
Int String User
Examples
Examples
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
Examples
val any = Any()
val user: Any = User()
val str: Any = "Test"
val int: Any = 10
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
Examples
val any = Any()
val user: Any = User()
val str: Any = "Test"
val int: Any = 10
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
User user = new User();
String str = "Test";
Integer i = 10;
Examples
val any = Any()
val user: Any = User()
val str: Any = "Test"
val int: Any = 10
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
User user = new User();
String str = "Test";
Integer i = 10;
val user: User = User()
val str: String = "Test"
val i: Int = 10
Examples
val any = Any()
val user: Any = User()
val str: Any = "Test"
val int: Any = 10
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
User user = new User();
String str = "Test";
Integer i = 10;
val user = User()
val str = "Test"
val i = 10
val user: User = User()
val str: String = "Test"
val i: Int = 10
Examples
val any = Any()
val user: Any = User()
val str: Any = "Test"
val int: Any = 10
Object obj = new Object();
Object user = new User();
Object str = "Test";
Object integer = 10;
User user = new User();
String str = "Test";
Integer i = 10;
val user = User()
val str = "Test"
val i = 10
val user: User = User()
val str: String = "Test"
val i: Int = 10
Type Inferrence
Examples (with null)
Examples (with null)
Object obj = null;
User user = null;
String str = null;
Integer i = null;
Examples (with null)
Object obj = null;
User user = null;
String str = null;
Integer i = null;
val any: Any = null
val user: User = null
val str: String = null
val i: Int = null
Examples (with null)
Object obj = null;
User user = null;
String str = null;
Integer i = null;
Examples (with null)
Object obj = null;
User user = null;
String str = null;
Integer i = null;
val any: Any? = null
val user: User? = null
val str: String? = null
val i: Int? = null
Type Hierarchy (Final) - Java
Type Hierarchy (Final) - Java
Object
Type Hierarchy (Final) - Java
Object
String
Type Hierarchy (Final) - Java
Object
Integer String
Type Hierarchy (Final) - Java
Object
Integer String User
Type Hierarchy (Final) - Java
Object
Integer String User
null
Type Hierarchy - Kotlin
Type Hierarchy - Kotlin
Any
Type Hierarchy - Kotlin
Any
String
Type Hierarchy - Kotlin
Any
Int String
Type Hierarchy - Kotlin
Any
Int String User
Type Hierarchy - Kotlin
Any
Int String User
null
Type Hierarchy - Kotlin
Any
Int String User
null then what???
Nothing Type
Nothing Type
Nothing Type
• A special type that exists but doesn’t represent
any value.
Nothing Type
• A special type that exists but doesn’t represent
any value.
• Should be used to mark any code that can
never be reached.
Nothing Type
• A special type that exists but doesn’t represent
any value.
• Should be used to mark any code that can
never be reached.
• We can also use it while type inference.
Examples - Nothing Type
Examples - Nothing Type
val s = person.name ?: throw IllegalArgumentException("Name required")
println(s) // 's' is known to be initialized at this point
Examples - Nothing Type
val s = person.name ?: throw IllegalArgumentException("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?>
var user: User? = x
Type Hierarchy - Kotlin
Type Hierarchy - Kotlin
Any
Type Hierarchy - Kotlin
Any
String
Type Hierarchy - Kotlin
Any
Int String
Type Hierarchy - Kotlin
Any
Int String User
Type Hierarchy - Kotlin
Any
Int String User
Nothing
Type Hierarchy - with null - Kotlin
Type Hierarchy - with null - Kotlin
Any?
Any
Type Hierarchy - with null - Kotlin
Any?
Any
String?
String
Type Hierarchy - with null - Kotlin
Any?
Any
String?
String
Any?
String?
Type Hierarchy - with null - Kotlin
Any?
Any
String?
String
Any?
String?
Any?
String
Type Hierarchy (Final) - Kotlin
Type Hierarchy (Final) - Kotlin
Any
Type Hierarchy (Final) - Kotlin
Any
String
Type Hierarchy (Final) - Kotlin
Any
StringInt
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
String?
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
String?Int?
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
String?Int? User?
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
String?Int? User?
Nothing?
Type Hierarchy (Final) - Kotlin
Any
StringInt User
Nothing
Any?
String?Int? User?
Nothing?
void == Unit
Examples - void and Unit
Examples - void and Unit
private void test() {}
private void test() {
return;
}
Examples - void and Unit
private void test() {}
private void test() {
return;
}
private fun test(): Unit {}
private fun test(): Unit {
return Unit
}
private fun test() {}
private fun test(): Unit {}
Array Type
Array Type
Array Type
• Kotlin has no [] syntax for creating Arrays.
Array Type
• Kotlin has no [] syntax for creating Arrays.
• Instead it has a special classs called Array<T>
Examples - Array Type
Examples - Array Type
String[] names = new String[10];
String[] colors = new String[] {"Red", "Green", "Blue"};
Examples - Array Type
String[] names = new String[10];
String[] colors = new String[] {"Red", "Green", "Blue"};
val names: Array<String> = emptyArray()
val colors: Array<String> = arrayOf("Red", "Green", "Blue")
val nulls: Array<String?> = arrayOfNulls(10)
Examples - Array of Primitive Type
Examples - Array of Primitive Type
int[] numbers = new int[10];
int[] nums = new int[] {1, 2, 3, 4, 5};
Examples - Array of Primitive Type
int[] numbers = new int[10];
int[] nums = new int[] {1, 2, 3, 4, 5};
val numbers: Array<Int> = emptyArray()
val nums: Array<Int> = arrayOf(2, 3, 4)
val nulls: Array<Int?> = arrayOfNulls(10)
Examples - Array of Primitive Type
int[] numbers = new int[10];
int[] nums = new int[] {1, 2, 3, 4, 5};
val numbers: Array<Int> = emptyArray()
val nums: Array<Int> = arrayOf(2, 3, 4)
val nulls: Array<Int?> = arrayOfNulls(10)
} Integer[]
Examples - Array of Primitive Type
int[] numbers = new int[10];
int[] nums = new int[] {1, 2, 3, 4, 5};
val numbers: Array<Int> = emptyArray()
val nums: Array<Int> = arrayOf(2, 3, 4)
val nulls: Array<Int?> = arrayOfNulls(10)
val nums: IntArray = intArrayOf(1, 2, 3)
val longs: LongArray = longArrayOf(1L, 2L, 3L)
} Integer[]
Examples - Array of Primitive Type
int[] numbers = new int[10];
int[] nums = new int[] {1, 2, 3, 4, 5};
val numbers: Array<Int> = emptyArray()
val nums: Array<Int> = arrayOf(2, 3, 4)
val nulls: Array<Int?> = arrayOfNulls(10)
val nums: IntArray = intArrayOf(1, 2, 3)
val longs: LongArray = longArrayOf(1L, 2L, 3L)
} Integer[]
} int[]
Nullable Types in Java
Nullable Types - Interoperability
Nullable Types - Interoperability
Since Java doesn’t have nullable types, we need
to take extra care of our code while taking
adventage of interoperability.
Nullable Types - Interoperability
Nullable Types - Interoperability
// Test.java
public class Test {
public String test() {
return null;
}
}
Nullable Types - Interoperability
// Test.java
public class Test {
public String test() {
return null;
}
}
// TestKotlin.kt
class Test {
private test() {
val test = Test()
test.test().toString()
}
}
Nullable Types - Interoperability
// Test.java
public class Test {
public String test() {
return null;
}
}
// TestKotlin.kt
class Test {
private test() {
val test = Test()
test.test().toString()
}
}
This will lead to NPE
How to avoid such NPE?
How to avoid such NPE?
// Test.java
public class Test {
@Nullable
public String test() {
return null;
}
}
How to avoid such NPE?
// TestKotlin.kt
class Test {
private test() {
val test = Test()
test.test().toString()
}
}
// Test.java
public class Test {
@Nullable
public String test() {
return null;
}
}
How to avoid such NPE?
// TestKotlin.kt
class Test {
private test() {
val test = Test()
test.test().toString()
}
}
Now no NPE as it fails at Compile Time
// Test.java
public class Test {
@Nullable
public String test() {
return null;
}
}
Take Away
Take Away
• Kotlin has no primitive and wrapper type
speartely.
Take Away
• Kotlin has no primitive and wrapper type
speartely.
• Nullable and Non-Nullable values are two
separate types.
Take Away
• Kotlin has no primitive and wrapper type
speartely.
• Nullable and Non-Nullable values are two
separate types.
• By default all types are Non-Nullable.
Take Away
• Kotlin has no primitive and wrapper type
speartely.
• Nullable and Non-Nullable values are two
separate types.
• By default all types are Non-Nullable.
• Type and Type? are not same.
Take Away - Continued…
Take Away - Continued…
• Object in Java is same as Any in Kotlin.
Take Away - Continued…
• Object in Java is same as Any in Kotlin.
• void in Java is same as Unit in Kotlin.
Take Away - Continued…
• Object in Java is same as Any in Kotlin.
• void in Java is same as Unit in Kotlin.
• Nothing is not same as Unit or void.
Take Away - Continued…
• Object in Java is same as Any in Kotlin.
• void in Java is same as Unit in Kotlin.
• Nothing is not same as Unit or void.
• Nothing is converted into void in byte code
level as JVM doesn’t has it.
Take Away - Continued…
• Object in Java is same as Any in Kotlin.
• void in Java is same as Unit in Kotlin.
• Nothing is not same as Unit or void.
• Nothing is converted into void in byte code
level as JVM doesn’t has it.
• Always annotate your Java types to avoid NPE
in Kotlin world.
Thank You
Chandra Sekhar
@iChanSek

Kotlin Types for Java Developers

  • 1.
    Kotlin Types forJava Developers Chandra Sekar @iChanSek
  • 2.
  • 3.
    Type System inJava • Primitive Types
  • 4.
    Type System inJava • Primitive Types • Class Types
  • 5.
  • 6.
  • 7.
    Primitive Types -Java • long
  • 8.
    Primitive Types -Java • long • int
  • 9.
    Primitive Types -Java • long • int • short
  • 10.
    Primitive Types -Java • long • int • short • byte
  • 11.
    Primitive Types -Java • long • int • short • byte • char
  • 12.
    Primitive Types -Java • long • int • short • byte • char • double
  • 13.
    Primitive Types -Java • long • int • short • byte • char • double • float
  • 14.
    Primitive Types -Java • long • int • short • byte • char • double • float • boolean
  • 15.
  • 16.
    Primitive Types -Kotlin • Long
  • 17.
    Primitive Types -Kotlin • Long • Int
  • 18.
    Primitive Types -Kotlin • Long • Int • Short
  • 19.
    Primitive Types -Kotlin • Long • Int • Short • Byte
  • 20.
    Primitive Types -Kotlin • Long • Int • Short • Byte • Char
  • 21.
    Primitive Types -Kotlin • Long • Int • Short • Byte • Char • Double
  • 22.
    Primitive Types -Kotlin • Long • Int • Short • Byte • Char • Double • Float
  • 23.
    Primitive Types -Kotlin • Long • Int • Short • Byte • Char • Double • Float • Boolean
  • 24.
  • 25.
    Primitive Types -Comparison Java Kotlin
  • 26.
    Primitive Types -Comparison Java Kotlin long = Long
  • 27.
    Primitive Types -Comparison Java Kotlin long = Long int = Int
  • 28.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short
  • 29.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short byte = Byte
  • 30.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short byte = Byte char = Char
  • 31.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short byte = Byte char = Char double = Double
  • 32.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short byte = Byte char = Char double = Double float = Float
  • 33.
    Primitive Types -Comparison Java Kotlin long = Long int = Int short = Short byte = Byte char = Char double = Double float = Float boolean = Boolean
  • 34.
  • 35.
    Wrapper Types -Java • Long
  • 36.
    Wrapper Types -Java • Long • Integer
  • 37.
    Wrapper Types -Java • Long • Integer • Short
  • 38.
    Wrapper Types -Java • Long • Integer • Short • Byte
  • 39.
    Wrapper Types -Java • Long • Integer • Short • Byte • Character
  • 40.
    Wrapper Types -Java • Long • Integer • Short • Byte • Character • Double
  • 41.
    Wrapper Types -Java • Long • Integer • Short • Byte • Character • Double • Float
  • 42.
    Wrapper Types -Java • Long • Integer • Short • Byte • Character • Double • Float • Boolean
  • 43.
  • 44.
    Primitive Types -Comparison Java Kotlin Java(Wrappers)
  • 45.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long
  • 46.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer
  • 47.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short
  • 48.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short byte = Byte ~ Byte
  • 49.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short byte = Byte ~ Byte char = Char ~ Character
  • 50.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short byte = Byte ~ Byte char = Char ~ Character double = Double ~ Double
  • 51.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short byte = Byte ~ Byte char = Char ~ Character double = Double ~ Double float = Float ~ Float
  • 52.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long int = Int ~ Integer short = Short ~ Short byte = Byte ~ Byte char = Char ~ Character double = Double ~ Double float = Float ~ Float boolean = Boolean ~ Boolean
  • 53.
  • 54.
    Example - PrimitiveType int x = 10; // Java - Primitive int
  • 55.
    Example - PrimitiveType int x = 10; // Java - Primitive int // is Same as val x: Int = 10 // Kotlin - Primitive int
  • 56.
    Example - PrimitiveType int x = 10; // Java - Primitive int // is Same as val x: Int = 10 // Kotlin - Primitive int // Even more simpler way val x = 10 // Kotlin - Type inferred as Int
  • 57.
  • 58.
    Example - WrapperType Integer x = new Integer(10); // Java - Wrapper Type Integer y = 10; // Java - Wrapper Type (Auto Boxing)
  • 59.
    Example - WrapperType Integer x = new Integer(10); // Java - Wrapper Type Integer y = 10; // Java - Wrapper Type (Auto Boxing) // Lets assume they are same as val x: Int = 10 val y = 10
  • 60.
    Example - WrapperType Integer x = new Integer(10); // Java - Wrapper Type Integer y = 10; // Java - Wrapper Type (Auto Boxing) // Lets assume they are same as val x: Int = 10 val y = 10 // then, how to represent this in Kotlin? Integer z = null;
  • 61.
  • 62.
  • 63.
    Nullable Types • Kotlinhas separate type to handle null.
  • 64.
    Nullable Types • Kotlinhas separate type to handle null. • By default all types are non nullable.
  • 65.
    Nullable Types • Kotlinhas separate type to handle null. • By default all types are non nullable. • If you want to assign null, then you have to declare them as nullable.
  • 66.
    Nullable Types • Kotlinhas separate type to handle null. • By default all types are non nullable. • If you want to assign null, then you have to declare them as nullable. • Syntax - postfix corresponding type with ‘?’
  • 67.
    Nullable Types • Kotlinhas separate type to handle null. • By default all types are non nullable. • If you want to assign null, then you have to declare them as nullable. • Syntax - postfix corresponding type with ‘?’ • Example - Int?, String?, User?
  • 68.
  • 69.
    Back to PreviousExample // then, how to represent this in Kotlin? Integer z = null;
  • 70.
    Back to PreviousExample // then, how to represent this in Kotlin? Integer z = null; // And, the answer is val z: Int? = null
  • 71.
  • 72.
    Primitive Types -Comparison Java Kotlin Java(Wrappers)
  • 73.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long
  • 74.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer
  • 75.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short
  • 76.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short byte = Byte ~ Byte? = Byte
  • 77.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short byte = Byte ~ Byte? = Byte char = Char ~ Char? = Character
  • 78.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short byte = Byte ~ Byte? = Byte char = Char ~ Char? = Character double = Double ~ Double? = Double
  • 79.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short byte = Byte ~ Byte? = Byte char = Char ~ Char? = Character double = Double ~ Double? = Double float = Float ~ Float? = Float
  • 80.
    Primitive Types -Comparison Java Kotlin Java(Wrappers) long = Long ~ Long? = Long int = Int ~ Int? = Integer short = Short ~ Short? = Short byte = Byte ~ Byte? = Byte char = Char ~ Char? = Character double = Double ~ Double? = Double float = Float ~ Float? = Float boolean = Boolean ~ Boolean? = Boolean
  • 81.
    Take Away -Primitive Types
  • 82.
    Take Away -Primitive Types • Kotlin doesn’t have Primitive types explicitly.
  • 83.
    Take Away -Primitive Types • Kotlin doesn’t have Primitive types explicitly. • Kotlin compiler chooses Primitive type when the type is mentioned as non nullable.
  • 84.
    Take Away -Primitive Types • Kotlin doesn’t have Primitive types explicitly. • Kotlin compiler chooses Primitive type when the type is mentioned as non nullable. • If the type is mentioned as nullable, Kotlin compiler treats it as a wrapper type.
  • 85.
  • 86.
  • 87.
    Class Types -Java • class String
  • 88.
    Class Types -Java • class String • interface CharSequence
  • 89.
    Class Types -Java • class String • interface CharSequence • enum Color
  • 90.
    Class Types -Java • class String • interface CharSequence • enum Color • class User
  • 91.
    Class Types -Java • class String • interface CharSequence • enum Color • class User
  • 92.
    Class Types -Java • class String • interface CharSequence • enum Color • class User
  • 93.
    Class Types -Java • class String • interface CharSequence • enum Color • class User Object is common to all.
  • 94.
  • 95.
  • 96.
    Supermost Type Object (inJava) is Same as
  • 97.
    Supermost Type Object (inJava) is Same as Any (in Kotlin)
  • 98.
  • 99.
    Type Hierarchy -Java Object
  • 100.
    Type Hierarchy -Java Object String
  • 101.
    Type Hierarchy -Java Object Integer String
  • 102.
    Type Hierarchy -Java Object Integer String User
  • 103.
  • 104.
    Type Hierarchy -Kotlin Any
  • 105.
    Type Hierarchy -Kotlin Any String
  • 106.
    Type Hierarchy -Kotlin Any Int String
  • 107.
    Type Hierarchy -Kotlin Any Int String User
  • 108.
  • 109.
    Examples Object obj =new Object(); Object user = new User(); Object str = "Test"; Object integer = 10;
  • 110.
    Examples val any =Any() val user: Any = User() val str: Any = "Test" val int: Any = 10 Object obj = new Object(); Object user = new User(); Object str = "Test"; Object integer = 10;
  • 111.
    Examples val any =Any() val user: Any = User() val str: Any = "Test" val int: Any = 10 Object obj = new Object(); Object user = new User(); Object str = "Test"; Object integer = 10; User user = new User(); String str = "Test"; Integer i = 10;
  • 112.
    Examples val any =Any() val user: Any = User() val str: Any = "Test" val int: Any = 10 Object obj = new Object(); Object user = new User(); Object str = "Test"; Object integer = 10; User user = new User(); String str = "Test"; Integer i = 10; val user: User = User() val str: String = "Test" val i: Int = 10
  • 113.
    Examples val any =Any() val user: Any = User() val str: Any = "Test" val int: Any = 10 Object obj = new Object(); Object user = new User(); Object str = "Test"; Object integer = 10; User user = new User(); String str = "Test"; Integer i = 10; val user = User() val str = "Test" val i = 10 val user: User = User() val str: String = "Test" val i: Int = 10
  • 114.
    Examples val any =Any() val user: Any = User() val str: Any = "Test" val int: Any = 10 Object obj = new Object(); Object user = new User(); Object str = "Test"; Object integer = 10; User user = new User(); String str = "Test"; Integer i = 10; val user = User() val str = "Test" val i = 10 val user: User = User() val str: String = "Test" val i: Int = 10 Type Inferrence
  • 115.
  • 116.
    Examples (with null) Objectobj = null; User user = null; String str = null; Integer i = null;
  • 117.
    Examples (with null) Objectobj = null; User user = null; String str = null; Integer i = null; val any: Any = null val user: User = null val str: String = null val i: Int = null
  • 118.
    Examples (with null) Objectobj = null; User user = null; String str = null; Integer i = null;
  • 119.
    Examples (with null) Objectobj = null; User user = null; String str = null; Integer i = null; val any: Any? = null val user: User? = null val str: String? = null val i: Int? = null
  • 120.
  • 121.
  • 122.
    Type Hierarchy (Final)- Java Object String
  • 123.
    Type Hierarchy (Final)- Java Object Integer String
  • 124.
    Type Hierarchy (Final)- Java Object Integer String User
  • 125.
    Type Hierarchy (Final)- Java Object Integer String User null
  • 126.
  • 127.
    Type Hierarchy -Kotlin Any
  • 128.
    Type Hierarchy -Kotlin Any String
  • 129.
    Type Hierarchy -Kotlin Any Int String
  • 130.
    Type Hierarchy -Kotlin Any Int String User
  • 131.
    Type Hierarchy -Kotlin Any Int String User null
  • 132.
    Type Hierarchy -Kotlin Any Int String User null then what???
  • 133.
  • 134.
  • 135.
    Nothing Type • Aspecial type that exists but doesn’t represent any value.
  • 136.
    Nothing Type • Aspecial type that exists but doesn’t represent any value. • Should be used to mark any code that can never be reached.
  • 137.
    Nothing Type • Aspecial type that exists but doesn’t represent any value. • Should be used to mark any code that can never be reached. • We can also use it while type inference.
  • 138.
  • 139.
    Examples - NothingType val s = person.name ?: throw IllegalArgumentException("Name required") println(s) // 's' is known to be initialized at this point
  • 140.
    Examples - NothingType val s = person.name ?: throw IllegalArgumentException("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?> var user: User? = x
  • 141.
  • 142.
    Type Hierarchy -Kotlin Any
  • 143.
    Type Hierarchy -Kotlin Any String
  • 144.
    Type Hierarchy -Kotlin Any Int String
  • 145.
    Type Hierarchy -Kotlin Any Int String User
  • 146.
    Type Hierarchy -Kotlin Any Int String User Nothing
  • 147.
    Type Hierarchy -with null - Kotlin
  • 148.
    Type Hierarchy -with null - Kotlin Any? Any
  • 149.
    Type Hierarchy -with null - Kotlin Any? Any String? String
  • 150.
    Type Hierarchy -with null - Kotlin Any? Any String? String Any? String?
  • 151.
    Type Hierarchy -with null - Kotlin Any? Any String? String Any? String? Any? String
  • 152.
  • 153.
  • 154.
    Type Hierarchy (Final)- Kotlin Any String
  • 155.
    Type Hierarchy (Final)- Kotlin Any StringInt
  • 156.
    Type Hierarchy (Final)- Kotlin Any StringInt User
  • 157.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing
  • 158.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any?
  • 159.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any? String?
  • 160.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any? String?Int?
  • 161.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any? String?Int? User?
  • 162.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any? String?Int? User? Nothing?
  • 163.
    Type Hierarchy (Final)- Kotlin Any StringInt User Nothing Any? String?Int? User? Nothing?
  • 164.
  • 165.
  • 166.
    Examples - voidand Unit private void test() {} private void test() { return; }
  • 167.
    Examples - voidand Unit private void test() {} private void test() { return; } private fun test(): Unit {} private fun test(): Unit { return Unit } private fun test() {} private fun test(): Unit {}
  • 168.
  • 169.
  • 170.
    Array Type • Kotlinhas no [] syntax for creating Arrays.
  • 171.
    Array Type • Kotlinhas no [] syntax for creating Arrays. • Instead it has a special classs called Array<T>
  • 172.
  • 173.
    Examples - ArrayType String[] names = new String[10]; String[] colors = new String[] {"Red", "Green", "Blue"};
  • 174.
    Examples - ArrayType String[] names = new String[10]; String[] colors = new String[] {"Red", "Green", "Blue"}; val names: Array<String> = emptyArray() val colors: Array<String> = arrayOf("Red", "Green", "Blue") val nulls: Array<String?> = arrayOfNulls(10)
  • 175.
    Examples - Arrayof Primitive Type
  • 176.
    Examples - Arrayof Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5};
  • 177.
    Examples - Arrayof Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5}; val numbers: Array<Int> = emptyArray() val nums: Array<Int> = arrayOf(2, 3, 4) val nulls: Array<Int?> = arrayOfNulls(10)
  • 178.
    Examples - Arrayof Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5}; val numbers: Array<Int> = emptyArray() val nums: Array<Int> = arrayOf(2, 3, 4) val nulls: Array<Int?> = arrayOfNulls(10) } Integer[]
  • 179.
    Examples - Arrayof Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5}; val numbers: Array<Int> = emptyArray() val nums: Array<Int> = arrayOf(2, 3, 4) val nulls: Array<Int?> = arrayOfNulls(10) val nums: IntArray = intArrayOf(1, 2, 3) val longs: LongArray = longArrayOf(1L, 2L, 3L) } Integer[]
  • 180.
    Examples - Arrayof Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5}; val numbers: Array<Int> = emptyArray() val nums: Array<Int> = arrayOf(2, 3, 4) val nulls: Array<Int?> = arrayOfNulls(10) val nums: IntArray = intArrayOf(1, 2, 3) val longs: LongArray = longArrayOf(1L, 2L, 3L) } Integer[] } int[]
  • 181.
  • 182.
    Nullable Types -Interoperability
  • 183.
    Nullable Types -Interoperability Since Java doesn’t have nullable types, we need to take extra care of our code while taking adventage of interoperability.
  • 184.
    Nullable Types -Interoperability
  • 185.
    Nullable Types -Interoperability // Test.java public class Test { public String test() { return null; } }
  • 186.
    Nullable Types -Interoperability // Test.java public class Test { public String test() { return null; } } // TestKotlin.kt class Test { private test() { val test = Test() test.test().toString() } }
  • 187.
    Nullable Types -Interoperability // Test.java public class Test { public String test() { return null; } } // TestKotlin.kt class Test { private test() { val test = Test() test.test().toString() } } This will lead to NPE
  • 188.
    How to avoidsuch NPE?
  • 189.
    How to avoidsuch NPE? // Test.java public class Test { @Nullable public String test() { return null; } }
  • 190.
    How to avoidsuch NPE? // TestKotlin.kt class Test { private test() { val test = Test() test.test().toString() } } // Test.java public class Test { @Nullable public String test() { return null; } }
  • 191.
    How to avoidsuch NPE? // TestKotlin.kt class Test { private test() { val test = Test() test.test().toString() } } Now no NPE as it fails at Compile Time // Test.java public class Test { @Nullable public String test() { return null; } }
  • 192.
  • 193.
    Take Away • Kotlinhas no primitive and wrapper type speartely.
  • 194.
    Take Away • Kotlinhas no primitive and wrapper type speartely. • Nullable and Non-Nullable values are two separate types.
  • 195.
    Take Away • Kotlinhas no primitive and wrapper type speartely. • Nullable and Non-Nullable values are two separate types. • By default all types are Non-Nullable.
  • 196.
    Take Away • Kotlinhas no primitive and wrapper type speartely. • Nullable and Non-Nullable values are two separate types. • By default all types are Non-Nullable. • Type and Type? are not same.
  • 197.
    Take Away -Continued…
  • 198.
    Take Away -Continued… • Object in Java is same as Any in Kotlin.
  • 199.
    Take Away -Continued… • Object in Java is same as Any in Kotlin. • void in Java is same as Unit in Kotlin.
  • 200.
    Take Away -Continued… • Object in Java is same as Any in Kotlin. • void in Java is same as Unit in Kotlin. • Nothing is not same as Unit or void.
  • 201.
    Take Away -Continued… • Object in Java is same as Any in Kotlin. • void in Java is same as Unit in Kotlin. • Nothing is not same as Unit or void. • Nothing is converted into void in byte code level as JVM doesn’t has it.
  • 202.
    Take Away -Continued… • Object in Java is same as Any in Kotlin. • void in Java is same as Unit in Kotlin. • Nothing is not same as Unit or void. • Nothing is converted into void in byte code level as JVM doesn’t has it. • Always annotate your Java types to avoid NPE in Kotlin world.
  • 203.