Skip to content

Commit f70c21c

Browse files
committed
minor cleanup prepping for demo, adding some notes and talking points
1 parent 78d7035 commit f70c21c

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

TODO.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#TODO stuff
2+
1. ccustom getter/setter i in classes
3+
2. backing fields

src/main/kotlin/me/kotlindemo/01_IntroToKotlin.kt

+29-13
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import java.lang.NumberFormatException
55
import kotlin.IllegalArgumentException
66

77
/** 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+
*
911
* TODO
1012
* 1. Destructuring https://kotlinlang.org/docs/reference/multi-declarations.html
1113
*/
1214

1315
fun main() {
14-
functions()
1516
someVariableStuff()
17+
functions()
1618
conditionals()
1719
loops()
1820
nullableStuff()
@@ -25,7 +27,8 @@ fun main() {
2527
* to other functions, and returned as a result from higher order functions. They do not need to be defined
2628
* in a class scope.
2729
*
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
2932
*/
3033
fun functions() {
3134
println("\n>>>>> Some functions")
@@ -88,23 +91,36 @@ fun doSomethngWithLambdas() {
8891

8992
/**
9093
* 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
94105
*
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)
97110
*/
98111
fun someVariableStuff() {
99112
println("\n>>>>> Variables")
100113

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")
102118
// implicitInt = 33 //compile time fail uncomment to show
103119
// implicitInt = "string" //compile time reassign val error AND type mismatch error
104120
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
106122
// 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
108124
println(" after changing reference varInt = $varInt")
109125
println(" varInt with ++ = ${++varInt}") //need {} to get ++ to work, ie an expression
110126
println(" varInt after increment = $varInt")
@@ -119,7 +135,7 @@ fun ifAsAnExpressionMaxValue(a: Int, b: Int) = if (a > b) a else b
119135
fun ifExpressionReturnsAValueWithBlocks(a: Int, b: Int): Int =
120136
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
121137
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
123139
} else {
124140
println(" if: b is bigger $b > $a")
125141
b //return b
@@ -146,7 +162,7 @@ fun someFunWithEquals() {
146162
println(" Showing equals")
147163
val personOne = Person("blah", "Johnny", "Walker")
148164
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
150166
println(" using === in place of .equals refernentioal equality: ${personOne === personTwo}")
151167
}
152168

0 commit comments

Comments
 (0)