Posts

Showing posts from February, 2019

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,

A Graph Application in Java: Using WordNet to Find Outcast Words

Image
I'll explain our intent with an example: Let's say we have the following words: horse, zebra, cat, bear, table Which one is the outcast? The table.. But how can a computer system find this? We are talking about the whole English language and any word can be in this list. The number of words in the list can be huge, so the solution should use appropriate data structures and algorithms.  We'll use WordNet which is a graph of English words. It contains the semantic relationships between words and our concern is the "is a" relationship. For our example, we know that horse and cat are both animals. "Table" is not an animal and it is the outcast in this situation. But we even have the ability to tell that horse and zebra is very close. Below is a unit test that shows a high level view of our application in Java: public class OutcastTest {     private WordNet wordNet;     private Outcast outcast;     @Before  

Revisiting Thread Safety and Multithreading in Java

One of the most important advice about multi threading is: don't try to improve performance on a particular area of code unless your performance measurements tell you to do so. In some cases, we may need to compromise good object oriented design to gain performance. But most of the time, good object oriented practices like encapsulation and immutability helps us to create thread-safe classes. A class is thread-safe if it behaves correctly in the case of being accessed by multiple threads and it does not need any synchronization by the caller. A class with no state (no field, no reference to another class) is thread safe, because it's methods use only local variables which live in the stack. And since each thread have their own stack, a thread cannot mess with a local variable of another. ++ count; This is not an atomic operation. It does three things and it is an example of read-modify-write operation. The result is dependent on the initial value. But what if the inital

Notes on Java Performance

Performance monitoring, performance profiling and performance tuning are different activities. For example, monitoring is not an intrusive action but profiling is. Because it may be changing the responsiveness of the running application. User CPU utilization vs Kernel CPU utilization: The ideal situation is when Kernel CPU utilization is 0%. So ideally, all the CPU cycles must be spent on our application code. JVM just in time compiler performs dynamic optimizations. So it makes decisions while the program is running and generates native code with better performance. Java 1.0 was completely interpreted but beginning with Java 1.1.8, there was a just-in-time compiler. It was 8 times faster. After that, paralellizing the garbage collector was a huge improvement. There are hundreds of Java tuning flags. https://github.com/ScottOaks/JavaPerformanceTuning Boolean flags use this syntax: -XX:+FlagName enables the flag, and -XX:-FlagName disables the flag. 32 BIT VS 64 BIT Fr

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 voi

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

swapLexOrder: Finding lexicographically largest string

A Graph Application in Java: Using WordNet to Find Outcast Words