Let’s start this activity with one example. You can pick any example in this case. Here I just want to showcase that all the existing Android Java files(entire application) can be converted to Kotlin syntax.
So I am going to consider one of my existing Android Java file, which is used to process for Binary search logic.
HelloWorld.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.jungle.technical.firsthelloworld; import android.os.Bundle; import android.os.PersistableBundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.Toast; /** * @author TechnicalJungle.com */ public class HelloWorld extends AppCompatActivity{ @Override public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { Log.i("TechnicalJungle", "java --- onCreate"); super.onCreate(savedInstanceState, persistentState); } @Override protected void onResume() { super.onResume(); callBinarySearch(); } private void callBinarySearch() { int arr[] = {2,3,4,10,40}; int n = arr.length; int x = 10; int result = binarySearch(arr,0,n-1,x); if (result == -1) { Log.i("TechnicalJungle", "java --- callBinarySearch element not present"); Toast.makeText(this, " element not present", Toast.LENGTH_SHORT).show(); } else{ Log.i("TechnicalJungle", "java --- callBinarySearch element present at " + result); Toast.makeText(this, " element present at " + result, Toast.LENGTH_SHORT).show(); } } // Returns index of x if it is present in arr[l..r], else // return -1 private int binarySearch(int arr[], int l, int r, int x) { if (r>=l) { int mid = l + (r - l)/2; // If the element is present at the middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then it can only // be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); // Else the element can only be present in right // subarray return binarySearch(arr, mid+1, r, x); } // We reach here when element is not present in array return -1; } } |
Output
1 2 |
06-17 23:02:51.394 2157-2157/? I/TechnicalJungle: java --- callBinarySearch 06-17 23:02:51.394 2157-2157/? I/TechnicalJungle: java --- callBinarySearch element present at 3 |

Once converted the Java to Kotlin, file extension will also be changed from (.java) to (.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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.jungle.technical.firsthelloworld import android.os.Bundle import android.os.PersistableBundle import android.support.v7.app.AppCompatActivity import android.util.Log import android.widget.Toast /** * @author TechnicalJungle.com */ class HelloWorld : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { Log.i("TechnicalJungle", "java --- onCreate") super.onCreate(savedInstanceState, persistentState) } override fun onResume() { super.onResume() callBinarySearch() } private fun callBinarySearch() { val arr = intArrayOf(2, 3, 4, 10, 40) val n = arr.size val x = 10 val result = binarySearch(arr, 0, n - 1, x) if (result == -1) { Log.i("TechnicalJungle", "<strong>java ---</strong> callBinarySearch element not present") Toast.makeText(this, " element not present", Toast.LENGTH_SHORT).show() } else { Log.i("TechnicalJungle", "<strong>java ---</strong> callBinarySearch element present at " + result) Toast.makeText(this, " element present at " + result, Toast.LENGTH_SHORT).show() } } // Returns index of x if it is present in arr[l..r], else // return -1 private fun binarySearch(arr: IntArray, l: Int, r: Int, x: Int): Int { if (r >= l) { val mid = l + (r - l) / 2 // If the element is present at the middle itself if (arr[mid] == x) return mid // If element is smaller than mid, then it can only // be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x) // Else the element can only be present in right // subarray return binarySearch(arr, mid + 1, r, x) } // We reach here when element is not present in array return -1 } } |
Output
1 2 |
06-17 23:02:51.394 2157-2157/? I/TechnicalJungle: java --- callBinarySearch 06-17 23:02:51.394 2157-2157/? I/TechnicalJungle: java --- callBinarySearch element present at 3 |
Mind a note, comment section will not be changed. Thats the reason, output is same. You can manually modify it.
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.