Using WSL from Java

With Windows Subsystem for Linux (WSL) we can now use Linux tools, such as grep, rsync, ssh, and network commands, such as dig and netstat, on Windows directly. No need for cygwin or other 3rd party software.

Here’s an example on how uname is unknown to Windows, but WSL (Ubuntu in my case) knows it:

WSL can be used to run Linux commands, such as uname.

So when writing Java programs you might want to use this feature. There really isn’t much to say about this because youcan just easily use it. But note that WSL probably uses UTF-8 (as most linux systems are using UTF-8) while CMD /U uses UTF-16LE .

I assume you will need 64bit Java. There’s probably a way to run WSL (which is 64bit) from a 32bit executable. But it’s 2019 and I see no reason to run 32bit Java.

Often you run a simple command that returns a single line, such as which or uname. So my function returns a List of Strings, so you can easily get the first line:

System.out.println(execute("wsl uname -a", utf8).get(0));

There’s nothing really special to do. Just use Runtime​.getRuntime()​.exec(cmd) and read from the InputStream (stdout of the process).

You find the code is on pastebin:
https://pastebin.com/nWvEXxmt

Misconceptions about Dates and Time

Was 1900 a leap year and does every minute have 60 seconds?

In Java we have a new API for Date and Time since Java SE 8. Before that, there was already an API with mutable dates and fore some reason there was a date just for SQL.
Since many were not happy with it there was Joda-Time, which later became the basis for JSR-310 and is now found in the package “java.time”.

Continue reading “Misconceptions about Dates and Time”

Stalin-Sort

Stalin-Sort using divide and conquer.

I just love this idea:

I     came up with a single pass O(n) sort algorithm I call Stalin-Sort. U iterate   down the list of elements checking if they are in order. Any element which is  out of order is eliminated. At the end u have a sorted list

But when you have an input such as [42,1,2,3,4,5,6] you just get
[42] instead of [1,2,3,4,5,6]. You can get a better result if you use divide and conquer. And that is something Stalin would do, don’t you think? So I implemented a recursive solution in Java. It tries all possible results, which is not a single pass and therefore defeats the purpose of having a fast O(n) algorithm. And my implementation isn’t even in-place. A new data structure has to be created. That’s why I also added an implementation using a linked list, which is what Mathew describes.

The code is on pastebin: https://pastebin.com/rEmvZSiA

Recursive FizzBuzz

FizzBuzz in two lines of code.

The only import you need is one for System.out:

import static java.lang.System.out;

Simple two-liner:

static void f(int a, int z) {
	out.println(a % 15 < 1 ? "FizzBuzz" : a % 3 < 1 ? "Fizz" : a % 5 < 1 ? "Buzz" : a);
	if (a < z) f(1 + a, z);
}

Without using the literal “FizzBuzz”:

static void g(int a, int z) {
  int x = 0;
  if (a % 3 < 1) { out.print("Fizz"); ++x; }
  if (a % 5 < 1) { out.print("Buzz"); ++x; }
  if (x < 1) out.print(a);
  out.println();
  if (a < z) g(1 + a, z);
}

Note that I use x<1 instead of x==0 to save a single character.

Set-theoretic explanation of IEEE 754

Why is 0.1+0.2 not equal to 0.3?

The IEEE Standard for Floating-Point Arithmetic is confusing for beginners. Here I try to give an alternative explanation. It’s not my goal to make it easy. It simply isn’t easy. But this might help understand some aspects of floating point arithmetic.

Continue reading “Set-theoretic explanation of IEEE 754”

Y Combinator in Java 8

The Y Combinator written in Java 8 with nothing but lambdas.

The Y Combinator allows us to use recursion without actually using the recursion that Java already has. And Java 8 actually doesn’t support recursion in lambdas. So this is one way of solving the problem. The simpler one would be to use a recursive Java method and then use a method reference to it (as in MyMathsFunctions::factorial). This is to show that Curry was right and we can do it with nothing but lambda expressions. Since Java is strongly typed I also need an interface, which references itself in the type declaration. This is necessary because at some point we need to apply a function to itself.

There are enough wiki and blog pages and articles on this. Even videos. So I won’t explain it in details. It’s nothing new, just written in Java 8.

Code on Pastebin

The full Code is here: https://pastebin.com/M2RmRqdU

	private static <T> T apply(UnaryOperator<UnaryOperator<T>> fn, T arg) {
		return ((Function<UnaryOperator<UnaryOperator<T>>, UnaryOperator<T>>)
				a -> ((Fn2Op<T>) b -> b.apply(b))
				.apply(
					c -> a.apply(
						x -> c.apply(c).apply(x)
					)
				)).apply(fn).apply(arg);
	}

Encapsulation

In my blog I write about misconceptions. Encapsulation is something you learn when you study Java and OOP. But it seems that most books fail to truly explain the problems you want to solve with it and those you get by using it. Often it’s just a short chapter or even just a small part about the important concepts of OOP. This is leading to misconceptions and poor understanding of OOP.

(Note: I wrote this before the release of Java 10. Expect that some things are somewhat outdated.)

Continue reading “Encapsulation”

Understanding Java

The if statement is often translated as “if”, but “if and only if” is what it really means.

To be able to use a language you need to actually understand it. This may seem obvious, but books on Java usually don’t have much on understanding the language. When you learn French you will want to be able to translate from and to French. 

Continue reading “Understanding Java”