Skip to content

Commit d6d7666

Browse files
author
Rajeev Kumar Singh
committed
Initial Commit
0 parents  commit d6d7666

File tree

15 files changed

+543
-0
lines changed

15 files changed

+543
-0
lines changed

.gitignore

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

Readme.md

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy