kotlin java_我从Java到Kotlin的旅程

kotlin java

In this piece, I’m going to tell you why and how I started moving from Java to Kotlin, and how to be productive and happy with Kotlin. Back in 2015, I started working on Android using the Java language for the programing part and XML for designing the UI. It wasn’t that difficult for me because I started learning Java in my college days. So it took three to four months of hard work to start my carrier as an Android developer.

在本文中,我将告诉您为什么以及如何开始从Java迁移到Kotlin,以及如何使Kotlin富有成效并感到满意。 早在2015年,我就开始在Android上使用Java语言进行编程,并使用XML来设计UI。 对我而言,这并不困难,因为我在大学时代就开始学习Java。 因此,开始我的运营商成为Android开发人员需要花费三到四个月的辛苦工作。

We’re living in a world where the next most significant transformation might be right around the corner. For me, it happened when Google announced Kotlin as one of the official languages for Android development in 2017. In the beginning, I thought it might be one of those languages that stayed as a low key as C and C++.

我们生活在一个世界中,下一个最重要的转变可能就在眼前。 对我而言,这发生在Google在2017年宣布Kotlin作为Android开发的官方语言之一时。一开始,我认为它可能是与C和C ++一样低调的那些语言之一。

That was my biggest mistake: What I failed to understand is Kotlin is an extension language for Java and has the potential to be used in multiplatform projects. To be honest, I was afraid about the transitioning away from the language I had learned for years and had also worked in for years. I was afraid I’d have to start over.

那是我最大的错误:我无法理解的是Kotlin是Java的一种扩展语言,并且有潜力在多平台项目中使用。 老实说,我担心会从多年学习的语言过渡到过去。 我担心我必须重新开始。

But the fact of moving from Java to Kotlin is more of an advancement than a complete transformation (starting over). I started working on Kotlin in 2018. The internet has enough material explaining the syntax of Kotlin and how to start. The part that helped me best while I was starting to learn Kotlin was its interoperability.

但是,从Java迁移到Kotlin的事实不仅仅是完整的转换(重新开始),而是进步。 我于2018年开始研究Kotlin。互联网上有足够的材料来解释Kotlin的语法以及如何启动。 在我开始学习Kotlin时,对我最好的帮助是它的互操作性。

Shortly after starting to learn Kotlin, I came to know that we can do Kotlin and Java coding in the same project (interoperability). Anyone who has worked on Kotlin will surely say it’s a developer-friendly language because of the leverage that Kotlin provides us while coding.

在开始学习Kotlin之后不久,我知道我们可以在同一项目中进行Kotlin和Java编码(互操作性)。 从事Kotlin工作的任何人肯定会说这是一种对开发人员友好的语言,因为Kotlin在编码时可以为我们提供帮助。

The Android studio team has done an excellent job of making it even easier for developers to code in Kotlin — with features like transforming a Java file to Kotlin in a single click. When you try to paste a Java code into a Kotlin file, the editor itself converts the code to Kotlin. Last but not least, the syntax of Kotlin is quite similar to Java. All these features gave me the confidence to start coding in Kotlin.

Android Studio团队做了出色的工作,使开发人员可以更轻松地在Kotlin中进行编码-只需单击一下即可将Java文件转换为Kotlin。 当您尝试将Java代码粘贴到Kotlin文件中时,编辑器本身会将代码转换为Kotlin。 最后但并非最不重要的一点是,Kotlin的语法与Java非常相似。 所有这些功能使我有信心开始使用Kotlin进行编码。

一开始让我充满信心的Kotlin事物 (Some of the Kotlin Things That Gave Me the Confidence in the Beginning)

By the end of 2018, I was sure about using Kotlin for Android development, and below are some of the reasons that made me believe so

到2018年底,我确定要使用Kotlin进行Android开发,下面是让我相信的一些原因

1. 更少的样板代码 (1. Less boilerplate code)

For example, if you want to create a data class in Java, it’s a bit of work, right? First, you need to declare a variable and then write getters and setters. But in Kotlin, it’s a single line of code. This type of simplified coding will increase the productivity of developers. Have a side-by-side look at Java and Kotlin code.

例如,如果要使用Java创建数据类,则需要一些工作,对吗? 首先,您需要声明一个变量,然后编写getter和setter。 但是在Kotlin中,它只是一行代码。 这种简化的编码类型将提高开发人员的工作效率。 并排查看Java和Kotlin代码。

Image for post

2. 简化的空检查 (2. Simplified null checks)

I don’t know about you, but I made a ton of NullPointerException crashes at the early stages of my career, but with Kotlin, it’s a different story. You can declare a variable as null or not null so when you use a null variable, the editor will always remind you to check if the variable is null or not.

我不了解您,但是在我职业生涯的早期,我就造成了很多NullPointerException崩溃,但是对于Kotlin来说,情况就不同了。 您可以将变量声明为nullnot null因此当您使用null变量时,编辑器将始终提醒您检查变量是否为null

NullPointerException crashes will be reduced, and also the way the null check is done in Kotlin is super optimistic. This type of feature will enhance the stability of the product. Have a look

NullPointerException崩溃将减少,并且在Kotlin中执行null检查的方式也非常乐观。 这种功能将增强产品的稳定性。 看一看

Image for post

3. Kotlin扩展 (3. Kotlin extensions)

With Kotlin, we can write functions on predefined or library classes as extensions and use them as normal functions anywhere. This feature alone has saved me a ton of time. With this feature, I feel like I’m creating my own language. There are predefined extensions, and we can also create our extensions.

使用Kotlin,我们可以将预定义或库类中的函数作为扩展编写,并在任何地方将其用作常规函数。 仅此功能就为我节省了大量时间。 有了这个功能,我感觉就像是在创造自己的语言。 有预定义的扩展名,我们也可以创建扩展名。

