Published in CodeX·Jun 1Finer points of Java: the difference between nested, inner and anonymous classesA Java project usually has several classes, each defined in its own source file by itself. Sometimes, though, it’s convenient to put one Java class inside another. Here’s a toy example: package org.example; public class EnclosingClass { class EnclosedClass { } } If I referred to EnclosedClass as an “inner…Java8 min read
Published in CodeX·May 18Use cases for inner classes in JavaIn a linked list, each node has an obvious and predictable relationship to other nodes in the list. The nodes of a linked list can be implemented as instances of a private class nested in the linked list class. As we saw in my article about nested static classes, the…Java5 min read
Published in CodeX·May 5A couple of use cases for nested static classes in JavaWhenever you can use an anonymous class, you can use a named nested static class instead. But that only makes sense if you need to instantiate the class from two or more places. That’s probably not going to be the case with an iterator, but it’s likely to be the…Java3 min read
Published in CodeX·May 3A couple of use cases for anonymous classes in JavaWhen I first learned about anonymous classes in Java, I didn’t quite see the point. I understood the syntax well enough to recognize anonymous classes in applications of Java’s Abstract Window Toolkit (AWT). Those didn’t quite seem to make sense to me. You want a particular window to close when…Java5 min read
May 3Exploring the Collatz conjecture on the Scala REPLYesterday I wrote about the Collatz conjecture. I mentioned that you can use a computer to help you explore the Collatz conjecture. If you have Scala on your computer, you can use the Scala Read-Eval-Print-Loop (REPL) sort of like a Mathematica or Python notebook. You can use the Scala REPL…Collatz Conjecture3 min read
May 2A look at the Collatz conjectureThe Collatz conjecture is like a lot of other conjectures in mathematics, in that it is very easy to explain but seems difficult and perhaps impossible to prove or disprove. Pick an integer. If it’s even, halve it. For example, pick 26. Half of that is 13. If the number…Mathematics4 min read
Published in Dev Genius·Apr 26Data structures exercise: Generalizing mutable array-backed collections in Java with an abstract superclassIf you did both the exercise for array-backed sets and the exercise for array-backed lists, you probably noticed that they have a lot in common. …Java28 min read
Apr 1Near misses for disproving Fermat’s conjectureAccording to Fermat’s so-called “last theorem,” the equation x³ + y³ = z³ has no solution in integers. Neither does x⁴ + y⁴ = z⁴, nor x⁵ + y⁵ = z⁵, and so on and so forth. If the exponent is an integer greater than 2, then at least one…Mathematics2 min read
Published in CodeX·Mar 28Make your Java comments more informative and actionableHow to make Java comments your coworkers will love — There are two extremes of opinion regarding comments in a Java program, or in a program in any language. Comments are always good because supposedly you’ll be glad to see them a year down the line. Comments are always bad because they can easily get outdated, so you should never…Java8 min read
Published in Dev Genius·Feb 19Checklist: When to write custom exceptions in JavaJava allows us to write custom exceptions. Suppose you want an exception called “MyCustomException” in the com.example package. It’s that simple. Just by extending RuntimeException (or Exception if we need this to be a checked exception), we have a new custom exception that can be thrown and caught just…Java5 min read