Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajeev Kumar Singh committed Jan 19, 2018
0 parents commit d6d7666
Show file tree
Hide file tree
Showing 15 changed files with 543 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
*.iml
37 changes: 37 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Kotlin Tutorials

### Kotlin Basics

* [Overview and Setup](https://www.callicoder.com/categories/kotlin/)

* [Writing your first Kotlin Program](https://www.callicoder.com/kotlin-introduction-hello-world/)

* [Variables and Data Types](https://www.callicoder.com/kotlin-variables-data-types/)

* [Kotlin Operators](https://www.callicoder.com/kotlin-operators/)

* [Kotlin Control Flow: if and when expressions, for and while loops](https://www.callicoder.com/kotlin-control-flow/)

* [Nullable Types and Null Safety in Kotlin](https://www.callicoder.com/kotlin-nullable-types-null-safety/)


### Kotlin Functions

* [Kotlin Functions, Default and Named Arguments, Varargs and Function Scopes](https://www.callicoder.com/kotlin-functions/)

* [Kotlin Infix Notation - Make function calls more intuitive](https://www.callicoder.com/kotlin-infix-notation/)


### Kotlin OOP

* [Classes, Objects, Constructors and Initializers](https://www.callicoder.com/kotlin-classes-objects-constructors-initializers/)

* [Properties, Backing Fields, Getters and Setters](https://www.callicoder.com/kotlin-properties-backing-fields-getters-setters/)

* [Kotlin Inheritance, Overriding Methods, and Properties](https://www.callicoder.com/kotlin-inheritance/)

* [Kotlin Abstract Classes](https://www.callicoder.com/kotlin-abstract-classes/)

* [Introduction to Data Classes in Kotlin](https://www.callicoder.com/kotlin-data-classes/)

* [Kotlin Type Checks and Smart Casts](https://www.callicoder.com/kotlin-type-checks-smart-casts/)
6 changes: 6 additions & 0 deletions src/t1_hello_world/Hello.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package t1_hello_world

// Kotlin Hello World Program
fun main(args: Array<String>) {
println("Hello, World!")
}
41 changes: 41 additions & 0 deletions src/t2_variables/Variables.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package t2_variables

fun main(args: Array<String>) {

/*
Declaring Variables
*/

// Immutable variable (val)
val name = "Bill Gates"
// name = "Satoshi Nakamoto" Error: Val cannot be reassigned

// Mutable variable (var)
var country = "USA"
country = "India" // Works

println("$name, $country")



//===========================

/*
Type inference
*/

val greeting = "Hello, World" // type inferred as `String`
val year = 2018 // type inferred as `Int`


// Explicitly defining the type of variables
val myStr: String = "Hello"
val myInt: Int = 20


// Type declaration is mandatory here, since the variable is not initialized at the time of declaration
var language: String
language = "French"

println("$greeting $year $myStr $myInt $language")
}
30 changes: 30 additions & 0 deletions src/t3_basic_types/Arrays.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package t3_basic_types

fun main(args: Array<String>) {

// Creating Arrays
var numbers = arrayOf(1, 2, 3, 4, 5)
var animals = arrayOf("Cat", "Dog", "Lion", "Tiger")

var mixedArray = arrayOf(1, true, 3, "Hello", 'A') // Works and creates an array of Objects

var numArray = arrayOf<Int>(1, 2, 3, 4) // Enforcing Type



// Array Indexing
val myDoubleArray = arrayOf(4.0, 6.9, 1.7, 12.3, 5.4)
val firstElement = myDoubleArray[0]
val lastElement = myDoubleArray[myDoubleArray.size - 1]



// Primitive Arrays
val myCharArray = charArrayOf('K', 'O', 'T') // CharArray (corresponds to Java 'char[]')
val myIntArray = intArrayOf(1, 3, 5, 7) // IntArray (corresponds to Java 'int[]')



// Creating Arrays using Array() constructor
var mySquareArray = Array(5, {i -> i * i}) // [0, 1, 4, 9, 16]
}
22 changes: 22 additions & 0 deletions src/t3_basic_types/NumericTypes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package t3_basic_types

fun main(args: Array<String>) {
// Kotlin Numeric Types Examples
val myByte: Byte = 10
val myShort: Short = 125

val myInt = 1000
val myLong = 1000L // The suffix 'L' is used to specify a long value

val myFloat = 126.78f // The suffix 'f' or 'F' represents a Float
val myDouble = 325.49

// Use underscores to make numeric values more readable
val hundredThousand = 100_000
val oneMillion = 1_000_000

val myHexa = 0x0A0F // Hexadecimal values are prefixed with '0x' or '0X'
val myBinary = 0b1010 // Binary values are prefixed with '0b' or '0B'

println("$myByte $myShort $myInt $myLong $myFloat $myDouble $hundredThousand $oneMillion $myHexa $myBinary")
}
23 changes: 23 additions & 0 deletions src/t3_basic_types/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package t3_basic_types

fun main(args: Array<String>) {
// Delcaring a String
var myStr = "Hello World"


// String Indexing
var name = "John"
var firstCharInName = name[0] // 'J'
var lastCharInName = name[name.length - 1] // 'n'


// Escaped and Raw String
var myEscapedString = "Hello Reader,\nWelcome to my Blog"

var myMultilineRawString = """
The Quick Brown Fox
Jumped Over a Lazy Dog.
"""

println("$name, $firstCharInName, $lastCharInName, $myEscapedString, $myMultilineRawString")
}
15 changes: 15 additions & 0 deletions src/t3_basic_types/TypeConversions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package t3_basic_types

fun main(args: Array<String>) {
val myInt = 100
val myLong = myInt.toLong() // Explicitly converting 'Int' to 'Long'

val doubleValue = 176.80
val intValue = doubleValue.toInt() // 176

val anotherInt = 1000
anotherInt.toString() // "1000"

val str = "1000"
val intValueofStr = str.toInt()
}
39 changes: 39 additions & 0 deletions src/t4_operators/NumericOperations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package t4_operators

fun main(args: Array<String>) {
var a = 10
var b = 20
var c = ((a + b) * ( a + b))/2 // 450

var isALessThanB = a < b // true

a++ // a now becomes 11
b += 5 // b equals to 25 now


// =======================


// Operators are internally converted to method calls
var x = 4
var y = 5

println(x + y)

// equivalent to
println(x.plus(y))


// ======================


// Bitwise Operators
1 shl 2 // Equivalent to 1.shl(2), Result = 4
16 shr 2 // Result = 4
2 and 4 // Result = 0
2 or 3 // Result = 3
4 xor 5 // Result = 1
4.inv() // Result = -5


}
13 changes: 13 additions & 0 deletions src/t4_operators/StringOperations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package t4_operators

fun main(args: Array<String>) {
// String Concatenation
var firstName = "Rajeev"
var lastName = "Singh"
var fullName = firstName + " " + lastName // "Rajeev Singh"

// String Interpolation
var a = 12
var b = 18
println("Avg of $a and $b is equal to ${ (a + b)/2 }")
}
64 changes: 64 additions & 0 deletions src/t5_conditionals/IfElse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package t5_conditionals

fun main(args: Array<String>) {
// If Statement
var n = 34
if(n % 2 == 0) {
println("$n is even")
}

// The curly braces are optional if the body of if statement contains a single line
if(n % 2 == 0) println("$n is even")


// =======================


// If-Else Statement

var a = 32
var b = 55

if(a > b) {
println("max($a, $b) = $a")
} else {
println("max($a, $b) = $b")
}

// =======================

// Using If as an expression
var max = if(a > b) a else b
println("max($a, $b) = $max")


// If-Else branches with block bodies
max = if(a > b) {
println("$a is greater than $b")
a
} else {
println("$a is less than or equal to $b")
b
}
println("max($a, $b) = $max")


// ========================

// If Else If Chain
var age = 17
if(age < 12) {
println("Child")
} else if (age in 12..17) {
println("Teen")
} else if (age in 18..21) {
println("Young Adult")
} else if (age in 22..30) {
println("Adult")
} else if (age in 30..50) {
println("Middle Aged")
} else {
println("Old")
}

}
Loading

0 comments on commit d6d7666

Please sign in to comment.