Scala problem: You want to define an
equals method for your class so you can compare object instances to each other.
If you’re new to Scala, a first thing to know is that object instances are compared with
"foo" == "foo" // true "foo" == "bar" // false "foo" == null // false null == "foo" // false 1 == 1 // true 1 == 2 // false 1d == 1.0d // true case class Person(name: String) Person("Jess") == Person("Jessie") // false
This is different than Java, which uses
== for primitive values and
equals for object comparisons.
I had never heard of the term “Yoda Conditions” until now, but I have seen them in some Java code where programmers put the constant first in an effort to avoid null pointer exceptions.
This page contains a collection of over 100 Scala
String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)
First, here are some basic uses of the Scala
String class to help get us warmed up:
If you ever need an example of a Unix/Linux shell script where you need to determine whether today is a weekend day, I can confirm that this code works:
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”
When using Scala, you want to compare two strings to see if they’re equal, i.e., whether they contain the exact same sequence of characters.
In Scala you compare two String instances with the
== operator. Given these strings:
Java String comparison FAQ: Can you share some examples of how to compare strings in Java?
If you’re like me, when I first started using Java, I wanted to use the
== operator to test whether two
String instances were equal, but that’s not the correct way to do it in Java.
I was doing a little Scala programming this morning, and because I hadn't written any code in a while, I managed to forget how isInstanceOf works with inheritance in Scala.
To refresh my memory, I wrote the following example code:
While working with various "Java instanceof" tests recently, my curiosity was piqued, and I thought I'd take a look at how the instanceof operator works when testing against a Java array.
You might think that when the Java
instanceof operator is used to test a
null reference, such as a
null reference to a String,
instanceof would realize that even though the reference is currently
null, it is a reference to a
String, so you might expect
instanceof to return
true ... but it doesn't.
Summary: This tutorial shows a collection of Perl if, else, and else if examples.
Here are some examples of the Perl if/else syntax, including the “else if” syntax, which is really elsif. (I wrote this because after working with many different languages I can never remember the “else if” syntax for most languages, and
elsif is pretty rare.)
The Perl if/else syntax
The Perl if/else syntax is standard, I don’t have any problems here: