Learn to program with kotlin

Basic syntax

This is a collection of basic syntax elements with examples. At the end of every section, you’ll find a link to a detailed description of the related topic.

You can also learn all the Kotlin essentials with the free Kotlin Core track by JetBrains Academy.

Package definition and imports

Package specification should be at the top of the source file.

It is not required to match directories and packages: source files can be placed arbitrarily in the file system.

Program entry point

An entry point of a Kotlin application is the main function.

Another form of main accepts a variable number of String arguments.

print prints its argument to the standard output.

println prints its arguments and adds a line break, so that the next thing you print appears on the next line.

Functions

A function with two Int parameters and Int return type.

A function body can be an expression. Its return type is inferred.

A function that returns no meaningful value.

Unit return type can be omitted.

Variables

Read-only local variables are defined using the keyword val . They can be assigned a value only once.

Variables that can be reassigned use the var keyword.

You can declare variables at the top level.

Creating classes and instances

To define a class, use the class keyword.

Properties of a class can be listed in its declaration or body.

The default constructor with parameters listed in the class declaration is available automatically.

Inheritance between classes is declared by a colon ( : ). Classes are final by default; to make a class inheritable, mark it as open .

Comments

Just like most modern languages, Kotlin supports single-line (or end-of-line) and multi-line (block) comments.

Block comments in Kotlin can be nested.

See Documenting Kotlin Code for information on the documentation comment syntax.

String templates

Conditional expressions

In Kotlin, if can also be used as an expression.

Источник

Get started with Kotlin

Kotlin is a modern but already mature programming language designed to make developers happier. It’s concise, safe, interoperable with Java and other languages, and provides many ways to reuse code between multiple platforms for productive programming.

To start, why not take our tour of Kotlin? This tour covers the fundamentals of the Kotlin programming language.

Install Kotlin

Kotlin is included in each IntelliJ IDEA and Android Studio release. Download and install one of these IDEs to start using Kotlin.

Create your powerful application with Kotlin

Here is how you can take the first steps in developing Kotlin server-side applications.

  1. Create your first backend application:
    • To start from scratch, create a basic JVM application with the IntelliJ IDEA project wizard.
    • If you prefer more robust examples, choose one of the frameworks below and create a project:

A mature family of frameworks with an established ecosystem that is used by millions of developers worldwide.

A lightweight framework for those who value freedom in making architectural decisions.

  • The Kotlin standard library offers a lot of useful things such as collections or coroutines.
  • Take a look at the following third-party frameworks, libs and tools for Kotlin.
  • How to write your first unit test.
  • How to mix Kotlin and Java code in your application.
  • Slack: get an invite and join the #getting-started, #server, #spring, or #ktor channels.
  • StackOverflow: subscribe to the «kotlin», «spring-kotlin», or «ktor» tags.

If you’ve encountered any difficulties or problems, report an issue to our issue tracker.

Here you’ll learn how to develop and improve your cross-platform mobile application using Kotlin Multiplatform.

  1. Set up your environment for cross-platform mobile development.
  2. Create your first application for iOS and Android:
    • To start from scratch, create a basic cross-platform mobile application with the project wizard.
    • If you have an existing Android application and want to make it cross-platform, complete the Make your Android application work on iOS tutorial.
    • If you prefer real-life examples, clone and play with an existing project, for example the networking and data storage project from the Create a multiplatform app using Ktor and SQLdelight tutorial or any sample project.
  3. Use a wide set of multiplatform libraries to implement the required business logic only once in the shared module. Learn more about adding dependencies.
Library Details
Ktor Docs
Serialization Docs and sample
Coroutines Docs and sample
DateTime Docs
SQLDelight Third-party library. Docs
  • Learn more about Kotlin Multiplatform.
  • Look through samples on GitHub.
  • Create and publish a multiplatform library.
  • Learn how Kotlin Multiplatform is used at Netflix, VMware, Yandex, and many other companies.
  • Slack: get an invite and join the #getting-started and #multiplatform channels.
  • StackOverflow: Subscribe to the «kotlin-multiplatform» tag.

If you’ve encountered any difficulties or problems, report an issue to our issue tracker.

  • If you want to start using Kotlin for Android development, read Google’s recommendation for getting started with Kotlin on Android.
  • If you’re new to Android and want to learn to create applications with Kotlin, check out this Udacity course.

Follow Kotlin on Twitter, Reddit, and Youtube, and don’t miss any important ecosystem updates.

Support for multiplatform programming is one of Kotlin’s key benefits. It reduces time spent writing and maintaining the same code for different platforms while retaining the flexibility and benefits of native programming.

Here you’ll learn how to develop and publish a multiplatform library:

  1. Create a multiplatform library:
    • Complete the Create and publish a multiplatform library tutorial. It shows how to create a multiplatform library for JVM, JS, and Native platforms, test it and publish to a local Maven repository.
  2. Use libraries in your application:
    • Ktor
    • Serialization
    • Coroutines
    • DateTime

Learn more about adding dependencies on libraries. You can also find a multiplatform library in the community-driven list.

  • Introduction to Kotlin Multiplatform.
  • Kotlin Multiplatform supported platforms.
  • Kotlin Multiplatform programming benefits.
  • Slack: get an invite and join the #getting-started and #multiplatform channels.
  • StackOverflow: Subscribe to the «kotlin-multiplatform» tag.

If you’ve encountered any difficulties or problems, report an issue to our issue tracker.

Is anything missing?

If anything is missing or seems confusing on this page, please share your feedback.

Источник

Читайте также:  Php e hfnm ghj tks
Оцените статью