Durgasoft sucks

I already have a long rant on how JavaTpoint sucks. This one is about Durgasoft.

Continue reading “Durgasoft sucks”

Misnomer: static Keyword

The keyword static is arguably a bad choice or naming.

If you look up “static” in a dictionary you find many meanings, none of which explains its use in programming languages. So why is it used in Java, and how is it misunderstood?

Continue reading “Misnomer: static Keyword”

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.)

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”

Java Memory Model

At some point you will learn about concurrent programming. The Java Memory Model is the essential concept you need to learn. But it’s not easy to grasp as a beginner. I try to explain some of the most important aspects.

Continue reading “Java Memory Model”