compare

How to compare floating-point numbers in Scala alvin July 10, 2017 - 2:00pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.5, “Comparing Floating-Point Numbers in Scala.”

Problem

You need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be.

How to test String equality in Scala alvin July 9, 2017 - 5:50pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”

Problem

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.

Solution

In Scala you compare two String instances with the == operator. Given these strings:

How to compare floating-point numbers in Scala

Scala FAQ: I need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be; how do I comparison floating-point numbers?

Solution

As in Java and many other languages, you solve this problem by creating a method that lets you specify the precision for your comparison. The following Scala “approximately equals” method demonstrates the approach:

How to compare String equality in Java

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.

A Java “instanceof null” example

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.

Perl if, else, elsif ("else if") syntax

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:

Perl - How to compare a string against multiple patterns

For a Perl program that I'm working on right now, I need to match the elements of an array of strings against several patterns, and then take an action if the current string matches one of my patterns. This is pretty easy in this case, in part because it's easy to match a string against multiple patterns in Perl, and also because my patterns are very simple -- no regular expressions involved.

Java String comparison FAQ: Why doesn't == work when comparing two String objects? alvin November 29, 2007 - 5:51pm

The == operator doesn't work when comparing two Java String objects, even if the Strings store the same content, because the == operator compares the two object references to each other. Since they aren't the same reference, they aren't equal.

In short, when comparing two Java String objects, compare them like this:

if (s1.equals(s2))
{
  // your code ...
}

and specifically do not compare two String objects like this:

A Java method that tests if a String is null or blank alvin July 7, 2007 - 5:51pm

Problem: You're working on a Java application, and you're repeatedly performing a test to determine whether a String (or many strings) are either blank or null.

Solution

Create a simple Java method to perform this test for you. The following Java method returns true if a String is blank or null, otherwise it returns false: