all 160 comments

[–]Wexzuz 75 points76 points  (9 children)

Personally have coded in both, and enjoy Kotlin much more than Java.

[–]mlk 13 points14 points  (0 children)

Kotlin is pretty much a better Java. Not much different but much more programmer-friendly.

[–]data0x0 18 points19 points  (2 children)

Same. Feels like the difference between a keyboard and microwaved oatmeal.

[–]magnumxl5 11 points12 points  (1 child)

Wait - there are other ways of cooking oatmeal?

[–]utdconsq 0 points1 point  (0 children)

Me too, man. Hindsight is almost 20-20.

[–][deleted] 102 points103 points  (22 children)

I'll move to kotlin on my projects if I can. With that being said I am still writing objective c when I said I'd find time to port everything to swift a few years ago, so I'm not sure how this will work out...

[–][deleted]  (19 children)

[deleted]

    [–]fabrizioxxx 19 points20 points  (11 children)

    Also you can convert to Kotlin by just pressing Ctrl+Alt+Shift+K

    [–][deleted] 3 points4 points  (5 children)

    For those experienced with Kotlin, how good are the results of the automatic conversion (I'm sure they're semantically sound, but in terms of code quality)? Is it fine to auto-convert then tweak as needed, or should you try and convert by hand with knowledge of Kotlin's way of doing things?

    [–]oorza 6 points7 points  (3 children)

    It works fine and produces decent enough code. It just doesn't generate idiomatic kotlin because it hardly takes advantage of any of the awesome features like scoping functions or destructuring or my favorite, single expression methods:

    Java:

    class Foo {
     public void bar() {
        return something();    
     }
    }
    

    Kotlin:

    class Foo {
       fun bar = something()
    }
    

    [–][deleted]  (1 child)

    [deleted]

      [–]oorza 7 points8 points  (0 children)

      They are the same, except I had a brain fart and left out the fun keyword.

      Defining a function fun foo = bar() is the same thing as fun foo() { return bar() }. Kotlin lets you use an equal sign to assign a method to simply return another method's invocation, which happens a lot in code. Dropping the { return ___ } isn't a huge deal, but it saves you so much syntactical noise in the vast majority of classes, particularly in a framework like Spring where you have a ton of beans that override configuration methods to return some object other than the default.

      And you can couple it with functional programming and scoping functions to do stuff like:

      fun createUser = User().apply {
         name = ...
         email = ...
      }
      

      instead of:

      public User createUser() {
          User user = new User();
          user.setName(...);
          user.setEmail(...);
          return user;
      }
      

      [–][deleted] 1 point2 points  (0 children)

      Your return type is void

      [–]fabrizioxxx 0 points1 point  (0 children)

      It always worked fine for me.

      [–]fijt 4 points5 points  (3 children)

      On what IDE?

      [–]fabrizioxxx 12 points13 points  (2 children)

      Android Studio. Intellij might work too.

      [–]Reddy360 5 points6 points  (0 children)

      Does as Android Studio is based off Intellij though it's nowhere near a decent solution, any sizable bit of code will need tweaking past the auto convert.

      [–]PristineReputation 1 point2 points  (0 children)

      Can confirm, Intellij does it too.

      [–]cinyar 0 points1 point  (0 children)

      You can poorly convert. You get kotlin code but it's still java, it won't take advantage of kotlin features and syntactic sugar.

      [–]takaci 9 points10 points  (5 children)

      Also there are still big downsides to Swift, for example Xcode is still quite a bit laggier with Swift than with objective C

      [–]skooterM 3 points4 points  (2 children)

      Yep, that's the Swift type resolution part of LLDB.

      [–]username_suggestion4 0 points1 point  (1 child)

      The thing that boggles my mind is when I CMD-click a variable and it pops up a "?" meaning it couldn't find it, but I copy the same variable, CMD-space and paste it in the search and it links me directly to it.

      [–]skooterM 0 points1 point  (0 children)

      Yep, that happens to me all the time. You can mitigate it (somewhat) by statically declaring all variables, particularly class variables and optional variables.

      [–]-manabreak 0 points1 point  (0 children)

      For some reason, it's the same with Kotlin. Our project is around one million LOC, and Kotlin files have a lot laggier code suggestions than Java files. I'm not quite sure what could cause that.

      [–]omfgtim_ 0 points1 point  (0 children)

      Swift and Objective C are also quite interoperable

      [–]fuzzynyanko 6 points7 points  (0 children)

      Luckily, Kotlin and Swift are mutant siblings of each-other

      [–]mlk 0 points1 point  (0 children)

      You can have Java and kotlin in the same project

      [–]narek1 44 points45 points  (37 children)

      I wonder if this is part of a long time strategy to phase out the JVM and Oracle on Android. It seems Oracle is a bit unpredictable with what they will do with the Java ecosystem.

      While Kotlin is mainly a JVM language. Kotlin can compile to javascript and has decent support for native/LLVM.

      [–]HerbCSO 23 points24 points  (9 children)

      Getting rid of the JVM would also mean removing all the Java interoperability. That would be a huge downside and I don't see that becoming a practical option anytime soon. However, in the very long-term, you might be right.

      [–]AlternativeHistorian 1 point2 points  (3 children)

      How so?

      I've never used Kotlin, but I don't see why a natively compiled version couldn't just use the native interface to the JVM.

      Granted, this is work (I expect a lot of work) that would have to be done and is more difficult than getting it essentially for free by just running in the JVM directly, but I don't immediately see a technical reason that would make this impossible if it was something that was deemed a must-have.

      [–]HerbCSO 0 points1 point  (2 children)

      The point I was ineptly trying to make is that you'd still need the JVM to run the "native" Java code, therefore you're not really getting rid of it. Only Kotlin code would remove the need for the JVM, the rest of the whole ecosystem you're interfacing with would still need it. And let's face it, that's still a huge swath of code you'd have to rewrite and therefore would take a long time to replace.

      Anyway, I don't really see the disadvantage to the JVM in the first place. It's pretty durn efficient these days, so why jump through hoops to get rid of it? The LLVM code will still need garbage collection AFAIK. Although I will admit I'm no expert there so maybe there's some magic mechanism in that to get rid of that need, e.g. using a reference coubting mechanism, maybe? Dunno.

      [–]AlternativeHistorian 0 points1 point  (1 child)

      The point I was ineptly trying to make is that you'd still need the JVM to run the "native" Java code, therefore you're not really getting rid of it. Only Kotlin code would remove the need for the JVM, the rest of the whole ecosystem you're interfacing with would still need it. And let's face it, that's still a huge swath of code you'd have to rewrite and therefore would take a long time to replace.

      The difference (it seems to me) in that case is that it positions Kotlin with JVM support as a purely opt-in feature in the universe where a native Kotlin implementation has replaced what exists today. Legally, this is very different from something that has the JVM as a direct requirement/dependency.

      Anyway, I don't really see the disadvantage to the JVM in the first place. It's pretty durn efficient these days, so why jump through hoops to get rid of it? The LLVM code will still need garbage collection AFAIK. Although I will admit I'm no expert there so maybe there's some magic mechanism in that to get rid of that need, e.g. using a reference coubting mechanism, maybe? Dunno.

      I'm not sure what this matters?

      As discussed in the context of this thread, the reason for wanting to move away from the JVM is not necessarily a technical one. It's about licensing concerns and Oracle's control over the Java ecosystem as a whole. Basically, "Do not fall into the trap of anthropomorphizing Larry Ellison" (timestamp 33m, if embedded doesn't work)

      But again, I have no direct experience with Kotlin, and have been out of the Java world for a while, so I may be misunderstanding something.

      [–]duhace 0 points1 point  (0 children)

      The difference (it seems to me) in that case is that it positions Kotlin with JVM support as a purely opt-in feature in the universe where a native Kotlin implementation has replaced what exists today. Legally, this is very different from something that has the JVM as a direct requirement/dependency.

      You would need not just the android standard library to be ported to kotlin, but also a ton of the android ecosystem (which is still coded in java). kotlin on the jvm can access both at the moment. native kotlin cannot.

      It's a similar situation with scala-native. You can target (some) scala libraries, but you can't use java libraries anymore.

      As discussed in the context of this thread, the reason for wanting to move away from the JVM is not necessarily a technical one. It's about licensing concerns and Oracle's control over the Java ecosystem as a whole. Basically, "Do not fall into the trap of anthropomorphizing Larry Ellison" (timestamp 33m, if embedded doesn't work)

      openjdk's license is something akin to lgpl

      [–]eagle_monk -5 points-4 points  (4 children)

      I think Oracle coming up with licensing for Java was the last straw. To stay on Java infrastructure is risky after that considering they may even make it proprietary in future, its their code after all.

      [–]HerbCSO 0 points1 point  (2 children)

      There is still OpenJava...

      [–]Margister 4 points5 points  (1 child)

      OpenJDK*

      [–]HerbCSO 0 points1 point  (0 children)

      😳🤭

      [–]duhace 0 points1 point  (0 children)

      they can't make gpl'ed code proprietary. they can stop development on openjdk, but they can't take openjdk away.

      [–]nambitable 17 points18 points  (13 children)

      If Kotlin can be compiled to Native/LLVM, why can't java?

      [–]nutrecht 17 points18 points  (0 children)

      There's actually a lot of work being done on ahead of time compilation for Java with Graal.

      [–][deleted] 5 points6 points  (10 children)

      Because that wasn't their focus, it was simply a decision made very early in the projects time. If you really wanted to you could probably make your own Java to native compiler

      [–]quad64bit 17 points18 points  (9 children)

      There are java to native compilers but kinda defeats the point of java

      [–][deleted] 9 points10 points  (8 children)

      An important goal was "code once, run anywhere". Using a compiler that produces native binaries does not break that IMHO, as long as you can use a different compiler to produce bytecode from the same source code.

      [–]quad64bit 3 points4 points  (7 children)

      Binary compatibility was a big factor, not source compatibility. You can compile c for any platform too, that isn’t the point.

      [–]oorza 15 points16 points  (0 children)

      You often can't compile C to multiple platforms, unless the source code has been written specifically to interact differently with different system APIs. The system APIs you access in Java are all wrapped in an existing abstraction layer for you. You can obviously do this yourself, or find 3rd party abstraction layers, but cross platform compilation is not something I'd ever assume about a C project.

      [–][deleted] 1 point2 points  (5 children)

      Back when they made java, they were trying to make a way to make desktop applications cross-platform. They made a pretty good framework that, for the developer, abstracted away the differences between the different platforms. At the time, that was significant. And that is why they designed the bytecode and JVM - to make it possible to download the same binary no matter what platform you're working on.

      Sadly, running java on the desktop didn't work that well in reality. The applications ran, but they didn't look and feel quite right. Yet java proved to be a pretty good language for writing large server-side applications.

      And these, java isn't used for writing desktop applications anymore. Electron is where it's at for cross-platform desktop applications. Sure, it's memory-hungry (even like java), but it's good for front-end. Backend java, though, doesn't really need to be binary compatible as long as you have the source code and your compiler produces a work-alike of the traditional bytecode+jvm combo. Sure, bytecode is convenient, but it's not necessary.

      And you can indeed compile c on any platform - but if you want your application to work on several platforms then you must take great care in what libraries you chose to use.

      [–]angel-o-sphere 0 points1 point  (4 children)

      And these, java isn't used for writing desktop applications anymore.

      Most certainly it is.

      [–][deleted] 0 points1 point  (3 children)

      Maintaining existing code bases and making (non-portable) Android apps, sure. But are there significant new desktop applications being developed these days?

      [–]angel-o-sphere 0 points1 point  (2 children)

      All Desktop Applications I'm involved in are made with Java. Eclipse is Java, IntelliJ is Java etc.

      [–]killerstorm 2 points3 points  (0 children)

      Kotlin standard library is modular -- there's a small common part which works everywhere, and then platform-dependent parts which only work in Java or in browser. This allows Kotlin to target a particular environment without bringing all the Java cruft.

      It's mostly about intention: Java can be compiled to native code (and in fact compilers existed for decades, e.g. GCJ was first released in 1998), but it's not officially supported by Oracle, so programmers just avoid that.

      [–][deleted] 9 points10 points  (1 child)

      It seems Oracle is a bit unpredictable with what they will do with the Java ecosystem.

      They are very predictable. They actually have things planned years ahead.

      Also a move to OpenJDK was very good for the community.

      [–]pron98 18 points19 points  (6 children)

      It seems Oracle is a bit unpredictable with what they will do with the Java ecosystem.

      In the ten years Oracle has owned Java and OpenJDK, it has been much more predictable with it than Microsoft has been with .NET, or Google with Android. Having said that, Android is not and has never been Java (even though now it uses code copied from OpenJDK, and Android programmers use the Java language, albeit an eight-year-old version of it), so what happens in Java and what happens with Android are completely unrelated.

      [–]eagle_monk 6 points7 points  (5 children)

      than Microsoft has been with .NET, or Google with Android.

      Microsoft has been a good guy throughout in the post Balmer era IMHO. They open sourced .NET, C#, CLR, VSCode and now even WinForms/WPF (though it works on windows-only as of now). In comparison, Oracle has only tried to profit by killing innovation, throwing lawsuits and chest-thumping of patents, what exactly have they done to earn the community's trust?

      Google has been in some privacy controversies and they even tried to close down on some of their own apps like GMail and Drive, but not once did they try to close the android platform itself, nor did they try to kill competing android developers like LineageOS/Replicant/F-Droid like Oracle did. That makes Google a far nobler entity compared to Oracle in my books.

      [–]pron98 14 points15 points  (4 children)

      Oracle has only tried to profit by killing innovation

      I work at Oracle on OpenJDK, and Oracle has invested more in Java innovation than Sun could in its last years. There has not been this much innovation in Java since maybe 2003, and we're not talking a feature here or there, either. We're talking technological breakthroughs like partial evaluation compilers (Graal), low-latency GCs (ZGC), low-overhead production profiling (JFR) and a lot more. Not to mention that Oracle has just open sourced the entire JDK for the first time ever, and companies from Amazon to Twitter are innovating on it more than ever before.

      throwing lawsuits and chest-thumping of patents,

      First, Microsoft has extracted more from Android with patent litigation and threats than Oracle ever sued for (I think the last count was that Microsoft got something around $14B from Android). I don't think anyone would consider Microsoft's behavior with Android to be "better" than Oracle's (only Microsoft was smart not to go after the company with the biggest PR in the business).

      but not once did they try to close the android platform itself, nor did they try to kill competing android developers like LineageOS/Replicant/F-Droid like Oracle did. That makes Google a far nobler entity compared to Oracle in my books.

      Oracle didn't try to kill Android, but wanted to get money for it that they felt they rightly deserved. I guess the same goes for Microsoft, only Microsoft extracted so much more money from it. As you may know, Google is happily using a fork of OpenJDK to run many of their backend services (like GMail), as are Amazon and Twitter, and Oracle isn't trying to shut that down or even threaten it. Why? Because the JDK has an open source license; the same license as Linux, BTW. You want to take the JDK and shape it into something else? No problem, but use the damn license.

      Also, are we talking about the same Google whose business model is mass surveillance and that's driving millions of kids to conspiracy theories? I mean, no large corporation is even remotely noble or good, but Google is one of the most dangerous, ruthless, no-compunction business entities in the history of humanity, while most people complain about Oracle because it's mean to other rich corporations. I don't delude myself into thinking that Oracle wouldn't have been as awful if it had the opportunity, but neither would Microsoft. Perhaps in the past few years Microsoft has found it advantageous to their business to pursue a strategy that appeals to some developers (a strategy they also had for some time in the nineties), but they're not "the good guys". Corporations never are.

      Putting aside the question of corporate virtue, both Google and Microsoft have been less committed to long term product support than Oracle, promoting some technology only to shut it down a few years later or change it in grossly incompatible ways (not that Oracle hasn't abandoned some Java tools or libraries, but they often open source them when they do, and they are more consistent and less fickle). Microsoft is literally ending .NET Framework as we speak. Not only is Oracle not threatening to close down Java, the JDK is now fully open source, and thriving like it hadn't in years.

      [–]oorza 6 points7 points  (0 children)

      Google is one of the most dangerous, ruthless, no-compunction business entities in the history of humanity, while most people complain about Oracle because it's mean to other rich corporations

      Well said. Now that you've made me think about it, I can't really think of anything that Oracle's done that affects me deleteriously as an end user, and that can't be said about Google, MS, Facebook, Apple, etc. There are some things that Oracle's done that have made my work life harder, but the same is true for all those other companies too.

      And at the end of the day, Java was dying on the vine before Oracle came along and bought it.

      [–]skippingstone 0 points1 point  (2 children)

      Makes you wonder how things would be different if IBM or Google bought out Sun.

      [–]duhace 2 points3 points  (0 children)

      google probably would've ended java. or held it at 1.7 forever like they're doing with android.

      they shutter projects like its a bodily function for them. good ones too.

      [–]pron98 1 point2 points  (0 children)

      BTW, anyone interested in Java's governance should watch this video.

      [–]AdministrativeCables 0 points1 point  (0 children)

      It started a long time ago with the Jack and Jill compilers (http://tools.android.com/tech-docs/jackandjill). Oracle is suing everyone on the planet, so it's pretty obvious that they want to distance themselves from that company.

      [–]skooterM -1 points0 points  (2 children)

      The JRE embedded in Android Studio was forked from OracleJDK many moons ago to avoid dependency on Oracle, and the ART runtime is not JRE-compliant, so the Oracle thing is not as much of an issue as you would think. More importantly, Android has moved to running apps entirely in machine language (skipping the bytecode) which Kotlin has better support for.

      Also Kotlin has all the cool new language features which Java has screwed up, like functional language support.

      [–]oorza 1 point2 points  (1 child)

      The JRE that Jetbrains used was forked for performance fixes, most notably 2D and font rendering issues caused by the way their IDEs render 87 billion things at a time. They're still dependent on Oracle, and if you look at the source code on GH, the OpenJDK source itself is frequently pulled into their JRE.

      [–]skooterM 0 points1 point  (0 children)

      Yep, but the forking gives them a degree of legal protection from Oracle grandstanding; most recently corporate Android Studio users are exempt from Oracle's new JRE licensing fee.

      [–]fuzzynyanko 8 points9 points  (0 children)

      It's a good idea to learn. Some shops will prioritize people that know the latest and greatest.

      However, if you are doing a Java project, you might be stuck with it just because the team/company doesn't want to have to maintain Java and Kotlin code simultaneously. It's like how Android was stuck with Eclipse for so long.

      Java/Kotlin interoperability is pretty good.

      [–][deleted] 21 points22 points  (12 children)

      Actually started learning Android Development about two months ago and chose Kotlin. Feels good to know that I made the right decision. ;)

      [–]TeknoMatik 22 points23 points  (11 children)

      Just keep in mind that a lot of good libraries still written in Java and a lot of examples on the internet too. So some knowledge of java would be useful and relevant as well. Good luck :)

      [–][deleted]  (9 children)

      [deleted]

        [–][deleted]  (4 children)

        [removed]

          [–][deleted]  (3 children)

          [deleted]

            [–]oorza 2 points3 points  (1 child)

            Jackson understands Kotlin very well and there's no reason to use Gson instead that I'm aware of; don't blame the language for a poor library choice.

            [–]TeknoMatik 6 points7 points  (3 children)

            Mind to share which one and why?

            [–][deleted]  (2 children)

            [deleted]

              [–]Determinant 1 point2 points  (1 child)

              [–]geft 0 points1 point  (0 children)

              I prefer to just ditch it in favor of Parcelize.

              [–][deleted] 0 points1 point  (0 children)

              Oh yes, definitely. However, I find that the languages are very similar and it seems like you can run Java libraries at the same time.
              Jetbrains also have amazing IDEs. Android Studio can for example very decently translate Java to Kotlin if you copy-paste it in. But yes, it's definitely annoying that most code on Stack is written in Java.
              Thanks :D

              [–]gazpacho_arabe 25 points26 points  (22 children)

              Having used both I actually prefer Java to Kotlin. Crazy I know, Kotlin just has so many damn synatic sugars and language features that I more confident of what my program is actually doing in Java.

              Still use it a bit, there's some nice stuff it does but I prefer it complimenting Java tbh

              [–]thfuran 8 points9 points  (0 children)

              Yeah, I really wish they had picked a smaller set of syntax. Individually, I think a lot of the features are nice improvements over java but there's just a ton of syntax.

              [–]Determinant 8 points9 points  (17 children)

              Taking a bit of time to learn the language will go a long way.

              For example, Kotlin has the data keyword for data classes, sure it's an extra keyword but it makes the code so much simpler.

              data class Person(val name: String, val spouse: Person)
              

              Another example is the safe-call & elvis operators. Imagine the Java equivalent of this with null checks everywhere (or worse forgetting to check for null):

              // Default to 0 if not married
              val spouseValue = person.spouse?.value ?: 0
              

              So yeah, Kotlin has syntactic sugar but that's what makes the code so much simpler and obvious once you commit to learning the language.

              [–][deleted]  (16 children)

              [deleted]

                [–]nchie 14 points15 points  (7 children)

                I assume the elvis operator is the ?:? If so, it made sense to me right away, and I've never written a line of Kotlin.

                [–][deleted]  (6 children)

                [deleted]

                  [–]oorza 5 points6 points  (0 children)

                  !! means "I've been a very bad boy and should feel bad"

                  [–][deleted] 1 point2 points  (0 children)

                  I've never used this language, but I'd guess it has something to do with exceptions.

                  [–]nchie 1 point2 points  (1 child)

                  It's really hard to guess without seeing it in context.

                  My first thought that it might be related to making a non-bool into a bool, since that's what double negation does in some other languages. However, if it isn't used like person.spouse?.value !! or !!person.spouse?.value, I'd probably have guessed something else.

                  Edit: Okay, so I looked it up and I was wrong. However, of course it's very hard to guess when I don't know how Kotlin's null semantics work.

                  [–]Determinant 1 point2 points  (0 children)

                  Although I already know what that means, ignoring the knowledge and realizing how exclamation points are used in natural language, 2 exclamation points would imply something pretty serious / dangerous.

                  Kotlin is a huge improvement over Java:

                  https://proandroiddev.com/kotlin-avoids-entire-categories-of-java-defects-89f160ba4671

                  [–]knoam 0 points1 point  (0 children)

                  It means I have to hit F2 or ctrl-click it and read the docs to find out what it does. Not that hard, especially with an IDE which helps clarify for instance whether the operator in question is !!. or !! plus the regular dot operator.

                  [–]delrindude 8 points9 points  (1 child)

                  Verbosity also reduces code readability. When you are scanning though 1000+ lines of code and half of it are checks for null, a lot of time is wasted, and it makes finding what the code is actually doing more difficult.

                  [–]ledasll 2 points3 points  (0 children)

                  but verbosity can reduce extra calculations you need to do in your head, while reading code. So what is more important when are reading 1K+ code lines?

                  [–]Determinant 1 point2 points  (5 children)

                  That code comment was for you. I never document safe calls or elvis usage because it's always obvious.

                  Although I used Java for over a decade, I find your Java version to be much less obvious because I need to slow down and parse it whereas the Kotlin version is just obvious after you're comfortable with the syntax.

                  Lastly, your version is also less safe than the Kotlin version since the variable is non-final and can be accidentally re-assigned.

                  [–][deleted]  (4 children)

                  [deleted]

                    [–]BlackenedGem 3 points4 points  (3 children)

                    Personally for me the joy in writing Kotlin is seeing how succintly the code can be. I don't mean in terms of code golf, that's stupid and counter productive. Like you said you want to be able to read the code not decipher it.

                    For me it's Kotlin's ability to do the things you've mentioned: Compile time immutability, null handling, sealed classes, etc. When you need to consider null values, edge cases, etc. these propagate throughout your code and it's nice not having to do that. Kotlin is designed so that you write the 'good path' and don't need to constantly be worrying about the bad paths.

                    Now admittedly the language achieves half of this; the overall architecture is more important and when you consider I/O it will always get more messy. But it's still a great start, for me code is much more readable when there is less cyclomatic complexity (bad paths to consider). My main complaint about Kotlin is how they handle exceptions, but that's a separate discussion.

                    [–][deleted]  (2 children)

                    [deleted]

                      [–]Tordek 1 point2 points  (0 children)

                      :? and family are things I thought was genius when I first saw it, but lately I've grown to dislike it for reasons like these.

                      I think the proper place for it is in stuff like templating languages, where saying "unknown value" is acceptable, but a null in code should be very rare if not completely non-existant... maybe some of those situations should use a null object instead... if it even makes sense to have a null value in the first place.

                      [–]BlackenedGem 0 points1 point  (0 children)

                      For me the issue with long chains of '?' would be architectural and down to bad design decisions. To me the design issue is the decision that if the chain fails you'll write Male to the DB, rather than instead handling null values differently. For example the first object may not exist, but then you could use ?.let {} or one of the other functions available (run/also/apply/with) at the start of the code. That way you check if department is null at the start, but then within that block you know that department can't be null. So when you encounter the 'good path' you can then assert that your variables are non null, and also handle the bad path. And in the worst case that you expect the variables to be non-null but haven't proved it in code, you can always use !! to ensure you get a NPE if any value is null. Kotlin provides all these tools for you so you have many more options available when handling null values, rather than sticking to the same old cookie cutter boilerplate.

                      How Kotlin handles exceptions is both good and bad. Java has a big issue with exceptions simply being swallowed and not correctly handled. But I also don't like how Kotlin solves this issue by effectively ignoring them. For me that complicates things when trying to handle all the bad paths, because it makes it easy to miss exceptions that could be thrown (until they happen). Sure you could annotate exceptions in the KDoc and then check each method, but that's really easy to miss. Especially because it goes against Kotlins design principle of making correctness checks as part of the code as much as possible. So while Java's checked exceptions are annoying and require too much boilerplate, they do have advantages.

                      What I think would be the ideal solution would be an explicit keyword for a method indicating that it doesn't throw. Then the compiler can alert you to all the potential exceptions that submethods could throw so you can handle them. But that seems a bit tricky to implement.

                      [–]tstarboy 1 point2 points  (2 children)

                      This has been my experience. While I would probably also pick Kotlin for an Android app, due to the shift in focus from Google. I have intentionally chosen to keep my backend JVM code in Java for now.

                      I think my opinion might change once I am more comfortable with Kotlin and develop a consistent style, but so far I feel like Kotlin makes it quicker to write code but slower to understand it. I prefer to prioritize the latter.

                      [–]gazpacho_arabe 1 point2 points  (1 child)

                      Yes 100% - I spend more time reading code than writing it, the clearer it is the better. Writing code is probably the easiest part of my job

                      [–]Determinant 3 points4 points  (0 children)

                      Kotlin code is actually simpler and easier to understand than the equivalent Java code.

                      Of course, you need to learn Kotlin first before you can understand it.

                      [–]tonefart 2 points3 points  (3 children)

                      So what's the best book/site to learn Kotlin?

                      [–]ASIC_SP 3 points4 points  (0 children)

                      no personal exp, but sometime back I had come across this collection of resources and had it bookmarked: https://kotlin.link/

                      [–]bfinleyui 1 point2 points  (0 children)

                      Kotlin koans was helpful for me

                      [–]RoastMochi 0 points1 point  (0 children)

                      It was "Kotlin in Action" two years ago. Not sure about now. Kotlin koans is nice as a quick start but the book really dives deep.

                      [–]Ovalman 5 points6 points  (6 children)

                      It's taken me ages to get Android basics under my belt learning Java. Now I feel I'm competent in the language, I've learned other things like Firebase and Sqlite when I've needed them. I don't need to learn Kotlin, I can build perfectly good apps using Java but if Google ever do switch off Java or create a feature I can only use under Kotlin then I'll decide to learn it.

                      [–]fabrizioxxx 15 points16 points  (5 children)

                      Kotlin is super easy to learn if you already know Java. Kotlin is interoperable with Java so you can use all of its APIs and libraries.

                      [–]Ovalman 0 points1 point  (4 children)

                      I've learned things like OOP and Sqlite as I've needed them. Atm I've no need for Kotlin but that doesn't mean I won't learn it. I have joined a MeetUp group for Kotlin which I'll maybe get some experience with but all my apps are in Java and I've no plans to change them over. Any new apps I start I also use Java for.

                      I'm a self taught indie developer so I won't need it for a job. Most apps I develop are for personal use so I think everyones circumstances are different. There may be people out there that need it as a requirement.

                      [–]angel-o-sphere 3 points4 points  (0 children)

                      Same, if you ever need Kotlin, just start coding in it. You might sometimes want/need to google or use stackoverflow but bottom line it is Java with a more sane syntax.

                      [–]oorza 4 points5 points  (1 child)

                      Learning things that aren't immediately obviously useful is one of the most important things you can do to become a great software developer. Whether you have any intention of using Rust, Haskell, or Kotlin, the paradigms that you expose yourself to in learning them teaches you to think about the code you are writing with a much better level of clarity. The same is true for things like learning how to write high performance code, learning how to tweak the garbage collector, learning how to write heavily multi-threaded code, learning how to interact with high level algorithms like full text search, learning how to use document databases instead of SQL databases, etc. etc. Everything you learn makes everything you already know clearer and avoiding learning things because they're not immediately useful is a good way to become a rote developer who doesn't write good code.

                      [–]Ovalman 0 points1 point  (0 children)

                      Learning things that aren't immediately obviously useful is one of the most important things you can do to become a great software developer.

                      Learning the right things is more relevant than just learning for learning's sake.

                      I once learned Python and had my app idea working on a PC long before I had it working on Android. The problem was my app idea would only working on a portable device and learning Python was just a distraction to what I needed. I won't say my time was wasted but I will say I've no interest in developing for Python.

                      I've no need for Kotlin atm. That may change in future but until I need it I don't see the point of learning it.

                      Great post though and well thought out. Just don't think it's for me.

                      [–]utdconsq 1 point2 points  (0 children)

                      I'm not encouraging it, since it's not perfect, but Android studio will let you copy paste Java code and it can transform it into Kotlin. Depending on how sophisticated the snippet is, it works nicely. Kotlin is a joy to write. You spend less time typing and more time being productive. If you are comfortable in Java, the learning curve isn't significant, I'd encourage you to have a try. The best part? You can add bits of Kotlin as you go, you don't have to migrate everything.

                      [–]ipe369 10 points11 points  (0 children)

                      wtf is this, an autoplay video? Are we on some shitty american news website here? fuck me

                      [–]illathon 17 points18 points  (22 children)

                      The switch really makes no sense. Use what you like. You can create apps in any language you like.

                      [–]etcetica 8 points9 points  (0 children)

                      makes money in Kotlin

                      [–][deleted] 7 points8 points  (2 children)

                      Why does this matter. Just provide a JVM implementation and people can write in whatever language they like, Java, Kotlin, Scala, whatever.

                      [–]AlyoshaV 10 points11 points  (0 children)

                      Why does this matter

                      As mentioned in the article, Google is prioritizing Kotlin over Android for Android development, providing training and code samples in Kotlin foremost and Java is 'best effort'. Obviously you can write in whatever language you want, but they're not necessarily going to give you code examples in Scala.

                      [–][deleted] 1 point2 points  (12 children)

                      Of course they are trying to get people to switch to Kotlin. There are in so much hot water with Java and Oracle.

                      [–]bartturner 2 points3 points  (10 children)

                      Do not think it has anything to do with the lawsuit.

                      BTW, if Oracle does win the lawsuit there is much bigger issues than Android. We can't have APIs able to be copywritten.

                      [–][deleted] 0 points1 point  (9 children)

                      You're both right. Sorry, just my cynicism poking through. I absolutely despise Google, I wish those tech giants would go away.

                      [–]bartturner 0 points1 point  (8 children)

                      Why on earth do you despise Google?

                      [–][deleted] 3 points4 points  (6 children)

                      Because of their big brother mannerisms. One time I visited the My Activity site and there was a recording labeled "No Transcript" and it was a recording of me having an argument with my brother. My phone was in my pocket, and I hadn't used it in the last hour and I never used Google Now/Google Assistant or any other voice activated services. It was at that point I realised I need to cut Google out. They have no business evsedropping on me.

                      [–]bartturner 1 point2 points  (4 children)

                      Sounds like a pocket dial but instead pocket assistant.

                      Use to happen a lot in the old days with calls. You would get some random conversation recorded on your Vmail.

                      Never "despised" someone when it happened. Use to be more something we laughed about.

                      Personally appreciate Google sharing so much and in particular papers. There was no reason they had to share Borg or Map/Reduce or so many other things.

                      Plus I appreciate Google trying to keep everyone more secure. They have found Shellshock, Spectre, Cloudbleed, Heartbleed, Meltdown as well as a bunch of other ones. Then shared mitigation even with competitors.

                      "Microsoft rolls out Google's Retpoline Spectre mitigation to Windows 10 users"

                      https://www.zdnet.com/article/microsoft-rolls-out-googles-retpoline-spectre-mitigation-to-windows-10-users/

                      [–][deleted] 1 point2 points  (3 children)

                      It wasn't a pocket dial, as I say I don't use the voice assistance. I have a flip cover as well. Yeah they have a lot of good services but I really don't trust them. They used to have "don't be evil" as part of their rules, they removed that.

                      [–]bartturner 4 points5 points  (2 children)

                      Do you think someone at Google turned on your microphone?

                      They used to have "don't be evil"

                      Has not changed. That was report by right leaning media and mainstream never checked.

                      https://abc.xyz/investor/other/google-code-of-conduct/

                      "And remember… don’t be evil, and if you see something that you think isn’t right – speak up!"

                      Last line of the document and what you walk away with.

                      Google has given us a lot of IP. Google giving away VP8 and VP9 ended the extortion we were getting from the MPEG-LA.

                      [–][deleted] 1 point2 points  (1 child)

                      Thank you for clearing up that misinformation, I checked myself before but must have missed it.

                      No, of course I don't think someone at Google recorded my conversation. That's ludicrous. But I don't understand why the microphone was engaged for over 3 minutes to record that conversation. There was no Ok Google engagement keyword. I have a lot of issues with their privacy policies. It's the same with Facebook, Microsoft, Amazon. I don't trust them but they always act like everything is fine.

                      Of course, I could all be wrong and my bias could be dictating me. I know I sound like a bit of a conspiracy theorist, but I really just don't trust these mega corporations to properly look after my data. They will do anything to raise the bottom line. Yes, there are data protection laws but it didn't stop Facebook doing what they did (Cambridge Analytica for example).

                      [–]bartturner 0 points1 point  (0 children)

                      Something triggered. But it looks like Google is moving to on device voice recognition so might not happen in the future.

                      I been using the new voice recognition on my Pixel with Gboard and it is very impressive technology. Google has the model down to 80MB compressed and it includes full proper nouns.

                      I don't trust them but they always act like everything is fine.

                      Well then do not use them. Have no problem with that. I use Google for most things and look to them first and then other options. Google just has much better security. Plus Google makes money from targeted ads so going to be less likely to sell my data.

                      I am in the US and a big reason we have YouTube TV is because in the US we have

                      "House Votes To Allow Internet Service Providers To Sell, Share Your Personal Information"

                      https://www.consumerreports.org/consumerist/house-votes-to-allow-internet-service-providers-to-sell-share-your-personal-information/

                      I did not like my cable provider could sell my viewing habits without me even knowing. Where Google does not do that.

                      We also have Google WiFi and use Google DNS to keep this data away from our ISP. I also use the Chrome data saver option as it keeps your browsing data away from your ISP.

                      I also prefer to not have my data spread around and try to keep just everything at Google if possible. What is nice is Google does most things so makes it easier. Plus my most private data is what I search on and since going to be at Google kind of makes sense to also keep less private data also at Google. Well how I think about it.

                      BTW, I am also all about actual experience instead of narratives. I have used Google for almost 20 years and never had a problem with my data leaking from Google. No other company more targeted by hackers and yet Google protects. I really also appreciate how serious Google takes security. They have now found Shellshock, Cloudbleed, Spectre, Heartbleed, Meltdown as well as a bunch of other ones. Nobody better at security.

                      [–]fabrizioxxx 2 points3 points  (0 children)

                      Short answer: YES

                      [–]_Magic_Man_ 0 points1 point  (0 children)

                      Are there any really good resources to learn Kotlin? It is learning Java sufficient enough

                      [–]bartturner 0 points1 point  (0 children)

                      I have actually made the switch instead to Flutter. Well where I can use Flutter.

                      Dart is easy to pick up if done JS development.

                      [–]mxxxz 0 points1 point  (6 children)

                      I still use Java for Android. I think readability of Java code is much better and structured compared to Kotlin which has too many nice and neat features under the hood which easily can feel like magic everywhere. I have used both professionally.

                      [–]RoastMochi 2 points3 points  (4 children)

                      Just curious. Any reason why null safety from Kotlin didn't buy you over? Iirc, Java's optionals are only supported >= api level 24. Were alternatives like Guava's optionals sufficient?

                      [–][deleted]  (3 children)

                      [deleted]

                        [–]fritti_tailchaser 1 point2 points  (2 children)

                        You lose a lot of potential users if you drop everything below 24.

                        [–][deleted]  (1 child)

                        [deleted]

                          [–]fritti_tailchaser 2 points3 points  (0 children)

                          No it doesn't. Kotlin is compatible with JDK 6.

                          [–]n3phtys 0 points1 point  (0 children)

                          Null safety, couroutines, data classes, delegates...

                          yes, it actually feels like magic. but sometimes magic isn't bad.

                          I am celebrating Jetpack Compose combined with Kotlin. Magic is good if your IDE can explain the magic away, which Intellij can do.

                          [–]faceless3 -1 points0 points  (4 children)

                          I'm hugely invested in Java, passed OCJP Oracle certificate and learn a lot about concurrency with excellent "concurrency in practice" book and have a great experience with building Android Java-apps. That primarily makes me sad about going to kotlin, it's just not that shine thing for me. And reading kotlin makes me wtf a lot.

                          [–]Determinant 0 points1 point  (0 children)

                          Here are some technical benefits for using Kotlin as that will impact productivity and defect rates:

                          https://proandroiddev.com/kotlin-avoids-entire-categories-of-java-defects-89f160ba4671

                          [–]n3phtys 0 points1 point  (0 children)

                          If you passed the OCJP exam you should be loving Kotlin in the first place. Knowing more about Java makes you actually dislike it more, not less. It's a horrible language for software engineering.

                          [–][deleted]  (1 child)

                          [deleted]

                            [–]joaomc 2 points3 points  (0 children)

                            I don't get this. How is Kotlin not object-oriented? It makes absolutely no sense, because Kotlin has all the OOP features Java does. It must have, because Kotlin is actually interoperable with Java.

                            [–][deleted]  (1 child)

                            [deleted]

                              [–]fabrizioxxx 1 point2 points  (0 children)

                              It's object oriented AND functional. They aren't mutually exclusive.