Kotlin is a statically typed programming language that targets the JVM, Android, JavaScript and Native platforms. It was developed by JetBrains and released in version 1.0 in 2016. Kotlin aims to be concise, safe, pragmatic and focused on interoperability with Java. It can be used for both server-side and Android development and works with existing Java libraries and frameworks.
2. What is
Kotlin is a statically typed programming language that targets the JVM,
Android, JavaScript and Native. It’s developed by JetBrains. The project
started in 2010 and was open source from very early on. The first
official 1.0 release was in February 2016.
5. What is Kotlin all About?
•It’s a new programming language
targeting the Java platform.
•Kotlin is concise, safe, pragmatic,
and focused on interoperability
with Java code.
6. •Kotlin can be used almost everywhere Java is used
today: for server-side development, Android apps, and
much more.
•Kotlin works great with all existing Java libraries and
frameworks and runs with (almost) the same level of
performance as Java.
•Any experienced Java developer can pick up Kotlin in a
few hours.
•C# developers will also feel at home working with Kotlin,
since the two languages share some features.
What is Kotlin all About?
9. Statically Typed
•Just like Java, Kotlin is a statically typed programming
language. This means the type of every expression in a
program is known at compile time, and the compiler can
validate that the methods and fields you’re trying to
access exist on the objects you’re using.
•On the other hand, in contrast to Java, Kotlin doesn’t
require you to specify the type of every variable explicitly
in your source code. In many cases, the type of a variable
can automatically be determined from the context,
allowing you to omit the type declaration (type inference).
Statically typed
10. Functional and Object-Oriented
Object-oriented programming may be obvious to everyone with Java
knowledge, but functional programming may not be.
The key concepts of functional programming are as follows:
• First-class functions— You work with functions (pieces of behavior) as values.
You can store them in variables, pass them as parameters, or return them
from other functions.
• Immutability— You work with immutable objects, which guarantees that their
state can’t change after their creation.
• No side effects— You use pure functions that return the same result given the
same inputs and don’t modify the state of other objects or interact with the
outside world.
https://medium.com/@JorgeCastilloPr/kotlin-functional-programming-does-it-make-sense-36ad07e6bacf
11. Kotlin & Java
•Developers have been building applications in Java for many
years and have accumulated a huge stack of frameworks and
technologies to help build them.
•The big advantage of Kotlin in this environment is its
seamless interoperability with existing Java code. You can
use Kotlin and Java files in the same project, and they will work
nicely together.
•You won’t run into problems when you need to extend Java
classes in Kotlin or annotate the methods and fields of a
class in a certain way. And the benefit is that the system’s code
will be more compact, more reliable, and easier to maintain.
12. Kotlin on Android
Since May, 2017 Kotlin is an official language on Android.
It's expressive, concise, and powerful. Best of all, it's
interoperable with the existing Android languages and runtime.
Android Studio is built upon IntelliJ IDEA, an IDE built by
JetBrains—the same company that created the Kotlin language.
The JetBrains team has been working for years to make sure Kotlin
works great with IntelliJ IDEA. So we're inheriting all their hard
work. Starting with Android Studio 3.0, tooling support for Kotlin is
bundled directly into Android Studio.
13. THE PHILOSOPHY OF KOTLIN
Pragmatic
•Kotlin is a practical language designed to solve real-world
problems.
•Kotlin is not a research language.
•Kotlin doesn’t enforce using any particular programming
style or para-digm.
•Another aspect of Kotlin’s pragmatism is its focus on tooling.
the IntelliJ IDEA plug-in was developed in lockstep with the
compiler, and language features were always designed with
tooling in mind.
14. Concise
• It’s common knowledge that developers spend more time reading existing
code than writing new code.
• The language is concise if its syntax clearly expresses the intent of the code
you read and doesn’t obscure it with boilerplate required to specify how the
intent is accomplished.
• In Kotlin, we’ve tried hard to ensure that all the code you write carries
meaning and isn’t just there to satisfy code structure requirements. A lot of
the standard Java boilerplate, such as getters, setters, and the logic for
assigning constructor parameters to fields, is implicit in Kotlin and doesn’t
clutter your source code.
• More concise code takes less time to write and, more important, less time
to read. This improves your productivity and lets you get things done faster.
THE PHILOSOPHY OF KOTLIN
15. Safe
•Kotlin attemps to achieve a higher level of safety than in
Java, with a smaller overall cost. Running on the JVM
already provides a lot of safety guarantees: for example,
memory safety, preventing buffer overflows, and other
problems caused by incorrect use of dynamically allocated
memory. As a statically typed language on the JVM, Kotlin
also ensures the type safety of your applications.
Safe
THE PHILOSOPHY OF KOTLIN
16. Safe
•Most important, Kotlin strives to remove the
NullPointerException from your program. Kotlin’s type
system tracks values that can and can’t be null and forbids
operations that can lead to a NullPointer-Exception at
runtime. The additional cost required for this is minimal:
marking a type as nullable takes only a single character, a
question mark at the end.
•Another type of exception that Kotlin helps avoid is the
ClassCastException.
Safe
THE PHILOSOPHY OF KOTLIN
17. Interoperable
• You can call Java methods, extend Java classes and implement interfaces,
apply Java annotations to your Kotlin classes, and so on.
• Unlike some other JVM languages, Kotlin goes even further with
interoperability, making it effortless to call Kotlin code from Java as well. No
tricks are required: Kotlin classes and methods can be called exactly like
regular Java classes and methods. This gives you the ultimate flexibility in
mixing Java and Kotlin code anywhere in your project. When you start
adopting Kotlin in your Java project, you can run the Java-to-Kotlin converter
on any single class in your codebase, and the rest of the code will continue to
compile and work without any modifications. This works regardless of the
role of the class you’ve converted.
THE PHILOSOPHY OF KOTLIN
18. Interoperable
Another area where Kotlin focuses on interoperability is its
use of existing Java libraries to the largest degree
possible. For example, Kotlin doesn’t have its own
collections library. It relies fully on Java standard library
classes, extending them with additional functions for more
convenient use in Kotlin. This means you never need to
wrap or convert objects when you call Java APIs from
Kotlin, or vice versa. All the API richness provided by Kotlin
comes at no cost at runtime.
THE PHILOSOPHY OF KOTLIN
19. Interoperable
The Kotlin tooling also provides full support for cross-language
projects. It can compile an arbitrary mix of Java and Kotlin source
files, regardless of how they depend on each other. The IDE
features work across languages as well, allowing you to:
• Navigate freely between Java and Kotlin source files
• Debug mixed-language projects and step between code written
in different languages
• Refactor your Java methods and have their use in Kotlin code
correctly updated, and vice versa
THE PHILOSOPHY OF KOTLIN
20. THE KOTLIN TOOLS
•Just like Java, Kotlin is a compiled language.
•Kotlin source code is normally stored in files with the
extension .kt
•You can use the «kotlinc» command to compile your
code from the command line and use the java command
to execute your code:
Kotlin build process
21. • Code compiled with the Kotlin compiler depends on the Kotlin
runtime library. It contains the definitions of Kotlin’s own
standard library classes and the extensions that Kotlin adds to
the standard Java APIs. The runtime library needs to be
distributed with your application.
• In most real-life cases, you’ll be using a build system such as
Maven, Gradle, or Ant to compile your code. Kotlin is compatible
with all those build systems. All of those build systems also
support mixed-language projects that combine Kotlin and Java
in the same codebase. In addition, Maven and Gradle take care
of including the Kotlin runtime library as a dependency of your
application.
THE KOTLIN TOOLS
22. Compatibility with IntelliJ IDEA and Android Studio
•The IntelliJ IDEA natively supports Kotlin and it’s the most
full-featured development environment available for Kotlin.
It’s mature and stable, and it provides a complete set of
tools for Kotlin development.
•Android Studio 3.0 ships with Kotlin out of the box,
meaning Android developers no longer need to install any
extras or worry about compatibility.
THE KOTLIN TOOLS
23. Interactive shell
If you want to quickly try out small fragments of Kotlin code, you can do
that using the interactive shell (the so-called REPL). In the REPL, you can
type Kotlin code line by line and immediately see the results of its
execution. To start the REPL, you can either run the kotlinc command with
no arguments or use the corresponding menu item in the IntelliJ IDEA
plug-in.
Eclipse plug-in
If you’re an Eclipse user, you also have the option to use Kotlin in your IDE.
The Kotlin Eclipse plug-in provides essential IDE functionality such as
navigation and code completion. The plug-in is available in the Eclipse
Marketplace. To install it, choose the Help > Eclipse Marketplace menu
item, and search for Kotlin in the list.
THE KOTLIN TOOLS
24. Online playground
The easiest way to try Kotlin doesn’t require any installation or configuration.
At http://try.kotl.in, you can find an online playground where you can write, compile, and
run small Kotlin programs. The playground has code samples demonstrating the features
of Kotlin including all examples from this book, as well as a series of exercises for learning
Kotlin interactively.
Java-to-Kotlin converter
Getting up to speed with a new language is never effortless. Fortunately, we’ve built a
nice little shortcut that lets you speed up your learning and adoption by relying on your
existing knowledge of Java. This tool is the automated Java-to-Kotlin converter. Using
the converter in IntelliJ IDEA is extremely easy. You can either copy a Java code fragment
and paste it into a Kotlin file, or invoke the Convert Java File to Kotlin File action if you
need to convert an entire file. The converter is accessible in Eclipse and online as well.
THE KOTLIN TOOLS
25. What is the current version of Kotlin?
The currently released version is 1.1.50, published on September 22, 2017.
The currently released version is 1.2.10, published on December 13, 2017.
Is Kotlin free?
Yes. Kotlin is free, has been free and will remain free. It is developed under
the Apache 2.0 license and the source code is available on GitHub.
Is Kotlin an object-oriented language or a functional one?
Kotlin has both object-oriented and functional constructs. You can use it in
both OO and FP styles, or mix elements of the two. With first-class support for
features such as higher-order functions, function types and lambdas, Kotlin is a
great choice if you’re doing or exploring functional programming.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
26. What advantages does Kotlin give me over the Java
programming language?
Kotlin is more concise. Rough estimates indicate approximately a 40% cut in
the number of lines of code.
It’s also more type-safe, e.g. support for non-nullable types makes
applications less prone to NPE’s.
Other features including smart casting, higher-order functions, extension
functions and lambdas with receivers provide the ability to write expressive
code as well as facilitating creation of DSL.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
27. Is Kotlin compatible with the Java programming language?
Yes. Kotlin is 100% interoperable with the Java programming language and
major emphasis has been placed on making sure that your existing codebase
can interact properly with Kotlin. You can easily call Kotlin code from Java and
Java code from Kotlin. This makes adoption much easier and lower-risk. There’s
also an automated Java-to-Kotlin converter built into the IDE that simplifies
migration of existing code.
What can I use Kotlin for?
Kotlin can be used for any kind of development, be it server-side, client-side,
web and Android. With Kotlin/Native currently in the works, support for other
platforms such as embedded systems, macOS and iOS is coming. People are
using Kotlin for mobile and server-side applications, client-side with JavaScript
or JavaFX, and data science, just to name a few possibilities.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
28. Can I use Kotlin for Android development?
Yes. Kotlin is supported as a first-class language on Android. There are hundreds of
applications already using Kotlin for Android, such as Basecamp, Pinterest and more.
For more information check out the resource on Android development.
Can I use Kotlin for server-side development?
Yes. Kotlin is 100% compatible with the JVM and as such you can use any existing
frameworks such as Spring Boot, vert.x or JSF. In addition there are specific
frameworks written in Kotlin such as Ktor. For more information check out the
resource on server-side development.
Can I use Kotlin for web development?
Yes. In addition to using for backend web, you can also use Kotlin/JS for client-side
web. Kotlin can use definitions from DefinitelyTyped to get static typing for common
JavaScript libraries, and it is compatible with existing module systems such as AMD
and CommonJS. For more information check out the resource on client-side
development.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
29. Can I use Kotlin for desktop development?
Yes. You can use any Java UI framework such as JavaFx, Swing or other. In
addition there are Kotlin specific frameworks such as TornadoFX.
Can I use Kotlin for native development?
Kotlin/Native is currently in the works. It compiles Kotlin to native code that
can run without a VM. There is a Technology Preview released but it is not
production-ready yet, and doesn’t yet target all the platforms that we plan to
support for 1.0. For more information check out the blog post announcing
Kotlin/Native.
What IDEs support Kotlin?
Kotlin is supported by all major Java IDEs including IntelliJ IDEA, Android
Studio, Eclipse and NetBeans. In addition, a command line compiler is available
and provides straightforward support for compiling and running applications.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
30. • «We are happy to announce the first Technology Preview of Kotlin/Native that
compiles Kotlin directly to machine code. The Kotlin/Native compiler produces
standalone executables that can run without any virtual machine.
• It is not a fully functional release yet, but you can already play with the
technology and take a look at its source
code https://github.com/JetBrains/kotlin-native/ . The compiler is available under
the Apache 2 OSS license.»
https://blog.jetbrains.com/kotlin/2017/04/kotlinnative-tech-preview-kotlin-without-a-vm/
31. What build tools support Kotlin?
On the JVM side, the main build tools include Gradle, Maven, Ant, and Kobalt.
There are also some build tools available that target client-side JavaScript.
What does Kotlin compile down to?
When targeting the JVM, Kotlin produces Java compatible bytecode. When
targeting JavaScript, Kotlin transpiles to ES5.1 and generates code which is
compatible with module systems including AMD and CommonJS. When
targeting native, Kotlin will produce platform-specific code (via LLVM).
Does Kotlin only target Java 6?
No. Kotlin lets you choose between generating Java 6 and Java 8 compatible
bytecode. More optimal byte code may be generated for higher versions of the
platform.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
32. Is Kotlin hard?
Kotlin is inspired by existing languages such as Java, C#, JavaScript, Scala and Groovy.
We've tried to ensure that Kotlin is easy to learn, so that people can easily jump
on board, reading and writing Kotlin in a matter of days. Learning idiomatic Kotlin
and using some more of its advanced features can take a little longer, but overall it is
not a complicated language.
What companies are using Kotlin?
There are too many companies using Kotlin to list, but some more visible companies
that have publicly declared usage of Kotlin, be this via blog posts, GitHub repositories
or talks include Square, Pinterest or Basecamp.
Who develops Kotlin?
Kotlin is primarily developed by a team of engineers at JetBrains (current team size
is 40+). The lead language designer is Andrey Breslav. In addition to the core team,
there are also over 100 external contributors on GitHub.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
33. Where can I learn more about Kotlin?
The best place to start is https://kotlinlang.org/. From there you can download the
compiler, try it online (https://try.kotlinlang.org/) as well as get access to
resources, reference documentation
(https://kotlinlang.org/docs/reference/index.html) and tutorials
(https://kotlinlang.org/docs/tutorials/index.html).
Are there any books on Kotlin?
There are already a number of books available for Kotlin, including Kotlin in
Action which is by Kotlin team members Dmitry Jemerov and Svetlana Isakova, Kotlin
for Android Developers targeted at Android developers.
Are there any online courses available for Kotlin?
There are a few courses available for Kotlin, including a Pluralsight Kotlin Course by
Kevin Jones, an O’Reilly Course by Hadi Hariri and an Udemy Kotlin Course by Peter
Sommerhoff.
There are also many recordings of Kotlin talks available on YouTube and Vimeo.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
35. Does Kotlin have a community?
Yes. Kotlin has a very vibrant community. Kotlin developers hang out on
the Kotlin forums, StackOverflowand more actively on the Kotlin Slack (with
close to 7000 members as of May 2017).
Are there Kotlin events?
Yes. There are many User Groups and Meetups now focused exclusively around
Kotlin. You can find a list on the web site. In addition there are community
organised Kotlin Nights events around the world.
Is there a Kotlin conference?
Yes. The first official KotlinConf, taking place in San Francisco 2-3 November
2017. Kotlin is also being covered in different conferences worldwide. You can
find a list of upcoming talks on the web site.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
37. Is Kotlin on Social Media?
Yes. The most active Kotlin account is on Twitter. There is also a Google+ group.
Any other online Kotlin resources?
The web site has a bunch of online resources
(https://kotlinlang.org/community/), including Kotlin Digests
(https://kotlin.link/) by community members, a newsletter
(http://www.kotlinweekly.net/), a podcast (https://talkingkotlin.com/) and
more.
https://kotlinlang.org/docs/reference/faq.html#what-is-the-current-version-of-kotlin
KOTLIN Q & A
38. Should I Learn Kotlin or Stick to Java?
https://blog.mindorks.com/should-i-learn-kotlin-or-stick-to-java-3a73c3580ac3
39. RebelLabs Developer Productivity Report 2017
https://zeroturnaround.com/rebellabs/developer-productivity-report-2017-why-do-you-use-java-tools-you-use/
programming language popularity.
40. Do you like the
tooling choices you
have made?
Congratulations to Kotlin on
taking the first place with the
highest satisfaction score of 9.1
(highest across the whole report,
not just the programming
languages question)! Allegedly,
Kotlin is the language you want
your Java to be like, and it seems
that developers who use Kotlin are
really happy about the design
choices made by the Kotlin
team. Well done, JetBrains!
41.
42. Kotlin aims to be an enhancement to Java, rather than a complete rewrite, so many of
the skills you've acquired and honored throughout your Java career should still be
applicable to your Kotlin projects.
Since Kotlin and Java classes can exist side by side in the same project, you can start
using Kotlin without having to do anything drastic like converting an entire project to
Kotlin or starting a new project specifically so that you can try your hand at Kotlin.
Thanks to this level of interoperability, if you do have a project that you’re already
working on, you can trial Kotlin in a small part of that project without affecting the
rest of your code base. And, if you do decide that you want to continue using Kotlin,
you can either migrate your project’s existing Java code to Kotlin one file at a time,
or leave your project's legacy Java code untouched and only use Kotlin for the new
classes and features.
Since Kotlin is completely interoperable with Java, you’ll also be able to use the
majority of Java libraries and frameworks in your Kotlin projects—even advanced
frameworks that rely on annotation processing.
https://code.tutsplus.com/articles/java-vs-kotlin-should-you-be-using-kotlin-for-android-development--cms-27846
Why Should I Make the Switch
From Java?
44. Kotlin’s Weaknesses
Extra Runtime Size
The Kotlin Standard Library and runtime will increase the size of your .apk. While
this only equates to around 800KB, if your application is already on the large side
then that extra 800KB may tip it over the edge and make users think twice before
downloading your app.
Initial Readability of Code
Although Kotlin’s concise syntax is one of the language's greatest strengths, you may
initially find some Kotlin difficult to decipher, simply because there’s so much going
on in such a small amount of code. Java may be more verbose, but the upside is that
everything is clearly spelled out, which means unfamiliar Java code tends to be easier
to decipher than unfamiliar Kotlin.
Also, if used incorrectly, Kotlin’s operator overloading can result in code that’s difficult
to read.
45. Kotlin’s Weaknesses
Android Studio’s Performance
Android Studio’s auto-complete and compilation tends to run slightly slower
when you’re working with Kotlin, compared to a pure Java project.
Smaller Community and Less Available Help
Since Kotlin is a relatively new language, the Kotlin community is still fairly
small, particularly compared to the community surrounding more established
languages like Java. If you do make the switch to Kotlin, then you may not have
access to the same number of tutorials, blog posts, and user documentation,
and may encounter less community support on places like forums and Stack
Overflow. At the time of writing, searching for Kotlin on Stack Overflow returns
just over 4,600 posts tagged with Kotlin—compared to the whopping
1,000,000+ posts that contain the Java tag.
46. Expressiveness: Kotlin is particularly good at reducing the amount of boilerplate code
you need to write, which should make coding in Kotlin a much more enjoyable experience,
compared to coding in more verbose languages such as Java
Scalability: Kotlin's support for coroutines helps build server-side applications that scale
to massive numbers of clients with modest hardware requirements.
Interoperability: Kotlin is fully compatible with all Java-based frameworks, which lets
you stay on your familiar technology stack while reaping the benefits of a more modern
language.
Migration: Kotlin supports gradual, step by step migration of large codebases from Java to
Kotlin. You can start writing new code in Kotlin while keeping older parts of your system
in Java.
Tooling: In addition to great IDE support in general, Kotlin offers framework-specific
tooling (for example, for Spring) in the plugin for IntelliJ IDEA Ultimate.
Learning Curve: For a Java developer, getting started with Kotlin is very easy. The
automated Java to Kotlin converter included in the Kotlin plugin helps with the first
steps. Kotlin Koans offer a guide through the key features of the language with a series of
interactive exercises.
Kotlin’s Strengths
47. Kotlin compiles to JVM bytecode or JavaScript
Like Java, Bytecode is the compiled format for Kotlin programs also. Bytecode
means Programming code that, once compiled, is run through a virtual
machine instead of the computer’s processor. By using this approach, source
code can be run on any platform once it has been compiled and run through
the virtual machine.
Kotlin programs can use all existing Java Frameworks and Libraries
Yes, it's true that Kotlin programs can use all existing java frameworks and
libraries, even advanced frameworks that rely on annotation processing. The
main important thing about kotlin language is that it can easily integrate with
Maven, Gradle and other build systems.
https://www.xenonstack.com/blog/overview-of-kotlin-comparison-between-kotlin-java?utm_source=medium&utm_campaign=kotlin_blog
Kotlin’s Strengths
48. Compilation Speed (Java vs Kotlin)
• Clean Builds (Building our Codebase first time)
When we compile our Kotlin code first time, then it takes more time than Java.
Java compilation is almost around 15-20% faster than Kotlin.
• Incremental Builds
But as we know that most of the time we need incremental builds like we are
doing some modifications in our existing code and then building them and
doing continuous deployment. So in that perspective, Kotlin takes same
amount of time to compile as compared to Java and even little bit faster than
Java.
• Conclusion
We know that clean build is done only one time in our project and I think
Incremental Builds Compilation time are more crucial for us than Clean Build.
So Kotlin is almost same as Java and yes we can go with Kotlin without
worrying about Compilation time.
49. What Kotlin has that Java does not (1)
•Lambda expressions + Inline functions = performant
custom control structures
•Extension functions
•Null-safety
•Smart casts
•String templates
•Properties
•Primary constructors
•First-class delegation
50. What Kotlin has that Java does not (2)
•Type inference for variable and property types
•Singletons
•Declaration-site variance & Type projections
•Range expressions
•Operator overloading
•Companion objects
•Data classes
•Separate interfaces for read-only and mutable collections
•Coroutines
51. The Future of Kotlin language
Kotlin interwork with Java and provides incremental change
of code and superior type system to Java and provides the
easy Migration path from Java with backward compatibility.
With features like more declarative, less
code, mixed language database and more
expressive than Java, make Kotlin the future
langauge for enterprises applications and
mobile.