Updating interfaces by using default methods

Take the following interface: public interface Cooking { public void fry(); public void boil(); public void chop(); } To add new functionality, simply adding a new method to Cooking called microwave() will cause problems. Any class that previously implemented Cooking will now have to be updated in order to function again. To avoid this, give microwave() a default implementation: public interface Cooking { public void fry(); public void boil(); public void chop(); default void microwave() { //some code implementing microwave } } As microwave() already has a default implementation defined in the Cooking interface definition, classes that implement it now don’t need to implement microwave() in order to work....

April 9, 2017 · 1 min · Rezha Julio

Converting Stacktrace to String

To store stack trace as a string, you can use Throwable.printStackTrace(...) For example: public static String getStackTrace( Throwable throwable ){ Writer result = new StringWriter(); PrintWriter printWriter = new PrintWriter(result); throwable.printStackTrace(printWriter); return result.toString(); } In the above example, getStackTrace takes a Throwable as a parameter and uses printStackTrace to print it to a PrintWriter output stream. This output is collected by the StringWriter and converted to a string using StringWriter.toString().

April 8, 2017 · 1 min · Rezha Julio

Synchronized Statement in Java

synchronized statements can be used to avoid memory inconsistency errors and thread interference in multi-threaded programs. When a thread executes code within a synchronized statement, the object passed as a parameter is locked. When writing a synchronized block, the object providing the lock must be specified after the synchronized keyword: public class Rezha { private int sum; public void addToSum(int value) { synchronized(this) { sum += value; } } } In this example the object providing the lock is this, which is the instance of Rezha that the method is being called in....

April 7, 2017 · 1 min · Rezha Julio

Function in Python are First-Class Object

Python’s functions are first-class objects. You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions. Grokking these concepts intuitively will make understanding advanced features in Python like lambdas and decorators (I will cover two this in the next post) much easier. It also puts you on a path towards functional programming techniques. In this post I’ll guide you through a number of examples to help you develop this intuitive understanding....

April 6, 2017 · 8 min · Rezha Julio

Django 1.11 Release Note a Reading

Django 1.11 is released for the world to use. It comes with a lot of changes, which can take some time to read. In this video, GoDjango read all of the release notes so you can just put on some headphones and hit play.

April 5, 2017 · 1 min · Rezha Julio