<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2025-03-21T16:44:26+00:00</updated><id>/feed.xml</id><title type="html">EB</title><entry><title type="html">Java Reminders</title><link href="/jekyll/update/2022/01/17/java-reminders.html" rel="alternate" type="text/html" title="Java Reminders" /><published>2022-01-17T17:00:00+00:00</published><updated>2022-01-17T17:00:00+00:00</updated><id>/jekyll/update/2022/01/17/java-reminders</id><content type="html" xml:base="/jekyll/update/2022/01/17/java-reminders.html"><![CDATA[<p>Some reminders when working with Java…if that is what you want to call them.
<br />
<br /></p>
<h3 id="mathabs-can-return-a-negative-value">Math.abs() can return a negative value.</h3>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>jshell&gt; Math.abs(Integer.MIN_VALUE)
$1 ==&gt; -2147483648

jshell&gt; Math.abs(Integer.MIN_VALUE + 1)
$2 ==&gt; 2147483647
</code></pre></div></div>
<p>The highest value a 32 bit integer can contain is <code class="language-plaintext highlighter-rouge">+2147483647</code>. Attempting to represent <code class="language-plaintext highlighter-rouge">+2147483648</code> in a 32 bit int 
will effectively roll over to <code class="language-plaintext highlighter-rouge">-2147483648</code>. When using signed integers, the two’s complement binary representations 
of <code class="language-plaintext highlighter-rouge">+2147483648</code> and <code class="language-plaintext highlighter-rouge">-2147483648</code> are identical. This is not a problem, however, as <code class="language-plaintext highlighter-rouge">+2147483648</code> is considered out of 
range.
<br />
<br /></p>
<h3 id="use-compareto-over-subtraction-pattern">Use compareTo() over subtraction pattern.</h3>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Testing</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">final</span> <span class="nc">BankAccount</span> <span class="n">accountOne</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BankAccount</span><span class="o">(</span><span class="mi">1_073_741_825</span><span class="o">);</span>
        <span class="kd">final</span> <span class="nc">BankAccount</span> <span class="n">accountTwo</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BankAccount</span><span class="o">(-</span><span class="mi">1_073_741_823</span><span class="o">);</span>
        <span class="kd">final</span> <span class="kt">int</span> <span class="n">comparison</span> <span class="o">=</span> <span class="n">accountOne</span><span class="o">.</span><span class="na">compareTo</span><span class="o">(</span><span class="n">accountTwo</span><span class="o">);</span>
        <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">comparison</span><span class="o">);</span> <span class="c1">// -2147483648</span>
    <span class="o">}</span>

    <span class="kd">private</span> <span class="n">record</span> <span class="nf">BankAccount</span><span class="o">(</span><span class="kt">int</span> <span class="n">balance</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">public</span> <span class="kt">int</span> <span class="nf">compareTo</span><span class="o">(</span><span class="nc">BankAccount</span> <span class="n">anotherAccount</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">balance</span> <span class="o">-</span> <span class="n">anotherAccount</span><span class="o">.</span><span class="na">balance</span><span class="o">;</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Subtracting a negative number from a positive number is addition. The comparison value <code class="language-plaintext highlighter-rouge">2147483648</code> is too large for 
an <code class="language-plaintext highlighter-rouge">int</code> and rolls over to <code class="language-plaintext highlighter-rouge">-2147483648</code>.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Testing</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">final</span> <span class="nc">BankAccount</span> <span class="n">accountOne</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BankAccount</span><span class="o">(</span><span class="mi">1_073_741_825</span><span class="o">);</span>
        <span class="kd">final</span> <span class="nc">BankAccount</span> <span class="n">accountTwo</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">BankAccount</span><span class="o">(-</span><span class="mi">1_073_741_823</span><span class="o">);</span>
        <span class="kd">final</span> <span class="kt">int</span> <span class="n">comparison</span> <span class="o">=</span> <span class="n">accountOne</span><span class="o">.</span><span class="na">compareTo</span><span class="o">(</span><span class="n">accountTwo</span><span class="o">);</span>
        <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">comparison</span><span class="o">);</span> <span class="c1">// 1</span>
    <span class="o">}</span>

    <span class="kd">private</span> <span class="n">record</span> <span class="nf">BankAccount</span><span class="o">(</span><span class="kt">int</span> <span class="n">balance</span><span class="o">)</span> <span class="o">{</span>
        <span class="kd">public</span> <span class="kt">int</span> <span class="nf">compareTo</span><span class="o">(</span><span class="nc">BankAccount</span> <span class="n">anotherAccount</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="nc">Integer</span><span class="o">.</span><span class="na">compare</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="na">balance</span><span class="o">,</span> <span class="n">anotherAccount</span><span class="o">.</span><span class="na">balance</span><span class="o">);</span>
        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Comparison value is 1.
<br />
<br /></p>
<h3 id="floating-point-numbers">Floating Point Numbers</h3>
<p>It’s 2022 and you are using floating point numbers. You can add a positive number to 1 and get a value less than 1.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>jshell&gt; DoubleStream.of(1.0, 1e-16).sum();
$2 ==&gt; 0.9999999999999999
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>jshell&gt; DoubleStream.of(1.0, 1e-16).reduce(0, Double::sum);
$3 ==&gt; 1.0
</code></pre></div></div>
<p><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/DoubleStream.html#sum()">DoubleStream</a> <code class="language-plaintext highlighter-rouge">double sum();</code></p>
<blockquote>
  <p>Returns the sum of elements in this stream. Summation is a special case of a reduction. If floating-point summation 
were exact, this method would be equivalent to: return reduce(0, Double::sum);
However, since floating-point summation is not exact, the above code is not necessarily equivalent to the summation 
computation done by this method. The value of a floating-point sum is a function both of the input values as well as 
the order of addition operations. The order of addition operations of this method is intentionally not defined to 
allow for implementation flexibility to improve the speed and accuracy of the computed result. In particular, this 
method may be implemented using compensated summation or other technique to reduce the error bound in the numerical 
sum compared to a simple summation of double values. Because of the unspecified order of operations and the possibility 
of using differing summation schemes, the output of this method may vary on the same input elements.</p>
</blockquote>

<p>Floating point addition is not commutative and I assume this is based on the 
<a href="https://en.wikipedia.org/wiki/Kahan_summation_algorithm">Kahan sum</a> implementation.</p>

<p>Remember, decimal literals are of type <code class="language-plaintext highlighter-rouge">double</code> and need to be cast down to <code class="language-plaintext highlighter-rouge">float</code>.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="kt">float</span> <span class="n">error</span> <span class="o">=</span> <span class="mf">0.0</span><span class="o">;</span> <span class="c1">// compile error</span>
<span class="kd">final</span> <span class="kt">float</span> <span class="n">error</span> <span class="o">=</span> <span class="mf">0.0</span><span class="no">F</span><span class="o">;</span>
</code></pre></div></div>
<p>But an <code class="language-plaintext highlighter-rouge">int</code> literal is only of type <code class="language-plaintext highlighter-rouge">long</code> if it ends with an <code class="language-plaintext highlighter-rouge">L</code>.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="kt">long</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">0L</span><span class="o">;</span>
</code></pre></div></div>
<p>It needs to be cast up to a long. Not the consistency we would expect.
<br />
<br /></p>

<h3 id="zero">Zero</h3>
<p>Lets not forget about <code class="language-plaintext highlighter-rouge">+0</code> and <code class="language-plaintext highlighter-rouge">-0</code>. The <a href="https://en.wikipedia.org/wiki/IEEE_754-2008_revision">IEEE-754</a> standard 
has two zeros. When you divide by each one, you get a different result.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="mf">0.0</span><span class="o">);</span> <span class="c1">// Infinity</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="o">-</span><span class="mf">0.0</span><span class="o">);</span> <span class="c1">// -Infinity</span>
</code></pre></div></div>
<p>But…</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(-</span><span class="mf">0.0</span> <span class="o">==</span> <span class="mf">0.0</span><span class="o">);</span> <span class="c1">// true</span>
</code></pre></div></div>
<p>…even though <code class="language-plaintext highlighter-rouge">-0.0</code> and <code class="language-plaintext highlighter-rouge">0.0</code> hava different bit patterns. Care must be taken when implementing the absolute
value of a number.
<br />
<br /></p>
<h3 id="modular-arithmetic">Modular arithmetic</h3>
<p>I’ve been burned by this a lot. I enjoy <a href="https://projecteuler.net/">Project Euler</a>,
<a href="https://rosalind.info/problems/locations/">Rosalind</a> and <a href="https://adventofcode.com/">Advent of Code</a>. When I think of
modulus, I think in terms of remainder classes. For any modulus <code class="language-plaintext highlighter-rouge">n</code>, there exists <code class="language-plaintext highlighter-rouge">n - 1</code> remainder classes denoted as the
<a href="https://en.wikipedia.org/wiki/Modular_arithmetic#Residue_systems">least residue system modulo n</a>. The set of integers 
<br /><code class="language-plaintext highlighter-rouge">{0, 1, 2, ... , n − 1}</code> less than <code class="language-plaintext highlighter-rouge">n</code> from 
<a href="https://en.wikipedia.org/wiki/Euclidean_division">Euclidean division</a>.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that

a = bq + r
and

0 ≤ r &lt; |b|
</code></pre></div></div>

<p>In Java, the <code class="language-plaintext highlighter-rouge">%</code> is the remainder operator and <code class="language-plaintext highlighter-rouge">Math.floorMod()</code> is the modulo operator which is consistent with
the Euclidean definition where the remainder is always non-negative, <code class="language-plaintext highlighter-rouge">0 ≤ r</code>, and is thus consistent with the 
<a href="https://en.wikipedia.org/wiki/Euclidean_division">Euclidean division algorithm</a>. Each language may have a different 
<a href="https://en.wikipedia.org/wiki/Modulo_operation#Variants_of_the_definition">definition</a> of this operation.
<br /></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(-</span><span class="mi">18</span> <span class="o">%</span> <span class="mi">5</span><span class="o">);</span>               <span class="c1">// -3</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="nc">Math</span><span class="o">.</span><span class="na">floorMod</span><span class="o">(-</span><span class="mi">18</span><span class="o">,</span> <span class="mi">5</span><span class="o">));</span> <span class="c1">// 2</span>
</code></pre></div></div>
<p><br /></p>

<h3 id="url-class">URL class</h3>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="nc">InterruptedException</span><span class="o">,</span> <span class="nc">MalformedURLException</span> <span class="o">{</span>
    <span class="kd">final</span> <span class="nc">Set</span><span class="o">&lt;</span><span class="no">URL</span><span class="o">&gt;</span> <span class="n">domains</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">HashSet</span><span class="o">&lt;&gt;();</span>
    <span class="n">domains</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="k">new</span> <span class="no">URL</span><span class="o">(</span><span class="s">"https://www.google.com/"</span><span class="o">));</span>
    <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">domains</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="k">new</span> <span class="no">URL</span><span class="o">(</span><span class="s">"https://www.google.com/"</span><span class="o">)));</span> <span class="c1">// true</span>
    <span class="c1">// Wait 5 minutes</span>
    <span class="nc">Thread</span><span class="o">.</span><span class="na">sleep</span><span class="o">(</span><span class="mi">300_000</span><span class="o">);</span>
    <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">domains</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="k">new</span> <span class="no">URL</span><span class="o">(</span><span class="s">"https://www.google.com/"</span><span class="o">)));</span> <span class="c1">// false</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Java resolves domain names in URLs into IP addresses before generating hash codes for them. If you wait 5 minutes,
Google will load balance to a new IP address, and any new URLs you construct will have different hash codes.
This is well documented but serves as a reminder.
<br />
<br /></p>
<h3 id="boxed-types">Boxed Types</h3>
<p>Just a reminder when using boxed types…
<br />
<br />
<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html">List interface</a>
<br />
<img src="/assets/api/java-list-api-remove.png" alt="List-api" />
<br />
<br /></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">List</span><span class="o">&lt;</span><span class="nc">Integer</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="nc">Integer</span><span class="o">&gt;(</span><span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="mi">2</span><span class="o">,</span><span class="mi">3</span><span class="o">));</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">first</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="n">list</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span><span class="n">first</span><span class="o">);</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">list</span><span class="o">);</span>
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[1, 3]
</code></pre></div></div>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">List</span><span class="o">&lt;</span><span class="nc">Integer</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ArrayList</span><span class="o">&lt;</span><span class="nc">Integer</span><span class="o">&gt;(</span><span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="mi">2</span><span class="o">,</span><span class="mi">3</span><span class="o">));</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">first</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="n">list</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span><span class="n">first</span><span class="o">);</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">list</span><span class="o">);</span>
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[2, 3]
</code></pre></div></div>
<p>Collections only use boxed (object) types but it’s not common to use a boxed type otherwise unless you are 
expecting a <code class="language-plaintext highlighter-rouge">null</code> or <code class="language-plaintext highlighter-rouge">Optional</code> check. Just a reminder…</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1000</span><span class="o">;</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">1000</span><span class="o">;</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="o">);</span> <span class="c1">// true</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">128</span><span class="o">;</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">d</span> <span class="o">=</span> <span class="mi">128</span><span class="o">;</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">c</span> <span class="o">==</span> <span class="n">d</span><span class="o">);</span> <span class="c1">// false</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">e</span> <span class="o">=</span> <span class="mi">127</span><span class="o">;</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">f</span> <span class="o">=</span> <span class="mi">127</span><span class="o">;</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">e</span> <span class="o">==</span> <span class="n">f</span><span class="o">);</span> <span class="c1">// true</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">g</span> <span class="o">=</span> <span class="mi">1000</span><span class="o">;</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">g</span><span class="o">);</span> <span class="c1">// true: g is unboxed to a primitive.</span>
</code></pre></div></div>
<p><a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7">Boxing Conversion</a></p>
<blockquote>
  <p>If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1),
