@@ -5,14 +5,16 @@ import java.lang.NumberFormatException
5
5
import kotlin.IllegalArgumentException
6
6
7
7
/* * note that package name does not have to match path on disk
8
- * default scope is public, other scopes are internal (module), private, protected
8
+ * default scope is public, other scopes are internal (module), private, protected.
9
+ * A module is everything that is in a single compile unit
10
+ *
9
11
* TODO
10
12
* 1. Destructuring https://kotlinlang.org/docs/reference/multi-declarations.html
11
13
*/
12
14
13
15
fun main () {
14
- functions()
15
16
someVariableStuff()
17
+ functions()
16
18
conditionals()
17
19
loops()
18
20
nullableStuff()
@@ -25,7 +27,8 @@ fun main() {
25
27
* to other functions, and returned as a result from higher order functions. They do not need to be defined
26
28
* in a class scope.
27
29
*
28
- * expression body vs
30
+ * if a function has a 'void' return type, in kotlin use 'Unit'. The compiler will implicit return 'Unit' if not declared
31
+ * expression body vs block body
29
32
*/
30
33
fun functions () {
31
34
println (" \n >>>>> Some functions" )
@@ -88,23 +91,36 @@ fun doSomethngWithLambdas() {
88
91
89
92
/* *
90
93
* VARIABLES
91
- * types do not need to be specified, they can be determined at time of assignment. You can explicitly define types tho
92
- * once a type has been set, it can't be chanved, ie a val/var set to a string cannot be assigned to an int
93
- * everything is an object, and properties are references
94
+ * https://kotlinlang.org/docs/reference/basic-types.html
95
+ *
96
+ * For the most part from a dev view, everything is an object. At runtime things may have a primitive value, but we always
97
+ * have access to member functions to these types (ie int is always Int)
98
+ * variables are a reference
99
+ * you can have immutable references (val) and mutable references (var)
100
+ * Types do not need to be specified, they can be determined at time of assignment, ie inferred.
101
+ * You can explicitly declare types if you want.
102
+ * Once a type has been set, it can't be changed, ie a var set to a string cannot be assigned to an int
103
+ *
104
+ * The use of val is encouraged. Immutability is default for a lot of things
94
105
*
95
- * using 'val' means once assigned a value reference, it cannot change (immutable)
96
- * using 'var' means it is a mutable reference
106
+ * Numbers are Byte (8bits), Short (16), Int (32) and Long(64). all are signed
107
+ * Floating point numbers are Float (32) and Double(64). Decimals are by default Double. If you need larger precision
108
+ * or money (or scienece stuff) use BigDecimal
109
+ * Others are String,, Char (16) and Boolean (8)
97
110
*/
98
111
fun someVariableStuff () {
99
112
println (" \n >>>>> Variables" )
100
113
101
- val implicitInt = 32 // type is implicit Int, val means the reference can only be set once
114
+ val implicitInt = 32_000 // type is implicit Int, val means the reference can only be set once, not underscore allowed to make more readable
115
+ val numWitDecimals = 32.0132 // type is Double
116
+ val floatWithDecimal = 32.013f // trailing 'f' makes it a Float
117
+ println (" some decimals ${numWitDecimals.javaClass} =$numWitDecimals and ${floatWithDecimal.javaClass} is $floatWithDecimal " )
102
118
// implicitInt = 33 //compile time fail uncomment to show
103
119
// implicitInt = "string" //compile time reassign val error AND type mismatch error
104
120
var varInt = implicitInt // var has mutable reference, but once assigned a type it can't change
105
- println (" varInt = $varInt " )
121
+ println (" varInt = $varInt values equal: ${varInt == implicitInt} " ) // string interpolation, property so no {}, note no .equals
106
122
// varInt = "blah" //type mismatch, once a var has a type assigned it can't change
107
- varInt = implicitInt * 2 // var has mutable reference
123
+ varInt = implicitInt * 2 // var has mutable reference, so we can reassign it
108
124
println (" after changing reference varInt = $varInt " )
109
125
println (" varInt with ++ = ${++ varInt} " ) // need {} to get ++ to work, ie an expression
110
126
println (" varInt after increment = $varInt " )
@@ -119,7 +135,7 @@ fun ifAsAnExpressionMaxValue(a: Int, b: Int) = if (a > b) a else b
119
135
fun ifExpressionReturnsAValueWithBlocks (a : Int , b : Int ): Int =
120
136
if (a > b) { // if this was a block body, return requred here on the if since if is an expression. return is required for block functions vs expression body
121
137
println (" if: a is bigger $a > $b " )
122
- a // return a
138
+ a // return a, 'return' not required since this is an expression body syntax, ie no {} for the block
123
139
} else {
124
140
println (" if: b is bigger $b > $a " )
125
141
b // return b
@@ -146,7 +162,7 @@ fun someFunWithEquals() {
146
162
println (" Showing equals" )
147
163
val personOne = Person (" blah" , " Johnny" , " Walker" )
148
164
val personTwo = Person (" blah" , " Johnny" , " Walker" )
149
- println (" using == in place of .equals structural equality: ${personOne == personTwo} " )
165
+ println (" using == in place of .equals structural equality: ${personOne == personTwo} " ) // == in place of .equals
150
166
println (" using === in place of .equals refernentioal equality: ${personOne == = personTwo} " )
151
167
}
152
168
0 commit comments