Image for post
Image for post
Photo by freestocks on Unsplash

Java provides both arrays and lists. So does Scala. Java lists have many advantages over arrays. In Scala, the advantages of lists over arrays are even greater than in Java.

Because most people learning Scala come at it from the perspective of Java, we’re going to start off by reviewing a couple of the ways in which arrays are declared and initialized in Java.

Here’s one way to declare an array of five specific integers in Java:

There are a couple of other ways to do it, but they all involve the “square brackets” and the “curly braces.” …

Image for post
Image for post
Photo by Yu (^^) on Unsplash

Usually, when I get a NullPointerException, it’s because I made some silly mistake in one of my Java source files that I can easily correct. Sometimes, though, it’s because some Java collections allow nulls to fall through the cracks, going undetected until they cause a problem.

Consider java.util.HashMap<K, V>. It provides a very convenient way to map key objects to value objects. Call the map’s put() to add a key-value pair, call get() to get the a value by its key.

As a toy example, suppose you’re working on a calendar program that gives an inspirational quote for each day. You have created a Quotation class that provides all the necessary functions and complies with all the best practices, including best practices for null safety. …

Image for post
Image for post
Photo by Michael Dziedzic on Unsplash

On one of those question-and-answer websites, someone asked: What is the point of Java access modifiers if you can always just change them? It’s a question that crossed my mind when I was starting to learn Java years ago.

The student learns what the access modifiers are, and their semantics. And hopefully the student is given some guidance as to how to choose a modifier in the most common situations.

For this article, I assume you already know what the access modifiers are. …

Image for post
Image for post
Photo by Markus Spiske on Unsplash

Using different combinations of access modifiers, Java programmers can create a system of very fine-grained access control in their Java projects. This is a good thing, it simplifies programming.

Access modifiers enable programmers to declare their intentions regarding encapsulation, and then to offload enforcement of those intentions to the computer, which is almost always better than the human mind at keeping track of such things.

If you’ve studied Java even just a little bit beyond Hello World, you have probably memorized the access modifiers and how they differ. Here’s a very quick refresher:

  • Public (public) means accessible from anywhere, subject to caveats for modules (Java 9 and later). …

Image for post
Image for post
Photo by Jason Pofahl on Unsplash

Access is an important element of encapsulation in object-oriented programming. For example: can an instance of a given class access the fields of an instance of another class? That depends on what access level that particular field has been designated with.

Java has four access levels for class fields and “methods”: public, protected, package private and class private (usually called just “private”). I’ve listed these in order from least restrictive to most restrictive.

The module system introduced in Java 9 allows for even more fine-grained control, but I really won’t be covering that in this article. I assume that you’re either using Java 8 or you’re using Java 9 or later with all your classes in the same module, such as the default unnamed module. …

Image for post
Image for post
Photo by Gleren Meneghin on Unsplash

Almost every Java student is taught early on that Java has four access levels: class private (often just “private”), package private, protected and public.

Something that is marked class private can only be accessed from the class that defines it. Something that is package private can be accessed from any class or interface (trait) in the same package.

Protected is package private plus all subclasses. And public members can be accessed from any class or interface, subject to caveats about modules for Java 9 and later.

Given that Martin Odersky designed Scala to interoperate with Java, it’s not surprising that Scala also has the concept of access levels, and access modifiers to specify those levels. …

The source of a Java function in green interspersed with a pangram in red.
The source of a Java function in green interspersed with a pangram in red.

When you write a program in Java or almost any other mainstream programming language, syntax highlighting is almost taken for granted. It’s all too easy to jump to the faulty conclusion that syntax highlighting makes every optional element of the programming language (e.g., Java) completely unnecessary.

For example, supposedly having to write “this.” to distinguish instance members from local variables is outdated, a relic of the past, something that we should never do anymore now that everyone is using the exact same integrated development environment (IDE) with the exact same settings.

Consider this equals() override for the Fraction class, which hews very closely to the IntelliJ IDEA…

Image for post
Image for post
Photo by Max Bender on Unsplash

One time I actually forgot how to write For loops in Java. I had to look it up. Fortunately these days, things like that are easy to look up. The typical example goes something like this:

This writes the numbers 1 to 10 to the console on separate lines.

Of course the iterator i will generally start at 0 rather than 1, but it can start at any other int; some people prefer to use less than or equal to rather than strictly less than for the Boolean condition, but you can use any Boolean condition on integers; and the increment doesn’t have to be 1, it can be any positive integer, or it can be a decrement, and the change doesn’t even have to be additive, it can be any int operation. …

Image for post
Image for post
Photo by Will Porada on Unsplash

Scala doesn’t have the static keyword like Java does. For an experienced Java programmer learning Scala, this raises a number of questions about programming in Scala things we take for granted in Java, such as:

  • How do you write a “static” utility class like java.lang.Math or JUnit’s Assertions?
  • How do you write a class that can only be instantiated once in a session?
  • How do you write a “main method” for a command line program?
  • How do you write “factory methods”?

The answer for each of these involves a special class declared with the object keyword instead of the class keyword. …

Image for post
Image for post
Photo by Thalia Ruiz on Unsplash

Sooner or later, any student learning Java learns that Java exceptions are propagated until reaching a handler. What are the consequences of that?

One consequence is that sometimes an exception only needs to be thrown from one place, and, by refactoring, you can make your program do the same thing and pass all the tests with fewer lines.

The word “propagate” suggests a spreading out in all directions. For example, if you throw a pebble in the middle of a pond, there are going to be ripples, roughly circular, radiating out. …


Alonso Del Arte

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store