I can’t show every extension that I used in this article, but I’ll show you how I saved time while implementing Rx click listener on a view. Have a look:

我无法显示本文中使用的每个扩展,但是将向您展示如何在视图上实现Rx click侦听器时节省时间。 看一看:

Image for post

4.直接从字符串访问变量 (4. Access variables directly from strings)

This allows a bit of comfort while coding, and, for me, it makes sense to access variables inside a string formation rather than concatenation in Java.

这给编码时带来一点舒适,对我而言,在字符串形式内访问变量而不是在Java中进行串联是有意义的。

These are some of the features that gave me the initial push to move from Java to Kotlin.

这些功能使我最初从Java迁移到Kotlin。

一次转化为Kotlin的神话 (The Myth of Transforming Into Kotlin at Once)

Right before 2019, I started to use Kotlin in my enterprise-level coding, and I slowly began to feel more comfortable coding in Kotlin instead of Java.

就在2019年之前,我开始在我的企业级编码中使用Kotlin,然后我逐渐开始感到用Kotlin而不是Java进行编码变得更加舒适。

So, whenever possible, I do an incremental transformation — like building new features in Kotlin. Or when you need to refactor something, start refactoring it in Kotlin. No matter how big or small the project is, you can do the transformation whenever possible. But the important thing is you should write the code in Kotlin from now.

因此,只要有可能,我都会进行渐进式转换-例如在Kotlin中构建新功能。 或者,当您需要重构某些东西时,请在Kotlin中开始对其进行重构。 无论项目大小,您都可以随时进行转换。 但是重要的是您应该从现在开始用Kotlin编写代码。

In a short form, let me tell you how it works: You write the entire code in Kotlin, and while compiling, the Kotlin compiler compiles the code and converts it into Java classes and bytecode. The same thing happens on the Java compiler side. So, finally, no matter which language you write in, the code is converted into bytecode so JVM can execute it. This is how Kotlin and Java survive in the same project.

简而言之,让我告诉您它是如何工作的:用Kotlin编写整个代码,并且在编译时,Kotlin编译器会编译代码并将其转换为Java类和字节码。 同样的事情发生在Java编译器方面。 因此,最后,无论您使用哪种语言编写,代码都将转换为字节码,以便JVM可以执行它。 这就是Kotlin和Java在同一个项目中生存的方式。

Image for post

Kotlin正在大规模发展 (Kotlin Is Evolving at Scale)

As I said at the beginning, I thought Kotlin was only a replacement for Java in Android development, but one day, I found Kotlin can be used outside of Android development.

就像我一开始所说的那样,我以为Kotlin只是Android开发中Java的替代品,但是有一天,我发现Kotlin可以在Android开发之外使用。

It can be used to replace JavaScript coding in web development, and it can be used in any platform with a compiler that has the ability to understand machine code. The Kotlin compiler is handy; it can convert the code into Java, JavaScript, and machine code.

它可以用来代替Web开发中JavaScript编码,并且可以在具有能够理解机器代码的编译器的任何平台中使用。 Kotlin编译器很方便; 它可以将代码转换为Java,JavaScript和机器代码。

Image for post

This is big, right? For me, it is. If you can work cross-platform with a unique language, the chances of your growth are very high in this tech era. There are things we should figure out before starting to code across platforms, but what I’m saying is you don’t need to learn the core part again — just the basics of respective platforms.

这个很大吧? 对我来说是。 如果您可以使用一种独特的语言跨平台工作,那么在这个技术时代,您成长的机会就很高。 在开始跨平台编码之前,我们需要弄清楚一些事情,但是我要说的是,您无需再次学习核心部分-只需了解各个平台的基础知识即可。

我对Kotlin的看法 (My Thoughts on Kotlin)

Not even a single advocate from the Android team is suggesting to start coding in Java for Android development because they know the future. Kotlin has made the grand entrance to Android development, and it’s on its way to the web and iOS platforms(maybe not as a full stack, but you’ll see its real capabilities in the near future) — which I’m currently learning.

甚至没有来自Android团队的拥护者建议他们开始使用Java进行Android开发的编码,因为他们知道未来。 Kotlin进入了Android开发的宏伟大门,并且正在进入Web和iOS平台(也许不是完整的堆栈,但是您将在不久的将来看到其真正的功能)-我正在学习。

Do you know that Kotlin has been around only for five years, and it was the fourth most loved language in 2019? Having worked in languages like Java for years, we need something new that can do better than Java (no offense, it’s doing a great job in various platforms). Something that supports us in multiplatform development.

您是否知道Kotlin成立仅五年了,它是2019年第四大最受欢迎的语言? 在使用Java之类的语言已有多年的历史之后,我们需要比Java做得更好的新东西(没有冒犯,它在各种平台上都做得很好)。 在多平台开发中支持我们的东西。

When I work in Kotlin, I’ve completed my tasks in half the time it takes me in Java. One day, I wondered why. So I started to code a screen in both Kotlin and Java. Then, I found out I code approximately 50% less in Kotlin thanks to its concise functional programming. More concise code takes less time to write and less time to read and understand — which results in the developer productivity.

当我在Kotlin工作时,我用Java所需的时间完成了一半。 有一天,我想知道为什么。 因此,我开始用Kotlin和Java编写屏幕编码。 然后,我发现由于Kotlin简洁的功能编程,我的代码减少了约50%。 更加简洁的代码花费更少的时间来编写代码,花费更少的时间来阅读和理解,从而提高了开发人员的工作效率。

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/better-programming/my-journey-from-java-to-kotlin-3bfcbcc6b734

kotlin java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值