-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rajeev Kumar Singh
committed
Jan 19, 2018
0 parents
commit d6d7666
Showing
15 changed files
with
543 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |
Oops, something went wrong.