Last Updated: August 18, 2017

Switch from Java to Kotlin

Description: In this post I'm gonna show some of the basic syntax and semantics difference while we use "KOTLIN" in your android application w.r.t JAVA(Java v/s KOTLIN).

So let's get started.

1. Variable Declaration
String name="coderconsole";

val  name:String="coderconsole"

2. Casting

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager

3. Function Declaration


void methodDeclaration(){
    methodDeclaration("First Method ");

void methodDeclaration(String firstParam) {
    methodDeclaration(firstParam, "Second Method ");

void methodDeclaration(String firstParam, String secondParams) {
    System.out.println(firstParam + secondParams);


fun methodDeclaration(){
    methodDeclaration("First Method ")

fun methodDeclaration(name : String){
    methodDeclaration(name, "Second Method ")

fun methodDeclaration(first : String, second: String){
    println(first + second)

4. Static Functions and Variable


class DeviceUtils{
    static final String name = "coderconsole";

    public static String getAndroidId(Context context){
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);


class DeviceUtils {

    companion object {
        val name: String = "coderconsole"
        @JvmStatic fun getAndroidId(context: Context): String = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)


5. Ternary

String ternary(String name) {
    return name.equalsIgnoreCase("coderconsole") ? " Ternary success" : " Ternary failed";

fun ternary(name: String): String {
    return if (name.equals("coderconsole"))"Ternary success" else "Ternary failed"

6. Operators(this operators are only useful in kotlin)

a) Null Type(?)- variable can be made null only using null type operator

   val array: String = null /** Null cannot be the value of not null type**/ 
   val array: String? = null  /** Bingo! Will work now **/

b) Safe Type:(?.) - Helpful when we donot know when the variable be null.So instead of throwing NPE it return the value as null.

//The below code will return "customerId" if present else return null even if the "customerObject" is null.
fun safeTypeDemo(customerObject: JSONObject?): String?{
         return customerObject?.optString("customerId")     

c) Elvis Operator(?:) - Helpful when we want to return some not-null values, if the first result is null.
int elvisDemo(JSONObject result){
      if (result != null)
        return result.optInt("marks");
     else  return -1;

fun elvisDemo(marksObject: JSONObject?): Int {
         return marksObject?.optInt("marks")?:-1      
fun elvisDemo(marksObject: JSONObject?): Int = marksObject?.optInt("marks")?:-1

d) !! operator - throws Null Pointer when asked explicitly
//The below code will throw NullPointerException if customerObject is null.
fun npeTypeDemoDemo(customerObject: JSONObject?): String{
         return customerObject!!.optString("customerId")     

7. Loops
a) for loop:
void forLoopDemo(List mList) {
        for (String item : mList)Log.d("Loop", item);

fun forLoopDemo(mList: List) {
       for (item in mList) Log.d("Loop", item)

b) while/do-while: - there is no syntactical difference from Java.

8. Switch Case
void switchCaseDemo(int type) {
    switch (type){
        case 0:
            Log.d("switch", "value - " + type);
        case 1:
            Log.d("switch", "value - " + type);
            Log.d("switch", "value default");


fun switchCaseDemo(type: Int) {
    when (type) {
        0 -> Log.d("switch", "value - " + type)
        1 -> Log.d("switch", "value - " + type)
        else -> Log.d("switch", "value default")

9. Extends/Implements

//This sample contains abstract class and an interface to printData.
public abstract class AbstractA {
    public void printAbstractData(String data){
public interface InterfaceA {
    public void printData(String data);

public class ExtendsImpDemo extends AbstractA implements InterfaceA {
    @Override    public void printData(String data) {


abstract class AbstractA{
    public fun printAbstractData(data: String){

interface InterfaceA{
    fun printData(data: String)

class ExtendsImpDemo : AbstractA(), InterfaceA {
    override fun printData(data: String) {

10. Iterating JSONArray

void arrayTest(JSONArray jsonArray) {
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.optJSONObject(i);
        Log.d("ArrayTest", jsonObject.optString("name"));

fun arrayTest(jsonArray: JSONArray){
    jsonArray.items<JSONObject>().forEachIndexed { i, jsonObject ->
        Log.d("ArrayTest", jsonObject.optString("name"))

Kotlin is an awesome language and very fun to write code in it.
For further reference you can try

For more updates follow us on -  Twitter

Last Updated: January 11, 2017

Introduction to Internet of things platform Octoblu


In this post I'm gonna describe very helpful platform for building some stuff that involves 'Internet of thing'/ Internet of apis.
So lets get started.

Octoblu - It's an platform to build an amazing iot/iota projects that involve connection of  'things' with variety of tools involved in it. The 'things' could be sensors/led lights/apis from social networks like G+ or twitter etc.

Few key terms
  1. Things - Entity that you need to integrate. E.g: Twitter, G+, Http Api,Sensors etc.
  2. Tools - Connectors to connect the things. E,g: Less than, Greater than operators, Timer.
  3. Flow - Actual Work space which contains THINGS and TOOLS
  4. Bluprints - Final Shared FLOW is Bluprint.
Note: 1. For the sake simplicity we're gonna build a Flow which triggers Twitter post after 5 mins.
          2. DONOT spam your timeline. This may result about your account may get blocked.

Step 1: Create flow

 After successful signup create an Flow and enter name and description as per your choice from  "Flow Inspector" as shown  in fig.1

Flow Inspector

Step 2: Add things.

After creating dummy flow its time to add "Things". For our demo we're gonna add "Twitter" as our thing from the Things tab.

Select the endpoint as "Post Tweet" and add a message for post.
Add status as "Hello world"

 As shown in the fig.2.

Note: You can also search through the things and simply drag and drop. You have to add your twitter account for testing.

Step 3: Add trigger.

After adding "Things" its time to add trigger to initiate our twitter post. You can find triggers within the "Tool" tabs as shown in the fig.3


Step 4: Connection

Now comes the best part "Connections" .Octoblu connections are seamless. We have two points for each "trigger" and "things" simple drag from one point to another as shown in fig4.


Step 5: Lets wrap everything up.

Now simply run the flow created above as shown in fig.5 this will make sure your flow is ready to get triggered

Note: You have to run the flow every time whenever you have made any changes


Step 6: Trigger the flow 

Simple click the play icon on the trigger. fig.3. This is trigger your twitter post with the text as "Hello world" on your twitter profile page.

Thats It !!!

How to debug?

This will help to figureout whats happening whenever the trigger  is initiated.

To debug the flow

  1. You have to enabled "DEBUG" mode from "Thing Inspectors" by  first clicking the things on the flow first.

How To Automate?

1. To automate you can use a tool called as "INTERVAL" and can assign the triggering time as 5 minutes

So finally we have automated our twitter post using "Octoblu". It has got immense power and can come handy for demo projects involving 'Internet of things/Internet of APIS'.

For more updates follow us on -  Twitter