https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
a

Alin B.

07/10/2018, 7:47 AM
Hey all. I am trying to see what Kotlin is about and since I come from java one thing that I see is the way Nulls are handled. Of course, the habit for having null default values and != null check is strong on my end and the first step would be to better understand how to program without having the nulls in mind. So for starters, having a class
class Person(val firstName: String, val lastName: String, var age: Int)
and avoiding null means that whenever I instantiate it, I need to pass default values? So maybe have firstName="", lastName="" and age=0?
l

Lucas Ł

07/10/2018, 7:50 AM
you can define default values (for `var`s and `val`s) like so:
class Person(val firstName: String="", val lastName: String="", var age: Int=0)
and you don't need to set them when instantiating/using the constructor, for `val`s however keep in mind they are
final
👍 1
g

gildor

07/10/2018, 7:51 AM
Kotlin do not force you to avoid nulls, I think it’s wrong statement. Yeah, use non-null types is more pleasant, but nulls still welcome and best way to handle many cases. Also Kotlin provides syntactic sugar to handle nullable types, like safe call operator, elvis operator, smart cast What Kotlin really does: has nullable types and force you to handle them in a safe way So I would say: use non-null types if it make sense for your use case and use nulls if it also make sense. For some cases like primitives or String I still use almost exclusively non-null types, because it’s more efficient for primitives and string has good default value - empty string,
👍 4
👆 3
a

Alin B.

07/10/2018, 7:56 AM
Thank you very much for your details. My thought is not get starting on the wrong foot with kotlin. Because for instance I could have used default values in java also so that would avoid a lot of NPE, but I haven't done it and now I wonder why...
But having a "" for String, doesn't mean that most likely I'll need to check if tat string is not "" so I can do something with it? isn't like replacing != null with !="" ?
g

gildor

07/10/2018, 8:00 AM
It depends on your case. If you want to somehow define absence of value, just use nulls, no need to use default empty string. If you just want to avoid NPE, use default value empty string In most cases empy string is just fine and no need to check for empty string
but I haven’t done it and now I wonder why...
Because it’s a lot of boilerplate code, for class constructors or for method arguments. Also java doesn’t support default method params, so everyting become even worse for a method with many nullable params, because you have to overload this method to make API more pleasant
👍 2
a

Alin B.

07/10/2018, 8:03 AM
Indeed, it makes sense what you are saying, Java has a lot more "ceremony" around its classes.
g

gildor

07/10/2018, 8:05 AM
again, there is no universal advice, use nullable or non-nullable types depending on your use case and choose what is better for your particular case null or default value.
a

Alin B.

07/10/2018, 8:05 AM
I have to admit I am a little afraid to dive into this since I also need to learn MVVM and the arch components, but hopefully it will not be a hard thing to get familiar with. I've watched some video demos and Kotlin looks really cool. One other thing I've seen is that
var
should only be used if really needed and stick with
val
instead. What would be the reason for this?
g

gildor

07/10/2018, 8:06 AM
because immutable always better than mutable 🙂
if some variable/property initialised only once why do you want to declare it as var?
a

Alin B.

07/10/2018, 8:08 AM
probably this is equivalent with
final
in java?
g

gildor

07/10/2018, 8:08 AM
also, one more note about case with empty string. If in some case you want to check for empty string you still should do this even for nullable type, there is no way to avoid it
yes, it’s like final and on JVM uses final modifer in bytecode
a

Alin B.

07/10/2018, 8:10 AM
What if a class has a
List<Items>
as field. that should also be declared as
new empy List
so it's not null?
g

gildor

07/10/2018, 8:11 AM
just use
emptyList<Items>()
as default value
but again, it’s not a requirement, everything depends on case, Kotlin even provide extensions to idiomatically handle nullable lists:
val list: List<Items>? = null
list.orEmpty().doSomething()
a

Alin B.

07/10/2018, 8:14 AM
@gildor thank you so much for your time and right on point answers! I really appreciate it. Do you think it's work switching to Kotlin from Java considering the future?
g

gildor

07/10/2018, 8:19 AM
Our team started to use Kotlin almost 2.5 years ago, much earlier than Kotlin became official language for Android and never regret about this decision. But now I don’t see any reason to avoid Kotlin. On Android it’s not even future, it’s present.
l

Lucas Ł

07/10/2018, 8:22 AM
If you look at it objectively, you obviously still need to know both java and kotlin, because most companies maintain old code bases, while writing new stuff in Kotlin, just because it's better on so many levels. Java-Kotlin interoperability basically shuts down any uncertainties for a lot of dev teams.
g

gildor

07/10/2018, 8:27 AM
Yeah, sure, I agree with this. Depends what is “switching to Kotlin” for you. You cannot work as JVM developer without knowledge of Java
a

Alin B.

07/10/2018, 8:29 AM
I usually create Android projects for clients and usually from scratch so probably it's better to do them directly in kotlin. And also from what I saw, it's possible to do node.js in kotlin and that would also be a big plus for backend development.
g

gildor

07/10/2018, 8:30 AM
Yeah, sure. But my personal opinion, that if I would choose backend platform I would rater choose JVM than node.js.
a

Alin B.

07/10/2018, 8:39 AM
Thank you again for your time. I've found https://github.com/googlesamples/android-sunflower and this seems like a good place to dig into the projiect and see how they've matched the jetpack components and mvvm.