IS JAVA DEAD? (RIP đ˘ JAVA)
WILL JAVA BE REPLACED BY KOTLIN?
Nowadays the android studio has made it official that java might be replaced by kotlin. earlier On 7 May 2019, Google announced that the Kotlin programming language is now its preferred language for Android app developers. Since the release of Android Studio 3.0 in October 2017, Kotlin has been included as an alternative to the standard Java compiler. To understand both sides of the debate, letâs first take a close look at their meanings.
Why kotlin?
Kotlin is a general-purpose, free, open-source, statically typed âpragmaticâ programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features. Now let us understand what is java.
Why java?
Java is a high-level Programming language developed by Sun Microsystems. It was originally designed for developing programs for set-top boxes and handheld devices but later became a popular choice for creating web applications
The Java syntax is similar to C++ but is strictly an object-oriented programming language. For example, most Java programs contain classes, which are used to define objects, and methods, which are assigned to individual classes.
Now let us talk about the difference between them
Difference between java and kotlin
1. Extension Functions
Dissimilar to Java, Kotlin permits the engineer to broaden the usefulness of classes without fundamentally acquiring from a class. To play out the expansion capability in Kotlin, the engineer should prefix the name of the class (that will be stretched out) to the name of the capability made by utilizing the â.â documentation.
In Java, to broaden the usefulness of a current class, one should make another class and acquire the capabilities from the parent class. As such, the expansion capability isnât accessible.
2. Null Safety
The renowned NullPointerExceptions in Java give designers very much a migraine. The thought behind NullPointerExceptions is that it empowers clients to credit an invalid worth to any factor. However, assume clients endeavor to utilize an item reference that ends up having an invalid worth. All things considered, Javaâs NullPointerExceptions become an integral factor and open an exemption that designers need to deal with.
On the contrary to that, in Kotlin, crediting invalid qualities to factors or items of course is unimaginable. Assuming we attempt to do such, the code will fizzle at arrange time. Thusly, there are no NullPointerExceptions in Kotlin. Nonetheless, on the off chance that the designer wishes to relegate an invalid worth, it is feasible to check the variable being referred to as nullable expressly. To do as such, one should add a question mark:
Val num: Float? = null
3. Code
One of the critical contrasts between Kotlin and Java is that Kotlin requires way less code. It is an exceptionally brief language, which lessens the possibility of making code mistakes and improves the engineersâ work.
Generally speaking, Kotlinâs curtness makes it more reasonable to compose huge undertakings thinking that it regularly requires fewer lines of code than Java to compose precisely the same capabilities. In addition, it knows how to keep it short and focused without undermining language structureâs coherence.
4. Smart Casts
To cast an object in Java, the developer must check the variablesâ type in consonance to the operation.
In Kotlin, the casting checks are handled by the smart casts feature. Kotlinâs intelligent compiler automatically manages redundant casts (with stable values) via the âis-checksâ keyword.
5. Coroutines Support
In Android, as a matter of course, parts that have a place with a similar application run in a similar cycle and string are normally alluded to as the principal string and are liable for the UI. Network I/O and CPU-escalated tasks are viewed as extensive. At the point when both of these activities are started, the particular calling string is hindered until the whole effort is finished.
To keep away from complexities on the fundamental string, Java empowers the making of different foundation strings while dealing with extended tasks. The drawback is that dealing with different strings is a mind-boggling task that might prompt more blunders in the code.
Additionally, Kotlin likewise gives the ability to make different strings. In any case, it presents a superior and more direct arrangement: coroutines.
How do coroutines work? Indeed, coroutines are stackless and permit the engineer to compose code, suspend the execution, and later resume it once more. This empowers a non-hindering offbeat code that appears to be coordinated. Thus, rather than making numerous strings that the designer should deal with later, coroutines try not to have such a large number of strings. Besides, they are more clear and brief than Javaâs answers.
6. Checked Exceptions
Checked exceptions are not available on Kotlin. Thus, Kotlinâs developers do not require to catch or declare exceptions. Is this a good thing? Well, it depends.
Java developers have checked exceptions support. Hence, they must catch and declare exceptions. On the one hand, this can be frustrating and time-consuming. But, on the other hand, it ensures robust code and that errors are being handled. So, checked exceptions support has its pros and cons. Ultimately, it depends on what each developer prioritizes the most.
7. Data Classes
On the one hand, in Java, developers need to establish the fields (or variables) to store the data, the constructor, and the getter and setter functions for the fields/variables, as well as other functions, such as the hashCode(), equals(), and toString().
Truth be told, these classes are mainly intended to store data and have zero (or very little, at most) functionality.
On the other hand, Kotlin provides a more straightforward way to create classes to hold data by simply including the âdataââ keyword in the class definition. Then the compiler will auto-generate the constructor and the getter and setter functions for several fields/variables.
8. Functional Programming: Higher-Order Functions and Lambdas
As mentioned at the beginning of the article, Kotlin is a mix of object-oriented and functional programming.
Functional programming is a declarative style of programming that handles computations based on mathematical functions. High-order functions and lambda expressions are (some) functional programming concepts.
The first one indicates that functions should be considered first-class. Thus, Kotlin, a statically typed language, can make the most out of various function types to represent functions. In other words, it is possible to operate functions in a variety of ways.
Furthermore, it is also possible to use lambda expressions or anonymous functions with Kotlin. These are considered a âfunctional literalâ. Hence, it represents a function that is not declared and immediately passes as an expression.
Contrarily, Java is more limited to the concept of object-oriented programming. However, it has been taking some steps toward functional programming too. Since Java 8, in 2014, Java has introduced lambda expressions, which is a functions that can be created without necessarily belonging to a class. Lambda expressions in Java can be passed as objects and can be executed on demand.
Further, with the introduction of lambda expressions, Java also started supporting high-order functions. Java associates a function with a method, and Java 8 enabled lambdaâs return from methods.
9. Implicit conversions
On the one hand, Kotlin does not provide support for implicit widening conversions. Therefore, smaller types cannot be converted to bigger types. To overcome this, Kotlin developers must execute an explicit conversion to achieve the desired type of conversion.
On the other hand, Java supports implicit conversions, meaning the developers do not need to perform explicit conversions.
10. Primitive types
In Java, variables of a primitive type are not objects; they are predefined in Javaâs data types. Java has eight different primitive data types: int, byte, short, double, float, boolean, char, and long. Consequently, these variables cannot be an object represented by a struct or a class.
Even though primitive types are not classes, Java developers can use classes that can wrap a value of the primitive type. To do so using Java, the developer must indicate it explicitly.
In contrast, in Kotlin, as soon as you initiate a variable of a primitive type, it will be automatically considered an object.
11. Wildcard Types
Typically, a question mark (?) in code is considered a wildcard, which suggests an unknown type (of variable, field, or parameter).
Unlike Java, Kotlin does not provide wildcards. Instead, it has a declaration-
12. Public Fields
Public fields (also known as non-private fields) are available in Java. They can be pretty convenient in case the callers of an object require to modify according to the representation of that same object since it enables the developer to change the representation of an object without needing to adjust the callers. This way, the fields can be public; thus, the public API remains unchanged, and the program can sustain a level of maintainability.
In contrast, Kotlin does not have public fields.
Summary of java vs kotlin
Conclusion
So, which one is better and should you use it? The answer to that question depends on your needs. If youâre looking for a language with solid support from Google, then Kotlin may be the best choice, as Android Studio 3 now supports Kotlin development. flexibility (especially in terms of third-party libraries), Java might be the right option for you.
I hope that this article is informative and interesting for you. Here are some of my latest articles. (IF you like my article I would love to have an audience like you and that helps me encourage you to make awesome articles like this one SO I WOULD APPRECIATE IF YOU FOLLOW ME TO CONTINUE GETTING MY NEWEST ARTICLES, here is a link to my website https://infoyl.com/blog/ )