<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Java on dlow's blog</title><link>https://blog.dlow.me/tags/java/</link><description>Recent content in Java on dlow's blog</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 22 May 2020 11:26:33 +0100</lastBuildDate><atom:link href="https://blog.dlow.me/tags/java/index.xml" rel="self" type="application/rss+xml"/><item><title>Struct embedding as implicit composition</title><link>https://blog.dlow.me/programming/golang/struct-embedding/</link><pubDate>Fri, 22 May 2020 11:26:33 +0100</pubDate><guid>https://blog.dlow.me/programming/golang/struct-embedding/</guid><description>&lt;p>In programming, the principle &amp;ldquo;don&amp;rsquo;t repeat yourself&amp;rdquo;, DRY for short, recommends that we try to reuse the same implementation where possible. In this post, I will cover what it&amp;rsquo;s like to do it in golang through struct embedding and my personal thoughts on that pattern.&lt;/p>
&lt;h1 id="golang-struct-embedding">Golang struct embedding &lt;a class="anchor" href="#golang-struct-embedding">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>One common way of code reuse is inheritance. In Java, we can do this by implementing a method on the parent class, then subclasses will inherit this implementation.&lt;/p></description></item><item><title>Logging in scala and java</title><link>https://blog.dlow.me/programming/logging/</link><pubDate>Fri, 05 Jul 2019 23:29:27 +0100</pubDate><guid>https://blog.dlow.me/programming/logging/</guid><description>&lt;p>A logging framework is superior to just using &lt;code>println&lt;/code> because it can support&lt;/p>
&lt;ul>
&lt;li>Log rotation (for daemons)&lt;/li>
&lt;li>Configurable log format&lt;/li>
&lt;li>Configurable log level&lt;/li>
&lt;/ul>
&lt;p>There are several libraries to use when it comes to logging in Java. For the practical programmer, the important things to note are&lt;/p>
&lt;ul>
&lt;li>What to include in the dependency resolution tool. e.g. sbt, pom, gradle.&lt;/li>
&lt;li>How to configure the logger. By default the configuration is usually found in the &lt;code>resources&lt;/code> directory. e.g. &lt;code>src/main/resources&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h1 id="java">Java &lt;a class="anchor" href="#java">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;h2 id="java-util-logging">Java util logging &lt;a class="anchor" href="#java-util-logging">&lt;span>&lt;/span>&lt;/a>&lt;/h2>&lt;p>This is included in the JRE. &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/logging/LogManager.html">Configuration&lt;/a> is done by setting the system property &lt;code>java.util.logging.config.file=logging.properties&lt;/code>. &lt;a href="https://docs.oracle.com/javase/7/docs/technotes/guides/logging/overview.html">Docs&lt;/a>&lt;/p></description></item><item><title>Class Design for Maintainability</title><link>https://blog.dlow.me/programming/class-design-for-maintainability/</link><pubDate>Sat, 23 Mar 2019 10:44:17 +0000</pubDate><guid>https://blog.dlow.me/programming/class-design-for-maintainability/</guid><description>&lt;p>In this essay, I want to explore the topic of how to write classes that can be easily extended/maintained for the future. We would like to design classes in a way such that it is easy to implement a new class as well as add functionality. We shall explore some of the common patterns for this.&lt;/p>
&lt;h1 id="the-problem-of-extending-classes">The problem of extending classes &lt;a class="anchor" href="#the-problem-of-extending-classes">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>Suppose we have the following model:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">abstract&lt;/span> &lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Animal&lt;/span>{}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Cat&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> Animal {}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Human&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> Animal {}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You are asked to count the legs of a collection of animals:&lt;/p></description></item><item><title>Strategy Pattern</title><link>https://blog.dlow.me/programming/java/strategy-pattern/</link><pubDate>Thu, 07 Mar 2019 20:53:55 +0000</pubDate><guid>https://blog.dlow.me/programming/java/strategy-pattern/</guid><description>&lt;h1 id="goals">Goals: &lt;a class="anchor" href="#goals">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>When the number of strategy changes, we want to minimize change.
When the implementation of a strategy changes, we want to minimize change.&lt;/p>
&lt;h1 id="case-study">Case study: &lt;a class="anchor" href="#case-study">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;ul>
&lt;li>Mode of transportation&lt;/li>
&lt;li>Representation of a data&lt;/li>
&lt;li>Behaviour of an actor&lt;/li>
&lt;li>Trading algorithm&lt;/li>
&lt;/ul>
&lt;h1 id="entitiesobjects-involved">Entities/Objects involved &lt;a class="anchor" href="#entitiesobjects-involved">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;ul>
&lt;li>There is a strategy interface encapsulating the behaviour&lt;/li>
&lt;li>There is a context which wraps the strategy and is responsible for getting the data to apply the strategy on.&lt;/li>
&lt;li>There are implementation of the strategy interface.&lt;/li>
&lt;/ul>
&lt;h1 id="motivating-problem">Motivating problem &lt;a class="anchor" href="#motivating-problem">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>Suppose you have a set of datapoints, and you want to visualize them. You can either visualze them as a frequency graph, or as a time chart.&lt;/p></description></item><item><title>8 Weeks into Java and Scala</title><link>https://blog.dlow.me/programming/8-weeks-into-java-and-scala/</link><pubDate>Sun, 15 Apr 2018 02:19:36 +0000</pubDate><guid>https://blog.dlow.me/programming/8-weeks-into-java-and-scala/</guid><description>&lt;p>This post contains some thoughts I have after working with Java and Scala professionally for 8 weeks. The content assumes the reader has some experiences writing Java/Scala code.&lt;/p>
&lt;h1 id="starting-from-scratch">Starting from scratch &lt;a class="anchor" href="#starting-from-scratch">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>There are several components a learner must understand before he/she can get productive.&lt;/p>
&lt;ul>
&lt;li>&lt;strong>An easy installation process&lt;/strong>, preferably something along the lines of download something, extract or run an installer, modify the user path if necessary. I think anything more than that is asking too much from the user.&lt;/li>
&lt;li>&lt;strong>An intuitive package manager&lt;/strong>. Golang comes with &lt;code>go get&lt;/code> and &lt;code>go install&lt;/code>. Node has &lt;code>npm&lt;/code>, Python has &lt;code>pip&lt;/code>, and Java has maven, ant, gradle. In my opinion, Java is probably the least beginner friendly for setting up.&lt;/li>
&lt;li>&lt;strong>The syntax (literal or not) for initializing common datatypes&lt;/strong> such as list/array, map/dictionary, and for manipulating data.&lt;/li>
&lt;/ul>
&lt;h1 id="java">Java &lt;a class="anchor" href="#java">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;h2 id="java-requires-good-mastery-of-the-ide">Java requires good mastery of the IDE &lt;a class="anchor" href="#java-requires-good-mastery-of-the-ide">&lt;span>&lt;/span>&lt;/a>&lt;/h2>&lt;p>From compilation to running the program, the IDE is immensely important.&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> The IDE, I use intelliJ, have probably generated more code than I what I physically typed. I feel that half of knowing Java is knowing which shortcuts to press in the IDE. The following are the shortcuts that bring me pleasure when I use them.&lt;/p></description></item><item><title>Java variance, covariance</title><link>https://blog.dlow.me/programming/java/variance/</link><pubDate>Wed, 28 Mar 2018 23:50:26 +0000</pubDate><guid>https://blog.dlow.me/programming/java/variance/</guid><description>&lt;h1 id="introduction">Introduction &lt;a class="anchor" href="#introduction">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>Many langauges support subtyping - this allows us to express that a type Apple is a subtype of Fruit. Notationally, we say &lt;code>T &amp;lt; S&lt;/code>. Subtyping manifest itself as subclass in most programming language concepts.&lt;/p>
&lt;p>Suppose we have the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-java" data-lang="java">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Fruit&lt;/span> {}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Apple&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> Fruit {}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Orange&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> Fruit {}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">GreenApple&lt;/span> &lt;span style="color:#66d9ef">extends&lt;/span> Apple {}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Then we have &lt;code>Apple &amp;lt; Fruit&lt;/code>. In simple English, we can think of this as whenever we expect a type &lt;code>Fruit&lt;/code>, we can use the type &lt;code>Apple&lt;/code>. Or think of it as an &lt;code>Apple&lt;/code> &lt;strong>is a&lt;/strong> &lt;code>Fruit&lt;/code>.&lt;/p></description></item><item><title>How to do common tasks in Java</title><link>https://blog.dlow.me/programming/java/java-common-tasks/</link><pubDate>Mon, 19 Mar 2018 21:49:03 +0000</pubDate><guid>https://blog.dlow.me/programming/java/java-common-tasks/</guid><description>&lt;h1 id="gulf-of-execution">Gulf of execution &lt;a class="anchor" href="#gulf-of-execution">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>In his book &lt;em>The design of everyday things&lt;/em>, Norman introduced the terms &lt;code>gulf of evaluation&lt;/code> and &lt;code>gulf of execution&lt;/code>. The latter is the difference between the intentions of the user and what the system allows them to do or how well the system supports those actions.&lt;/p>
&lt;p>This describes perfectly my experience as I moved from Golang to Java.&lt;/p>
&lt;p>I find myself asking questions like:&lt;/p>
&lt;ul>
&lt;li>What is the least painful way of iterating over a map?&lt;/li>
&lt;li>How do I make lazy functions?&lt;/li>
&lt;li>How do I run something in a different thread?&lt;/li>
&lt;li>How to get the path of folder and files?&lt;/li>
&lt;/ul>
&lt;p>This post documents my learning through my struggle.&lt;/p></description></item></channel></rss>