NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Kotlin – Basics
- Variables and Data Types
- Control Flow
- Collections
- Improve Code Quality
- Exceptions Handling
- Multi Threading
- Functional Programming

Kotlin
● Kotlin is a new open source programming language like Java, JavaScript, etc. It is a high level strongly statically typed language that combines functional and technical part in a same place. Currently, Kotlin targets Java and JavaScript. It runs on JVM.
● Kotlin is influenced by other programming languages such as Java, Scala, Groovy, Gosu, etc. The syntax of Kotlin may not be exactly similar to JAVA, however, internally Kotlin is reliant on the existing Java Class library to produce wonderful results for the programmers. Kotlin provides interoperability, code safety, and clarity to the developers around the world.

Advantages and Disadvantages
Advantages of Kotlin:
● Easy Language − Kotlin is a functional language and very easy to learn. The syntax is pretty much similar to Java, hence it is very easy to remember. Kotlin is more expressive, which makes your code more readable and understandable.
● Concise − Kotlin is based on JVM and it is a functional language. Thus, it reduce lots of boiler plate code used in other programming languages.
● Runtime and Performance − Better performance and small runtime.
● Interoperability − Kotlin is mature enough to build an interoperable application in a less complex manner.
● Brand New − Kotlin is a brand new language that gives developers a fresh start. It is not a replacement of Java, though it is developed over JVM. It is accepted as the first official language of android development. Kotlin can be defined as - Kotlin = JAVA + extra updated new features.
Disadvantages of Kotlin:
● Namespace declaration − Kotlin allows developers to declare the functions at the top level. However, whenever the same function is declared in many places of your application, then it is hard to understand which function is being called.
● No Static Declaration − Kotlin does not have usual static handling modifier like Java, which can cause some problem to the conventional Java developer.

Kotlin – Architecture
Kotlin compiler creates a byte code and that byte code can run on the JVM, which is exactly equal to the byte code generated by the Java .class file. Whenever two byte coded file runs on the JVM, they can communicate with each other and this is how an interoperable feature is established in Kotlin for Java.

Variables and Data Types
Variables are a little bucket where we can put information.
Types:
● String
● Int
● Double
● Boolean
● Float
● Char

Kotlin - Control Flow
● Conditionals - "if-else" Expressions
● Conditionals - "when" Expressions
● For Loop
● While Loop and Do-While Loop
● Use of Return, Break, Continue

Collections
• Array
• ArrayList
• arrayOf
• listOf
• mutableListOf
• hashMapOf

Exceptions in Kotlin
Kotlin inherited the concept of exception from Java and supports them to seamlessly interoperate with JVM libraries. Just like other successor languages, Kotlin learned from Java that checked exceptions should not be a thing.
● Handling program logic errors
● Dual-use APIs
● API design
● Input/output
● Exceptions, asynchronous programming, and coroutines

Exceptions classes
All exception classes in Kotlin are descendants of the class Throwable. Every exception has a message, stack trace and an optional cause.
To throw an exception object, use the throw-expression:
To catch an exception, use the try-expression:

Exceptions - Handling program logic errors
● The first and foremost use of exceptions in Kotlin is to handle program logic errors.
● Calling a function with a negative or zero quantity indicates a logic error in some other place in the code and throws an exception.
● As a rule of thumb, you should not be catching exceptions in general Kotlin code.

Multi Threading
Process: A process is a running instance of a program.
Thread: A thread is the smallest sequence of instructions that can be managed independently by a scheduler. Each process is divided into one or more threads.
Quantum: Multiple threads are interleaved and receive a small amount of time slice called quantum.
Thread Context Switch: When a thread’s quantum is complete, the thread scheduler switches the thread with another one.
This is called thread context switch and it happens so quickly that the threads appear to be running in parallel while actually running sequentially.

Thread
Created and controlled by the java.lang.Thread class. A thread is an independent path of execution within a program. A java program can have many threads and these threads can run concurrently, either asynchronously or synchronously.
How to create a thread:
1. Implement the runnable interface (java.lang.Runnable)
2. Extending the Thread class (java.lang.Thread)

Lifecycle of Thread
● New: The thread is in the new state if you create an instance of Thread class but before the invocation of start() method
● Runnable: The thread is in the runnable state after the invocation of the start() method, but the thread scheduler has not selected it to be the running thread.
● Running: The thread is in running state if the thread scheduler has selected it.
● Non-runnable: This is the state when the thread is still alive but is currently not eligible to run.
● Terminated: A thread is in a terminated or dead state when its run() method exits.

Thread termination: Why and When?
● Thread consume Memory, kernel resources CPU cycles and cache memory
● If a thread finished its work but the application is still running we want to clean up the thread resources.
● If a thread is misbehaving (e.g a server request not responding and a heavy calculation takes longer time than expected), we want to stop it.
● the application will not stop as long as at least one thread is still running even if the main thread stops running.

Daemon Thread
Background thread does not prevent the application from existing if the main thread terminates.
● Scenario-1: Background task that should not block our application for terminating (e.g File saving thread in the text editor)
● Scenario-2: Code in the worker thread is not under our control and we do not want it to block our application for terminating. (e.g Worker thread that uses an external library)
thread.daemon=true

Thread join
● join(): It will put the current thread on wait until the thread on which it is called is dead. If a thread is interrupted then it will throw InterruptedException.
● join(long millis): It will put the current thread on wait until the thread on which it is called is dead or wait for a specified time (milliseconds).
● join(long millis, int nanos): It will put the current thread on wait until the thread on which it is called is dead or wait for a specified time (milliseconds + nanos).

Functional Programming
Functional programming can lead to code that is easier to think about, has fewer bugs and is easier to test.
We construct our programs using only pure functions — functions which have no side effects.
Pure functions are easier to test, reuse, parallelize, generalize, and reason about.
The task is to map raw data to domain specific data

Functional Programming
● Pure functions
● Immutability
● Lazy initialization
● Function as First-class Citizen
● Lambda Expression
● Higher order function
● Function type
● Functional vs Imperative Paradigm

Scope Functions
The Kotlin standard library contains several functions whose sole purpose is to execute a block of code within the context of an object. When you call such a function on an object with a lambda expression provided, it forms a temporary scope. In this scope, you can access the object without its name. Such functions are called scope functions.
There are five of them:
let, run, with, apply, and also

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.