or the boolean literal true or false (§3.10.3), or a character literal between ‘\u0000’ and ‘\u007f’
inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the
case that a == b.</p>
</blockquote>

<p>But <code class="language-plaintext highlighter-rouge">-128 &lt; 10 &lt; 127</code>.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">Integer</span> <span class="n">m</span> <span class="o">=</span> <span class="mi">10</span><span class="o">;</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span><span class="o">;</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">m</span> <span class="o">==</span> <span class="n">n</span><span class="o">);</span> <span class="c1">// true</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">x</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span>
<span class="kd">final</span> <span class="nc">Integer</span> <span class="n">y</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Integer</span><span class="o">(</span><span class="mi">10</span><span class="o">);</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="o">);</span> <span class="c1">// false</span>
</code></pre></div></div>
<p><code class="language-plaintext highlighter-rouge">Integer(int)</code> can be fooled but it is deprecated and marked for removal.</p>

<p>Remember, use <code class="language-plaintext highlighter-rouge">if (x.intValue() == y.intValue())</code> or <code class="language-plaintext highlighter-rouge">if (x.equals(y))</code>.
<br />
<br /></p>
<h3 id="stringbuilder">StringBuilder</h3>
<p>Remember…</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">StringBuilder</span> <span class="n">stringBuilder</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">StringBuilder</span><span class="o">(</span><span class="sc">'['</span><span class="o">);</span>
</code></pre></div></div>
<p>does not add a <code class="language-plaintext highlighter-rouge">[</code> to the beginning as intended. The <code class="language-plaintext highlighter-rouge">char</code> passed in has it’s <code class="language-plaintext highlighter-rouge">int</code> value <code class="language-plaintext highlighter-rouge">91</code> used as the initial
capacity of the <code class="language-plaintext highlighter-rouge">StringBuilder</code> object.
<br />
<br /></p>
<h3 id="string">String</h3>
<p>You can say that format string vulnerabilities are not a thing in Java, but <code class="language-plaintext highlighter-rouge">String.format("%2147483647%")</code> is not 
your friend. It tries to create a String of length <code class="language-plaintext highlighter-rouge">Integer.MAX_VALUE</code>, that is 2GB. To make it succeed 
(assuming your heap size is big enough), pick a slightly smaller size.</p>

<p>Remember, String literals get interned.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">String</span> <span class="n">s1</span> <span class="o">=</span> <span class="s">"hello"</span><span class="o">;</span>
<span class="kd">final</span> <span class="nc">String</span> <span class="n">s2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">String</span><span class="o">(</span><span class="s">"hello"</span><span class="o">).</span><span class="na">intern</span><span class="o">();</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">s1</span> <span class="o">==</span> <span class="n">s2</span><span class="o">);</span> <span class="c1">// true</span>
</code></pre></div></div>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">String</span> <span class="n">s1</span> <span class="o">=</span> <span class="s">"hello"</span><span class="o">;</span>
<span class="kd">final</span> <span class="nc">String</span> <span class="n">s2</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">String</span><span class="o">(</span><span class="s">"hello"</span><span class="o">);</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">s1</span> <span class="o">==</span> <span class="n">s2</span><span class="o">);</span> <span class="c1">// false</span>
</code></pre></div></div>
<p>…but don’t confuse reference comparison with content comparison. Use <code class="language-plaintext highlighter-rouge">s1.equals(s2)</code>.
<br />
<br />
Remember, as with any programing language or tool, just because you can doesn’t mean you should.</p>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[Some reminders when working with Java…if that is what you want to call them. Math.abs() can return a negative value. ``` jshell&gt; Math.abs(Integer.MIN_VALUE) $1 ==&gt; -2147483648]]></summary></entry><entry><title type="html">Zero Width Space</title><link href="/jekyll/update/2021/06/08/zero-width-space.html" rel="alternate" type="text/html" title="Zero Width Space" /><published>2021-06-08T17:00:00+00:00</published><updated>2021-06-08T17:00:00+00:00</updated><id>/jekyll/update/2021/06/08/zero-width-space</id><content type="html" xml:base="/jekyll/update/2021/06/08/zero-width-space.html"><![CDATA[<p>I was working on an open source project recently and came across a couple issues that contained <code class="language-plaintext highlighter-rouge">[ZWSP]</code> 
displayed in the test files. The <code class="language-plaintext highlighter-rouge">[ZWSP]</code> was only displayed in the IntelliJ IDE. Eclipse, Atom and 
vim showed no issues with the code. The <code class="language-plaintext highlighter-rouge">[ZWSP]</code> stands for zero-width space. It was breaking the test file
and there were multiple issues in the github repository documenting it.</p>

<p>I found nine zero-width space characters in the <a href="https://unicode-table.com/en/">unicode characters table</a>
and there may be more.</p>

<table>
  <thead>
    <tr>
      <th>Character</th>
      <th>Unicode</th>
      <th>Hex</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Zero-width space</td>
      <td>U+200B</td>
      <td>0xe2 0x80 0x8b</td>
    </tr>
    <tr>
      <td>Zero-width non-joiner</td>
      <td>U+200C</td>
      <td>0xe2 0x80 0x8c</td>
    </tr>
    <tr>
      <td>Zero-width joiner</td>
      <td>U+200D</td>
      <td>0xe2 0x80 0x8d</td>
    </tr>
    <tr>
      <td>Left-To-Right Mark</td>
      <td>U+200E</td>
      <td>0xe2 0x80 0x8e</td>
    </tr>
    <tr>
      <td>Right-To-Left Mark</td>
      <td>U+200F</td>
      <td>0xe2 0x80 0x8f</td>
    </tr>
    <tr>
      <td>Left-To-Right Embedding</td>
      <td>U+202A</td>
      <td>0xe2 0x80 0xaa</td>
    </tr>
    <tr>
      <td>Right-To-Left Embedding</td>
      <td>U+202B</td>
      <td>0xe2 0x80 0xab</td>
    </tr>
    <tr>
      <td>Word joiner</td>
      <td>U+2060</td>
      <td>0xe2 0x81 0xa0</td>
    </tr>
    <tr>
      <td>Zero-width no-break space</td>
      <td>U+FEFF</td>
      <td>0xef 0xbb 0xbf</td>
    </tr>
  </tbody>
</table>

<p>Zero-width characters are non-printing characters that are not displayed by most applications. They are Unicode 
characters, typically used to mark possible line breaks or join/separate characters in writing systems that use ligatures.
They are not removed if the text is formatted, copied or pasted. It’s really hard to detect them without special tools.
It most likely happened from copy and pasting code from the <a href="https://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/AbstractIterableAssert.html#containsExactlyInAnyOrder(ELEMENT...)">AssertJ API</a>.</p>

<p>I used a tool called <a href="https://plugins.jetbrains.com/plugin/10-hexview">HexView</a> to view the hexidecimal
format of the file and inspect for it.</p>

<p><img src="/assets/hexview/contains-zero-width-space.png" alt="hexview" /></p>

<p>HexView allowed me to inspect the code using the column on the right. As you can see, it is not the IDE or editor,
the value is in the actual code. I removed the hex value<br /> <code class="language-plaintext highlighter-rouge">e2 80 8b</code><br />
<br />
<img src="/assets/hexview/removed-zero-width-space.png" alt="hexview" /></p>

<p>It was interesting to note that IntelliJ was the only IDE or editor to pick up the zwsp. Users that did
not use IntelliJ experienced no issues.</p>

<p>The ability to use invisible characters with no width has serious security implications. Users could create 
usernames, email addresses and websites that look identical to a human, but different to computers. 
Luckily, zero-width spaces are prohibited in email addresses and domain names. See 
<a href="https://en.wikipedia.org/wiki/IDN_homograph_attack#Non-displayable_characters">homograph attack</a> for
an example. Zero-width spaces can hide an encoded binary message in a piece of text. 
<code class="language-plaintext highlighter-rouge">String -&gt; byte -&gt; bit -&gt; 1 -&gt; ZWSP and 0 -&gt; ZWNJ</code>. They can be added to sensitive documents, hidden in 
different places for each of the recipients, creating a unique finger print to identify the source.</p>

<p>This reminded me of hidden dots and values produced by printers. Surely, there is a tool to inspect for these. 
Insert <a href="https://github.com/dfd-tud/deda">DEDA</a>, created from the researchers of 
<a href="/assets/hexview/Forensic-Analysis-and-Anonymisation-of-Printed-Documents.pdf">Forensic Analysis and Anonymisation of Printed Documents</a>.
I’m not sure how valuable or effective it is but I will dig into it.</p>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[I was working on an open source project recently and came across a couple issues that contained [ZWSP] displayed in the test files. The [ZWSP] was only displayed in the IntelliJ IDE. Eclipse, Atom and vim showed no issues with the code. The [ZWSP] stands for zero-width space. It was breaking the test file and there were multiple issues in the github repository documenting it.]]></summary></entry><entry><title type="html">IntelliJ Java Webapp Gradle Build Failure</title><link href="/jekyll/update/2020/11/18/intellij-ultimate-webapp-gradle-build.html" rel="alternate" type="text/html" title="IntelliJ Java Webapp Gradle Build Failure" /><published>2020-11-18T12:00:00+00:00</published><updated>2020-11-18T12:00:00+00:00</updated><id>/jekyll/update/2020/11/18/intellij-ultimate-webapp-gradle-build</id><content type="html" xml:base="/jekyll/update/2020/11/18/intellij-ultimate-webapp-gradle-build.html"><![CDATA[<p>I have a good experience with JetBrains products IntelliJ, GoLand, RubyMine etc 90% of the time.
IDE’s, however, tend to do things “automagically” that a standard text editor and command line don’t provide.
Configuring the GOPATH in GoLand, obscuring directories in IntelliJ and the Gradle build default settings
that inspired this post, are a few things that can inhibit the learning process with regards to how things
should and actually work.</p>

<p>I won a one year license at a <a href="https://www.meetup.com/PhillyJUG/">PhillyJUG</a> meetup raffle and became hooked. 
The IntelliJ Ultimate edition provides server integration, a significant amount of framework support as well as a 
team of support people to resolve your issues that the community edition does not have. You can view a
<a href="https://www.jetbrains.com/idea/features/editions_comparison_matrix.html">comparison</a>. The server support
is a mere convenience and isn’t required. It does create a more pleasant experience over the verbose Tomcat
command line server arguments or the Gradle Jetty plugin configuration.</p>

<p>I keep getting burned every time I create a new Java webapp project in IntelliJ. 
When deploying the war file to the server, IntelliJ can not find it. I forget this issue exists because 
I don’t create Java webapps that often. I’m using Spring Boot or Dropwizard mostly with a built in server. 
I end up wasting an hour or so trying to understand the problem until I remember the issue. It is a 
documented issue with the default gradle build process. The issue can be found
<a href="https://youtrack.jetbrains.com/issue/IDEA-176700">here</a>, as well as a 
<a href="https://youtrack.jetbrains.com/issue/IDEA-178450#focus=streamItem-27-4068591.0-0">work around</a>, but it
hasn’t been integrated into updated releases for some reason. The comments on the issue page echo this. 
The first time this happened, I dug for hours before I ended up opening a support ticket. The team resolved
my issue within a few hours. Below is an illustration of the steps.</p>

<p><br />
Initial setup structure.
<img src="/assets/intellij/project-structure.png" alt="project structure" /></p>

<p>Adding Tomcat configuration with the exploded war artifact.
<img src="/assets/intellij/server-setup.png" alt="server setup" /></p>

<p>The war file can not be found with the default gradle build setting.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[2020-11-18 08:13:42,572] Artifact Gradle : org.example : spring-rest-demo-1.0-SNAPSHOT.war (exploded): 
com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException:
/Users/ericbalawejder/Development/spring-rest-demo/build/libs/exploded/spring-rest-demo-1.0-SNAPSHOT.war
not found for the web module.
</code></pre></div></div>
<p>Select the following menu settings:
<br />
<code class="language-plaintext highlighter-rouge">IntelliJ IDEA -&gt; Preferences -&gt; Build, Execution, Deployment -&gt; Build Tools -&gt; Gradle</code>
<img src="/assets/intellij/gradle-default.png" alt="gradle default" /></p>

<p>Change to build and run with IntelliJ.
<img src="/assets/intellij/build-with-intellij.png" alt="build with intellij" /></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Connected to server
[2020-11-18 08:20:25,110] Artifact Gradle : org.example : spring-rest-demo-1.0-SNAPSHOT.war (exploded):
Artifact is being deployed, please wait...
[2020-11-18 08:20:25,564] Artifact Gradle : org.example : spring-rest-demo-1.0-SNAPSHOT.war (exploded):
Artifact is deployed successfully
[2020-11-18 08:20:25,564] Artifact Gradle : org.example : spring-rest-demo-1.0-SNAPSHOT.war (exploded):
Deploy took 454 milliseconds
</code></pre></div></div>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[I have a good experience with JetBrains products IntelliJ, GoLand, RubyMine etc 90% of the time. IDE’s, however, tend to do things “automagically” that a standard text editor and command line don’t provide. Configuring the GOPATH in GoLand, obscuring directories in IntelliJ and the Gradle build default settings that inspired this post, are a few things that can inhibit the learning process with regards to how things should and actually work.]]></summary></entry><entry><title type="html">Thoughts on Monopolies</title><link href="/jekyll/update/2020/04/02/a-note-on-monopolies.html" rel="alternate" type="text/html" title="Thoughts on Monopolies" /><published>2020-04-02T17:00:00+00:00</published><updated>2020-04-02T17:00:00+00:00</updated><id>/jekyll/update/2020/04/02/a-note-on-monopolies</id><content type="html" xml:base="/jekyll/update/2020/04/02/a-note-on-monopolies.html"><![CDATA[<p>I received an e-mail from Meetup.com a couple days ago. I enjoy using Meetup to explore my local
software community. WeWork acquired Meetup in late 2017. After the recent debacle and Adam Neumann
departure (I’m being nice here), Meetup announced fees for attendees and the community began to scramble
for alternatives. Shortly after the community outcry caught attention, fees were scrubbed just before
the new year. There were a few lines in the e-mail that caught my attention:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Dear Meetup members,

Today we’re announcing that Meetup is no longer a subsidiary of WeWork and has 
been acquired by a group of investors dedicated to continuing our mission. 
This was a planned acquisition and is not related to the coronavirus. With 
these new investors by our side, we’ll continue to create real human 
connections, support our organizers, and grow Meetup to its fullest potential 
with you.
</code></pre></div></div>

<p>This interested me because of the recent legislation passed for the COVID-19 crisis. Even thought the CEO
states his position about it being in the works well before COVID-19, it brought to light the possible
fallout. Acquisitions and mergers lead to monopolies. A dangerous concetration of power in the hands of
a few who misuse their power. We see this in almost every major industry with coercive contracts, secret
rebates, retaliation, and political corruption. The “buy to kill” technique follow by the “price
adjusting” one that stifles innovation, consolidates supply chains and promotes intellectual laziness.
It’s commonly sold to the people under the term “free market” where institutions are organized entirely
around the short-term horizon of financiers whose actual goal is to create a monopoly. A hierarchy where
people who do the actual work have little control over it and watch their leverage in the work force
decline. The Fed shifted hundreds of billions of dollars to some of the most toxic private equity groups
in the country. Now, when small business are most vulnerable and big business is borrowing money for
free, this problem will grow.</p>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[I received an e-mail from Meetup.com a couple days ago. I enjoy using Meetup to explore my local software community. WeWork acquired Meetup in late 2017. After the recent debacle and Adam Neumann departure (I’m being nice here), Meetup announced fees for attendees and the community began to scramble for alternatives. Shortly after the community outcry caught attention, fees were scrubbed just before the new year. There were a few lines in the e-mail that caught my attention:]]></summary></entry></feed>