Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d6d7666

Browse filesBrowse files
author
Rajeev Kumar Singh
committed
Initial Commit
0 parents  commit d6d7666
Copy full SHA for d6d7666

File tree

Expand file treeCollapse file tree

15 files changed

+543
-0
lines changed
Filter options
Expand file treeCollapse file tree

15 files changed

+543
-0
lines changed

‎.gitignore

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
*.iml

‎Readme.md

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Kotlin Tutorials
2+
3+
### Kotlin Basics
4+
5+
* [Overview and Setup](https://www.callicoder.com/categories/kotlin/)
6+
7+
* [Writing your first Kotlin Program](https://www.callicoder.com/kotlin-introduction-hello-world/)
8+
9+
* [Variables and Data Types](https://www.callicoder.com/kotlin-variables-data-types/)
10+
11+
* [Kotlin Operators](https://www.callicoder.com/kotlin-operators/)
12+
13+
* [Kotlin Control Flow: if and when expressions, for and while loops](https://www.callicoder.com/kotlin-control-flow/)
14+
15+
* [Nullable Types and Null Safety in Kotlin](https://www.callicoder.com/kotlin-nullable-types-null-safety/)
16+
17+
18+
### Kotlin Functions
19+
20+
* [Kotlin Functions, Default and Named Arguments, Varargs and Function Scopes](https://www.callicoder.com/kotlin-functions/)
21+
22+
* [Kotlin Infix Notation - Make function calls more intuitive](https://www.callicoder.com/kotlin-infix-notation/)
23+
24+
25+
### Kotlin OOP
26+
27+
* [Classes, Objects, Constructors and Initializers](https://www.callicoder.com/kotlin-classes-objects-constructors-initializers/)
28+
29+
* [Properties, Backing Fields, Getters and Setters](https://www.callicoder.com/kotlin-properties-backing-fields-getters-setters/)
30+
31+
* [Kotlin Inheritance, Overriding Methods, and Properties](https://www.callicoder.com/kotlin-inheritance/)
32+
33+
* [Kotlin Abstract Classes](https://www.callicoder.com/kotlin-abstract-classes/)
34+
35+
* [Introduction to Data Classes in Kotlin](https://www.callicoder.com/kotlin-data-classes/)
36+
37+
* [Kotlin Type Checks and Smart Casts](https://www.callicoder.com/kotlin-type-checks-smart-casts/)

‎src/t1_hello_world/Hello.kt

Copy file name to clipboard
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package t1_hello_world
2+
3+
// Kotlin Hello World Program
4+
fun main(args: Array<String>) {
5+
println("Hello, World!")
6+
}

‎src/t2_variables/Variables.kt

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package t2_variables
2+
3+
fun main(args: Array<String>) {
4+
5+
/*
6+
Declaring Variables
7+
*/
8+
9+
// Immutable variable (val)
10+
val name = "Bill Gates"
11+
// name = "Satoshi Nakamoto" Error: Val cannot be reassigned
12+
13+
// Mutable variable (var)
14+
var country = "USA"
15+
country = "India" // Works
16+
17+
println("$name, $country")
18+
19+
20+
21+
//===========================
22+
23+
/*
24+
Type inference
25+
*/
26+
27+
val greeting = "Hello, World" // type inferred as `String`
28+
val year = 2018 // type inferred as `Int`
29+
30+
31+
// Explicitly defining the type of variables
32+
val myStr: String = "Hello"
33+
val myInt: Int = 20
34+
35+
36+
// Type declaration is mandatory here, since the variable is not initialized at the time of declaration
37+
var language: String
38+
language = "French"
39+
40+
println("$greeting $year $myStr $myInt $language")
41+
}

‎src/t3_basic_types/Arrays.kt

Copy file name to clipboard
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package t3_basic_types
2+
3+
fun main(args: Array<String>) {
4+
5+
// Creating Arrays
6+
var numbers = arrayOf(1, 2, 3, 4, 5)
7+
var animals = arrayOf("Cat", "Dog", "Lion", "Tiger")
8+
9+
var mixedArray = arrayOf(1, true, 3, "Hello", 'A') // Works and creates an array of Objects
10+
11+
var numArray = arrayOf<Int>(1, 2, 3, 4) // Enforcing Type
12+
13+
14+
15+
// Array Indexing
16+
val myDoubleArray = arrayOf(4.0, 6.9, 1.7, 12.3, 5.4)
17+
val firstElement = myDoubleArray[0]
18+
val lastElement = myDoubleArray[myDoubleArray.size - 1]
19+
20+
21+
22+
// Primitive Arrays
23+
val myCharArray = charArrayOf('K', 'O', 'T') // CharArray (corresponds to Java 'char[]')
24+
val myIntArray = intArrayOf(1, 3, 5, 7) // IntArray (corresponds to Java 'int[]')
25+
26+
27+
28+
// Creating Arrays using Array() constructor
29+
var mySquareArray = Array(5, {i -> i * i}) // [0, 1, 4, 9, 16]
30+
}

‎src/t3_basic_types/NumericTypes.kt

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package t3_basic_types
2+
3+
fun main(args: Array<String>) {
4+
// Kotlin Numeric Types Examples
5+
val myByte: Byte = 10
6+
val myShort: Short = 125
7+
8+
val myInt = 1000
9+
val myLong = 1000L // The suffix 'L' is used to specify a long value
10+
11+
val myFloat = 126.78f // The suffix 'f' or 'F' represents a Float
12+
val myDouble = 325.49
13+
14+
// Use underscores to make numeric values more readable
15+
val hundredThousand = 100_000
16+
val oneMillion = 1_000_000
17+
18+
val myHexa = 0x0A0F // Hexadecimal values are prefixed with '0x' or '0X'
19+
val myBinary = 0b1010 // Binary values are prefixed with '0b' or '0B'
20+
21+
println("$myByte $myShort $myInt $myLong $myFloat $myDouble $hundredThousand $oneMillion $myHexa $myBinary")
22+
}

‎src/t3_basic_types/Strings.kt

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package t3_basic_types
2+
3+
fun main(args: Array<String>) {
4+
// Delcaring a String
5+
var myStr = "Hello World"
6+
7+
8+
// String Indexing
9+
var name = "John"
10+
var firstCharInName = name[0] // 'J'
11+
var lastCharInName = name[name.length - 1] // 'n'
12+
13+
14+
// Escaped and Raw String
15+
var myEscapedString = "Hello Reader,\nWelcome to my Blog"
16+
17+
var myMultilineRawString = """
18+
The Quick Brown Fox
19+
Jumped Over a Lazy Dog.
20+
"""
21+
22+
println("$name, $firstCharInName, $lastCharInName, $myEscapedString, $myMultilineRawString")
23+
}

‎src/t3_basic_types/TypeConversions.kt

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package t3_basic_types
2+
3+
fun main(args: Array<String>) {
4+
val myInt = 100
5+
val myLong = myInt.toLong() // Explicitly converting 'Int' to 'Long'
6+
7+
val doubleValue = 176.80
8+
val intValue = doubleValue.toInt() // 176
9+
10+
val anotherInt = 1000
11+
anotherInt.toString() // "1000"
12+
13+
val str = "1000"
14+
val intValueofStr = str.toInt()
15+
}

‎src/t4_operators/NumericOperations.kt

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package t4_operators
2+
3+
fun main(args: Array<String>) {
4+
var a = 10
5+
var b = 20
6+
var c = ((a + b) * ( a + b))/2 // 450
7+
8+
var isALessThanB = a < b // true
9+
10+
a++ // a now becomes 11
11+
b += 5 // b equals to 25 now
12+
13+
14+
// =======================
15+
16+
17+
// Operators are internally converted to method calls
18+
var x = 4
19+
var y = 5
20+
21+
println(x + y)
22+
23+
// equivalent to
24+
println(x.plus(y))
25+
26+
27+
// ======================
28+
29+
30+
// Bitwise Operators
31+
1 shl 2 // Equivalent to 1.shl(2), Result = 4
32+
16 shr 2 // Result = 4
33+
2 and 4 // Result = 0
34+
2 or 3 // Result = 3
35+
4 xor 5 // Result = 1
36+
4.inv() // Result = -5
37+
38+
39+
}

‎src/t4_operators/StringOperations.kt

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package t4_operators
2+
3+
fun main(args: Array<String>) {
4+
// String Concatenation
5+
var firstName = "Rajeev"
6+
var lastName = "Singh"
7+
var fullName = firstName + " " + lastName // "Rajeev Singh"
8+
9+
// String Interpolation
10+
var a = 12
11+
var b = 18
12+
println("Avg of $a and $b is equal to ${ (a + b)/2 }")
13+
}

‎src/t5_conditionals/IfElse.kt

Copy file name to clipboard
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package t5_conditionals
2+
3+
fun main(args: Array<String>) {
4+
// If Statement
5+
var n = 34
6+
if(n % 2 == 0) {
7+
println("$n is even")
8+
}
9+
10+
// The curly braces are optional if the body of if statement contains a single line
11+
if(n % 2 == 0) println("$n is even")
12+
13+
14+
// =======================
15+
16+
17+
// If-Else Statement
18+
19+
var a = 32
20+
var b = 55
21+
22+
if(a > b) {
23+
println("max($a, $b) = $a")
24+
} else {
25+
println("max($a, $b) = $b")
26+
}
27+
28+
// =======================
29+
30+
// Using If as an expression
31+
var max = if(a > b) a else b
32+
println("max($a, $b) = $max")
33+
34+
35+
// If-Else branches with block bodies
36+
max = if(a > b) {
37+
println("$a is greater than $b")
38+
a
39+
} else {
40+
println("$a is less than or equal to $b")
41+
b
42+
}
43+
println("max($a, $b) = $max")
44+
45+
46+
// ========================
47+
48+
// If Else If Chain
49+
var age = 17
50+
if(age < 12) {
51+
println("Child")
52+
} else if (age in 12..17) {
53+
println("Teen")
54+
} else if (age in 18..21) {
55+
println("Young Adult")
56+
} else if (age in 22..30) {
57+
println("Adult")
58+
} else if (age in 30..50) {
59+
println("Middle Aged")
60+
} else {
61+
println("Old")
62+
}
63+
64+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.