Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison in Scala

Tags:

scala

What is the difference between val a=new String("Hello") and val a="Hello"

Example:

val a="Hello"
val b="Hello"
a eq b
res:Boolean=True

Similarly:

val a=new String("Hello")
val b=new string("Hello")
a eq b
res:Bolean=False
like image 891
AAA Avatar asked Aug 16 '16 09:08

AAA


People also ask

What is == in Scala?

The operator == is the same as equals method. It is also used to compare the equality of values. The operator == returns True if both the values being compared are the same, and returns False otherwise.

Can you compare strings in Scala?

In Scala, the == method defined in the AnyRef class first checks for null values, and then calls the equals method on the first object (i.e., this ) to see if the two objects are equal. As a result, you don't have to check for null values when comparing strings.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

Does Scala use ==?

Methods: While == is an operator in several languages, Scala reserved The == equality for the natural equality of every type. it's a method in Scala, defined as final in Any. value equality will be tested by this. Here, x == y is true if both x and y have the same value.


2 Answers

eq compares memory references.

String literals are put in a string constants pool, so in the first example they share the same memory reference. This is a behavior that comes from Java (scala.String is built on top of java.lang.String).

In the second example you're allocating two instances at runtime so when you compare them they're are at different memory locations.

This is exactly the same as Java, so you can refer to this answer for more information: What is the difference between "text" and new String("text")?

Now, if you want to compare their values (as opposed to their memory references), you can use == (or equals) in Scala.

Example:

val a = new String("Hello")
val b = new String("Hello")
a eq b // false
a == b // true
a equals b // true

This is different than Java, where == is an operator that behaves like eq in Scala.

Also note that == and equals are slightly different in the way the deal with null values (== is generally advised). More on the subject: Whats the difference between == and .equals in Scala?

like image 91
Gabriele Petronella Avatar answered Sep 17 '22 13:09

Gabriele Petronella


First of all eq (and its opposite ne) are used for what is called reference equality.

The behavior you observed is the result of what's technically known as string interning and is the inherited behavior from Java. Scala makes use of java.util.String under the hood. You can observe this in the REPL:

scala> val s = "Hello World!"
s: String = Hello World!

scala> s.isInstanceOf[java.lang.String]
res1: Boolean = true

You can see a general explanation of eq, ne, and == here.

To learn about JVM string interning see this Wikipedia article.

like image 35
Scott Shipp Avatar answered Sep 21 '22 13:09

Scott Shipp