Differences between "beginner" Java and Kotlin

What is Kotlin

--Kotlin is a language that runs on the JVM created by JetBrains. --Kotlin is fully interoperable with Java. You can write a class in Java, a class that inherits it in Kotlin, and a class that inherits it in Java. --You can write additional development in Kotlin for a project written in Java, which makes it easy to introduce. --Google has announced official Kotlin support for Android.

Variable declaration

Use val for constants and var for mutable variables.

test.java


String str = "String";
final int value = 0;

test.kt


var str: String = "String"
val value = 0

Since type inference is possible in Kotlin, variables can be defined by omitting the type. No semicolon is needed.

if statement

test.java


String value = bar < 10 ? "abc" : "xyz";

test.kt


 var value = if (bar < 10) "abc" else "xyz"

You can directly assign the result in the if or else block.

for statement

test.java


for(int num : numbers)
{
System.out.println(num);
}
for(int i = 0; i< 4; i++)
{
System.out.println(i); 
} 

test.kt


for (num in numbers)
{
  println(num)
}
for (i in 0..3)
{
  println(i)
}

Kotlin uses in.

function

test.java


public int add(int a, int b) 
{
    return a + b;
}
add(1,2);

test.kt


fun add(a:Int, b:Int): Int {
    return a + b
}

//Caller
add(1,2)

In the case of Kotlin, if the access modifier is omitted, it is treated as public. Furthermore, if the return type allows the compiler to perform type inference, it is possible to omit the definition of the return type.

Recommended Posts

Differences between "beginner" Java and Kotlin
Differences between Java and .NET Framework
Conversion between Kotlin nullable and Java Optional
Relationship between kotlin and java access modifiers
[For beginners] Difference between Java and Kotlin
[Java] Differences between instance variables and class variables
Differences between Ruby strings and symbols [Beginner]
Summarize the differences between C # and Java writing
[Java] Difference between == and equals
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
[Java beginner] Difference between length and length () ~ I don't know ~
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
[Java Bronze learning] Differences between encapsulation, data hiding, and information hiding
[Java] Difference between Hashmap and HashTable
java beginner 4
[Java beginner] About abstraction and interface
java beginner 3
[Java beginner] == operator and equals method
java beginner
[JAVA] Difference between abstract and interface
[Java] Relationship between H2DB and JDBC
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Java starting from beginner, variables and types
Difference between final and Immutable in Java
Differences between preface and postfix of operators
A Java engineer compared Swift, Kotlin, and Java.
[Note] Cooperation between Java and DB (basic)
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
[Rails] Difference between redirect_to and render [Beginner]
HashMap # putAll () behaves differently between Java 7 and Java 8
A note on the differences between interfaces and abstract classes in Java
Java Beginner Exercises
Java and JavaScript
XXE and Java
Java Exercise "Beginner"
[Java] Understand the difference between List and Set
Kotlin functions and lambdas to send to Java developers
Java switch statement and break, Kotlin when expression ...
Differences between Spring Initializr packaging JAR and WAR
Difference between next () and nextLine () in Java Scanner
Build Java environment and output hello world [Beginner]
[Understanding] Differences between hashes and arrays in Ruby
Differences in writing Java, C # and Javascript classes
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Java] Difference between "final variable" and "immutable object"
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
Getters and setters (Java)
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
[Java] Thread and Runnable
Please note the division (division) of java kotlin Int and Int
Java true and false
Differences between Ruby syntax error statements in Ruby and binary