Skip to main content

Posts

Showing posts from February 28, 2023

Kotlin Programming Language

Kotlin  Basics Kotlin Hello World Program Kotlin Variable Kotlin Data Type Kotlin Type Conversion Kotlin Operator Kotlin Standard Input/Output Kotlin Comment Kotlin  Control Flow Kotlin if Expression Kotlin When Expression Kotlin For Loop Kotlin While Loop Kotlin Do-While Loop Kotlin Return and Jump Kotlin Continue Jump Structure Kotlin  Function Kotlin Function Kotlin Recursion Function Kotlin For Loop Kotlin Default and Named Argument Kotlin Lambda Function Kotlin Higher order function Kotlin Inline Function

Kotlin Type Conversion

Kotlin Type Conversion Type conversion is a process in which one data type variable is converted into another data type. In Kotlin, implicit conversion of smaller data type into larger data type is not supported (as it supports in java). For example Int cannot be assigned into Long or Double. In Java int value1 = 10; long value2 = value1; //Valid code In Kotlin var value1 = 10 val value2: Long = value1 //Compile error, type mismatch However in Kotlin, conversion is done by explicit in which smaller data type is converted into larger data type and vice-versa. This is done by using helper function. var value1 = 10 val value2: Long = value1.toLong() The list of helper functions used for numeric conversion in Kotlin is given below: toByte() toShort() toInt() toLong() toFloat() toDouble() toChar() Kotlin Type Conversion Example Let see an example to convert from Int to Long. fun main(args : Array ) {      var value1 = 100      val value2: Long =

Kotlin Data Type

Kotlin Data Type Data type (basic type) refers to type and size of data associated with variables and functions. Data type is used for declaration of memory location of variable which determines the features of data. In Kotlin, everything is an object, which means we can call member function and properties on any variable. Kotlin built in data type are categorized as following different categories: Number Character Boolean Array String Number Types Number types of data are those which hold only number type data variables. It is further categorized into different Integer and Floating point. Byte 8 bit -128 to 127 Short 16 bit -32768 to 32767 Int 32 bit -2,147,483,648 to 2,147,483,647 Long 64 bit -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Float 32 bit 1.40129846432481707e-45 to 3.40282346638528860e+38 Double 64 bit 4.94065645841246544e-324 to 1.79769313486231570e+308 Char 4 bit -128 to 127 val value1 = 'A' //or val value2: Char

Kotlin Variable

Kotlin Variable Variable refers to a memory location. It is used to store data. The data of variable can be changed and reused depending on condition or on information passed to the program. Variable Declaration Kotlin variable is declared using keyword var and val. var language ="Java" val salary = 30000 The difference between var and val is specified later on this page. Here, variable language is String type and variable salary is Int type. We don't require specifying the type of variable explicitly. Kotlin complier knows this by initilizer expression ("Java" is a String and 30000 is an Int value). This is called type inference in programming. We can also explicitly specify the type of variable while declaring it. var language: String ="Java" val salary: Int = 30000 It is not necessary to initialize variable at the time of its declaration. Variable can be initialized later on when the program is executed. var language: String

Kotlin Hello World Program

Kotlin is a statically-typed, general-purpose programming language. It is widely used to develop android applications. Our Kotlin Tutorial includes all topics of Kotlin such as introduction, architecture, class, object, inheritance, interface, generics, delegation, functions, mixing Java and Kotlin, Java vs. Kotlin, etc. Kotlin is a general-purpose, statically typed, and open-source programming language. It runs on JVM and can be used anywhere Java is used today. It can be used to develop Android apps, server-side apps and much more. Kotlin was developed by JetBrains team. A project was started in 2010 to develop the language and officially, first released in February 2016. Kotlin was developed under the Apache 2.0 license. Kotlin First Program Concept Let's understand the concepts and keywords of Kotlin program 'Hello World.kt'. fun main(args: Array ) { println("Hello World!") } 1. The first line of program defines a function called main

Kotlin Comment

Kotlin Comment Comments are the statements that are used for documentation purpose. Comments are ignored by compiler so that don't execute. We can also used it for providing information about the line of code. There are two types of comments in Kotlin.   Single line comment. Multi line comment.   Single line comment Single line comment is used for commenting single line of statement. It is done by using '//' (double slash). For example:   fun main(args: Array ) {      // this statement used for print      println("Hello World!") } Output Hello World! Multi line comment Multi line comment is used for commenting multiple line of statement. It is done by using /* */ (start with slash strict and end with star slash). For example: fun main(args: Array ) {      /* this statement      is used      for print */           println("Hello World!") } Output: Hello World!

Kotlin Standard Input/Output

Kotlin Standard Input/Output Kotlin standard input output operations are performed to flow byte stream from input device (keyboard) to main memory and from main memory to output device (screen). Kotlin Output Kotlin output operation is performed using the standard methods print() and println(). Let's see an example:   fun main(args: Array ) {      println("Hello World!")      print("Welcome to Earth") } Output Hello World! Welcome to Earth The methods print() and println() are internally call System.out.print() and System.out.println() respectively. Difference between print() and println() methods: print(): print() method is used to print values provided inside the method "()". println(): println() method is used to print values provided inside the method "()" and moves cursor to the beginning of next line. Example fun main(args: Array ){ println(10) println("Welcome to Earth")