Unit 1 Introduction To Android and Kotlin
Unit 1 Introduction To Android and Kotlin
and Kotlin
What is Kotlin ?
Kotlin = Java + Extra updated
new features.
• 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.
• It has built world-class IDEs like IntelliJ IDEA, PhpStorm,
Appcode, etc.
History of Kotlin
• Arithmetic operator
• Relation operator
• Assignment operator (Compound Assignments)
• Unary operator
• Logical operator
• Bitwise operator
Arithmetic Operators
Operator Description Expression Translate to
+ Addition a+b a.plus(b)
- Subtraction a-b a.minus(b)
* Multiply a*b a.times(b)
/ Division a/b a.div(b)
% Modulus a%b a.rem(b)
Relational Operators
Operato Expressi
Meaning Translate to
rs on
> greater than a>b a.compareTo(b) > 0
greater than
>= a >= b a.compareTo(b) >= 0
or equal to
less than or
<= a <= b a.compareTo(b) <= 0
equal to
a?.equals(b) ?: (b ===
== is equal to a == b
null)
!(a?.equals(b) ?: (b ===
!= not equal to a != b
Note: ? means it could benull))
NULL>0
Assignment Operators
Operato Expressi
Translate to
rs on
+= a = a + b a.plusAssign(b) > 0
a.minusAssign(b) <
-= a=a–b
0
a.timesAssign(b)>=
*= a=a*b
0
/= a=a/b a.divAssign(b) <= 0
%= a = a % b a.remAssign(b)
Unary Operators
if (celsius != null) {
// Function to convert Fahrenheit to println("$celsius°C is equal to $
Celsius {CtoF(celsius)}°F")
fun FtoC(fahrenheit: Double): Double } else {
{ println("Invalid input for Celsius.")
return (fahrenheit - 32) * 5 / 9 }
}
if (fahrenheit != null) {
println("$fahrenheit°F is equal to $
fun main() { {FtoC(fahrenheit)}°C")
// Read and convert input values } else {
safely println("Invalid input for Fahrenheit.")
}
val celsiusInput = readLine()
}
val fahrenheitInput = readLine()
Class in Kotlin
• A class is a blueprint
for creating objects.
• Kotlin classes are
declared using
keyword class.
• Kotlin class has a
class header which
specifies its type
parameters,
constructor etc.
class Student
{
fun result()
{
println("Pending
")
}
}
fun main()
{
var s1= Student()
s1.result()
}
Primary and Secondary Constructors
• The primary constructor • A secondary constructor
is a simple, concise way is an additional way to
to initialize a class. It’s create an object. It’s
defined right after the useful when you need to
class name. set up the object with
• The primary constructor different parameters or
is used when you want to extra logic.
set up properties as soon • The secondary
as the object is created. constructor is used when
you need more flexibility
in how objects are
created.
class Person {
var name:
String fun intro()
var age:Int {
println("my name is $name and
constructor(x: age is $age")
String,y:Int) { }
this.name = x }
this.age = y fun main()
} {
var a=Person("Riya",22)
constructor(x:String a.intro()
){ var b=Person()
this.name=x b.intro()
this.age=0 var c=Person("Heena")
} c.intro()
constructor() {
this.name="Ra }
hul"
this.age=32
}
Using Primary Constructor
import java.math.BigInteger