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,...

Revisiting Java Generics and Wildcard Types

Generics were added with Java 5. Before that you had to cast every object you read from a collection. With generics you tell the compiler what types of objects are permitted in collections. Then the compiler tells you when there is a problem.

If a class or interface has type parameters, then it is called a generic class/interface. Generic classes or interfaces can be used as "raw types". This is primarily for backward compatibility. We get a warning message from compiler when we use raw types.

List<Object> can be used but you cannot assign List<String> to this type. They are both subtypes of raw List type.
Set<Integer> set1 = new HashSet<>();
Set<Object> set3 = new HashSet<>();
// compile error.
set1 = set3;
But what if you don't care about the actual type of the collection elements? Lets say you want to have a method with 2 sets as parameter.

We can use Set<?> which means "set of any type".
public static void main(String[] args) {
        Set<Integer> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        calculate(set1, set2);

        Set<?> mySet = new HashSet<>();

        // compile error. You cannot do that when the type is uncertain.
        // you can do that by using raw types but you get a warning and it is not reccomended!
        // this means, wildcard type is useful to assign a reference to another
        mySet.add(new Integer(1));

        Set myRawSet = new HashSet();

        myRawSet.add(new Integer(2));

        // legal
        mySet = set1;
        mySet = set2;
    }

    private static void calculate(Set<?> set1, Set<?> set2) {
    }

Regarding arrays, it is illegal to create an array of a generic type. In Java, arrays and generics don't mix very well. 

Below is an example that illustrates the syntax of generics:
public class Test {

    // generic method
    public <T> T doSomething(T a, T b) {
        return null;
    }

    // generic method with bounded type
    public <T extends Comparable> T doSomething2(T a, T b) {
        return null;
    }

    class MyGenericClass<K> {

        public K operation() {
            return null;
        }
    }

    private void wildCardUnbounded() {
        MyGenericClass<?> variable = new MyGenericClass<>();

        MyGenericClass<Integer> variable2 = new MyGenericClass<>();

        variable = variable2;
    }

    private void wildCardBounded() {
        MyGenericClass<? extends Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // compile error: not comparable
        variable = variable2;

    }

    private void wildCardBounded2() {
        MyGenericClass<? super Comparable> variable = new MyGenericClass<>();

        MyGenericClass<Object> variable2 = new MyGenericClass<>();

        // legal because Object is super class of Comparable
        variable = variable2;
    }
}


Below is an example that uses bounded wildcard types:

public class UnionExample {

    public static <E> Set<E> union(Set<? extends E> s1,
                                   Set<? extends E> s2) {
        //..
        return null;
    }

    public static void main(String[] args) {
        Set<Integer> integers = Set.of(1, 2, 3);

        Set<Double> doubles = Set.of(1.2, 2.3, 4.5);

        Set<UnionExample> examples = new HashSet<>();

        // valid
        Set<Number> result = union(integers, doubles);

        // compile error: UnionExample is not a subclass of Number
        result = union(integers, examples);
    }
}

Differences between Java Generics and C# Generics

The most important difference seems to be the "type erasure" concept. In Java, generics are a compile time structure, in runtime the type of the object is erased (erasure) so you cannot have the type information at runtime. In C#, generics are also present at the CLR level, and you can get the type information at runtime.

It is an error in C# to create a Generic Type without providing any Type Arguments. Java permits creating Generic Types without providing any Type Arguments; these are called raw types. This is because of backward compatibility. Compiler gives a warning and using raw types is a bad practice for modern java code.

In Java you cannot use your type variable of a class inside a static method:

public class GenericWithStaticMethod<Tgt;{

    // compile error!
    public static void doSomething(T a) {

    }
}

You see this famous error message:

error: non-static class T cannot be referenced from a static context







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