Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,...

More fun with left shift operator

Lets say we want to have sum of two integers. But we are not allowed to use + and - operators. Below is a way to do that:

public class Solution {

    public static int getSum(int a, int b) {

        if (a > 0 && b > 0) {
            return positiveSum(a, b);
        }

        if (a < 0 && b < 0) {
            return negate(positiveSum(Math.abs(a), Math.abs(b)));
        }

        if (a > 0 && b < 0) {
            return minus(a, Math.abs(b));
        } else {
            return minus(b, Math.abs(a));
        }
    }

    private static int minus(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb;

            if (carry) {
                result[i] = !result[i];
            }

            if (ba == true) {
                carry = false;
            }

            if (ba == false && bb == true) {
                carry = true;
            }

        }

        int intResult = getIntResult(result);

        return intResult;


    }

    private static int positiveSum(int a, int b) {
        boolean[] result = new boolean[32];

        boolean carry = false;

        for (int i = 0; i < 32; ++i) {

            boolean ba = ((a & (1 << i))) != 0;
            boolean bb = (b & (1 << i)) != 0;

            result[i] = ba ^ bb ^ carry;

            if (carry == true) {
                if (result[i] == true) {
                    carry = false;
                }
            }

            if ((a & (1 << i)) != 0) {
                if ((b & (1 << i)) != 0) {
                    carry = true;
                }
            }
        }

        int intResult = getIntResult(result);

        return intResult;
    }

    private static int getIntResult(boolean[] result) {
        int intResult = 0;
        for (int i = 0; i < result.length; ++i) {

            if (result[i] == true)
                intResult |= (1 << i);

        }
        return intResult;
    }



    // negate a positive value
    private static int negate(int value) {
        if (value < 0) {
            throw new RuntimeException();
        }

        return minus(0, value);
    }

 
    public static void main(String[] args) {

        System.out.println(getSum(-17, -10));
        System.out.println(getSum(17, -10));
        System.out.println(getSum(-10, 17));
        System.out.println(getSum(-15, 32));
        System.out.println(getSum(15, 32));
    }
}

Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

Virtual Memory

NOTES ON COMPUTER ARCHITECTURE: Some important concepts in computer architecture