Android Studio 3.0.0 has the support for Kotlin Programming Language. This platform is fully compatible to Kotlin & Java together.
Key Notes
- Java can call to Kotlin function or visa versa is also true.
- If you don’t how to write Kotlin code then it’s still very easy to convert Java code to Kotlin code by just copying java functions to Kotlin file. One confirmation pop-up will be appeared, once you accept then java code will be converted to Kotlin easily.
- There is no “extends” keyword.
- There is no semi colons (;) to terminate the statements.
- Functions will be starting with “fun” keyword.Let’s share my thoughts through an code example below:
HelloActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.jungle.technical.firsthelloworld import android.support.v7.app.AppCompatActivity import android.os.Bundle class HelloActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_hello) getName() processPrime() HelloJava.getName() HelloJava.processPrime() } protected fun getName(): String { println("Kotlin --- TechnicalJungle.com") return "Kotlin --- TechnicalJungle.com" } protected fun processPrime(): String { var i: Int var m = 0 var flag = 0 val n = 17//it is the number to be checked m = n / 2 i = 2 while (i <= m) { if (n % i == 0) { println("Kotlin --- Number is not prime") flag = 1 return "Kotlin --- 17 is not a primeNumber" } i++ } if (flag == 0) { println("Kotlin --- Number is prime") return "Kotlin --- 17 is a primeNumber" } return "Kotlin --- 17 is not a primeNumber" } } |
Some points:
- Copied getName() and processPrime() methods from java and pasted to Kotlin file.
- Kotlin and Java both have same two methods, only signature is different. Now you can compare it easily.
HelloJava.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.jungle.technical.firsthelloworld; public class HelloJava { public HelloJava() { } protected static String getName() { System.out.println("java --- TechnicalJungle.com"); return "Java --- TechnicalJungle.com"; } protected static String processPrime() { int i,m=0,flag=0; int n=17;//it is the number to be checked m=n/2; for(i=2;i<=m;i++){ if(n%i==0){ System.out.println("Java --- Number is not prime"); flag=1; return "Java --- 17 is not a primeNumber"; } } if(flag==0) { System.out.println("Java --- Number is prime"); return "Java --- 17 is a primeNumber"; } return "Java --- 17 is not a primeNumber"; } } |
Output
1 2 3 4 |
06-17 22:08:10.875 25890-25890/com.jungle.technical.firsthelloworld I/System.out: Kotlin --- TechnicalJungle.com 06-17 22:08:10.875 25890-25890/com.jungle.technical.firsthelloworld I/System.out: Kotlin --- Number is prime 06-17 22:08:10.875 25890-25890/com.jungle.technical.firsthelloworld I/System.out: java --- TechnicalJungle.com 06-17 22:08:10.875 25890-25890/com.jungle.technical.firsthelloworld I/System.out: Java --- Number is prime |
If you have some doubt then I recommend you to have a look on above video for more better understanding. You can put your question also in the comment section below.