SlideShare a Scribd company logo
1 of 203
Download to read offline
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

More Related Content

What's hot

K is for Kotlin
K is for KotlinK is for Kotlin
K is for KotlinTechMagic
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformEastBanc Tachnologies
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
Getting Started With Kotlin Development - Rivu
Getting Started With Kotlin Development - Rivu Getting Started With Kotlin Development - Rivu
Getting Started With Kotlin Development - Rivu CodeOps Technologies LLP
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentAdam Magaña
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
Kotlin  what_you_need_to_know-converted event 4 with nigeriansKotlin  what_you_need_to_know-converted event 4 with nigerians
Kotlin what_you_need_to_know-converted event 4 with nigeriansjunaidhasan17
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Eamonn Boyle
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming PatternsVasil Remeniuk
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)mircodotta
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android DevelopmentSpeck&Tech
 
Kotlin for all the Things
Kotlin for all the ThingsKotlin for all the Things
Kotlin for all the ThingsEamonn Boyle
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java DevelopersRobert Reiz
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Mario Camou Riveroll
 

What's hot (19)

K is for Kotlin
K is for KotlinK is for Kotlin
K is for Kotlin
 
Introduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platformIntroduction to Kotlin Language and its application to Android platform
Introduction to Kotlin Language and its application to Android platform
 
Javascript
JavascriptJavascript
Javascript
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
Getting Started With Kotlin Development - Rivu
Getting Started With Kotlin Development - Rivu Getting Started With Kotlin Development - Rivu
Getting Started With Kotlin Development - Rivu
 
Say Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android DevelopmentSay Goodbye To Java: Getting Started With Kotlin For Android Development
Say Goodbye To Java: Getting Started With Kotlin For Android Development
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
Kotlin  what_you_need_to_know-converted event 4 with nigeriansKotlin  what_you_need_to_know-converted event 4 with nigerians
Kotlin what_you_need_to_know-converted event 4 with nigerians
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019Kotlin Native - C / Swift Interop - ACCU Autmn 2019
Kotlin Native - C / Swift Interop - ACCU Autmn 2019
 
Fall in love with Kotlin
Fall in love with KotlinFall in love with Kotlin
Fall in love with Kotlin
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Guava et Lombok au Lyon JUG
Guava et Lombok au Lyon JUGGuava et Lombok au Lyon JUG
Guava et Lombok au Lyon JUG
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
 
Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)Effective Scala (JavaDay Riga 2013)
Effective Scala (JavaDay Riga 2013)
 
Kotlin for Android Development
Kotlin for Android DevelopmentKotlin for Android Development
Kotlin for Android Development
 
Kotlin for all the Things
Kotlin for all the ThingsKotlin for all the Things
Kotlin for all the Things
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java Developers
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 

Similar to Kotlin Types for Java Developers

Apple Swift API Design Guideline
Apple Swift API Design GuidelineApple Swift API Design Guideline
Apple Swift API Design GuidelineChihyang Li
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaBrian Topping
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010ssoroka
 
Mobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMohammad Shaker
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART IHari Christian
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classesteach4uin
 
Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
Introduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and TypeclassesIntroduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and TypeclassesJordi Pradel
 
Protocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That WayProtocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That WayAlexis Gallagher
 
Python45s - Session 01
Python45s - Session 01Python45s - Session 01
Python45s - Session 01Al Sayed Gamal
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Jamshid Hashimi
 

Similar to Kotlin Types for Java Developers (20)

Apple Swift API Design Guideline
Apple Swift API Design GuidelineApple Swift API Design Guideline
Apple Swift API Design Guideline
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Lesson2
Lesson2Lesson2
Lesson2
 
Lesson2
Lesson2Lesson2
Lesson2
 
Lesson2
Lesson2Lesson2
Lesson2
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Mobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java PrimerMobile Software Engineering Crash Course - C02 Java Primer
Mobile Software Engineering Crash Course - C02 Java Primer
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART I
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Learning core java
Learning core javaLearning core java
Learning core java
 
data types.pdf
data types.pdfdata types.pdf
data types.pdf
 
Introduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and TypeclassesIntroduction to Scala Implicits, Pimp my library and Typeclasses
Introduction to Scala Implicits, Pimp my library and Typeclasses
 
Protocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That WayProtocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That Way
 
Python_basics.pptx
Python_basics.pptxPython_basics.pptx
Python_basics.pptx
 
Exploring Kotlin
Exploring KotlinExploring Kotlin
Exploring Kotlin
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python45s - Session 01
Python45s - Session 01Python45s - Session 01
Python45s - Session 01
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
 
Java
JavaJava
Java
 

Recently uploaded

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Kotlin Types for Java Developers

  • 1. Kotlin Types for Java Developers Chandra Sekar @iChanSek
  • 3. Type System in Java • Primitive Types
  • 4. Type System in Java • Primitive Types • Class Types
  • 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
  • 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. Primitive Types - Comparison
  • 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
  • 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. Primitive Types - Comparison
  • 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
  • 54. Example - Primitive Type int x = 10; // Java - Primitive int
  • 55. Example - Primitive Type int x = 10; // Java - Primitive int // is Same as val x: Int = 10 // Kotlin - Primitive int
  • 56. 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
  • 58. Example - Wrapper Type Integer x = new Integer(10); // Java - Wrapper Type Integer y = 10; // Java - Wrapper Type (Auto Boxing)
  • 59. 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
  • 60. 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;
  • 63. Nullable Types • Kotlin has separate type to handle null.
  • 64. Nullable Types • Kotlin has separate type to handle null. • By default all types are non nullable.
  • 65. 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.
  • 66. 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 ‘?’
  • 67. 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?
  • 68. Back to Previous Example
  • 69. Back to Previous Example // then, how to represent this in Kotlin? Integer z = null;
  • 70. Back to Previous Example // then, how to represent this in Kotlin? Integer z = null; // And, the answer is val z: Int? = null
  • 71. Primitive Types - Comparison
  • 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.
  • 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.
  • 96. Supermost Type Object (in Java) is Same as
  • 97. Supermost Type Object (in Java) is Same as Any (in Kotlin)
  • 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. Type Hierarchy - Kotlin
  • 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
  • 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
  • 116. Examples (with null) Object obj = null; User user = null; String str = null; Integer i = null;
  • 117. 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
  • 118. Examples (with null) Object obj = null; User user = null; String str = null; Integer i = null;
  • 119. 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
  • 121. Type Hierarchy (Final) - Java Object
  • 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. Type Hierarchy - Kotlin
  • 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???
  • 135. Nothing Type • A special type that exists but doesn’t represent any value.
  • 136. 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.
  • 137. 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.
  • 139. Examples - Nothing Type val s = person.name ?: throw IllegalArgumentException("Name required") println(s) // 's' is known to be initialized at this point
  • 140. 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
  • 141. Type Hierarchy - Kotlin
  • 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
  • 153. Type Hierarchy (Final) - Kotlin Any
  • 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?
  • 165. Examples - void and Unit
  • 166. Examples - void and Unit private void test() {} private void test() { return; }
  • 167. 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 {}
  • 170. Array Type • Kotlin has no [] syntax for creating Arrays.
  • 171. Array Type • Kotlin has no [] syntax for creating Arrays. • Instead it has a special classs called Array<T>
  • 173. Examples - Array Type String[] names = new String[10]; String[] colors = new String[] {"Red", "Green", "Blue"};
  • 174. 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)
  • 175. Examples - Array of Primitive Type
  • 176. Examples - Array of Primitive Type int[] numbers = new int[10]; int[] nums = new int[] {1, 2, 3, 4, 5};
  • 177. 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)
  • 178. 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[]
  • 179. 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[]
  • 180. 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[]
  • 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 avoid such NPE?
  • 189. How to avoid such NPE? // Test.java public class Test { @Nullable public String test() { return null; } }
  • 190. 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; } }
  • 191. 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; } }
  • 193. Take Away • Kotlin has no primitive and wrapper type speartely.
  • 194. Take Away • Kotlin has no primitive and wrapper type speartely. • Nullable and Non-Nullable values are two separate types.
  • 195. 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.
  • 196. 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.
  • 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.