Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:05:44+00:00 2026-06-01T12:05:44+00:00

One would expect that even though strings are immutable, value-equality and reference-equality would not

  • 0

One would expect that even though strings are immutable, value-equality and reference-equality would not be the same for java.lang.String objects in Scala. This means that two string-holding vals should not be reference-equal even when their strings are identical. But here’s what I get in a 2.9.1.final REPL:

scala> val s1 = "a"; val s2 = "a"
s1: java.lang.String = a
s2: java.lang.String = a

scala> s1 eq s2
res0: Boolean = true

Any idea why the result was not false? The same experiment with List("a") instead of "a" works as expected. The eq method is marked as final in AnyRef. Is there any compiler magic done specifically for String or java.lang.String?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:05:45+00:00Added an answer on June 1, 2026 at 12:05 pm

    Yes it’s compiler magic. Specifically, it’s called interning. Java does it as well, and it’s simply for efficiency reasons, such as memory usage and allowing comparisons without comparing every character. Here’s a Wikipedia article on it. You can also intern Strings manually with the intern() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of pages in my site, as one would expect. For
To value initialize an object of type T , one would do something along
Why does this code delete only even elements within the array? I would expect
Why would anyone want to not use a code behind file so that server
I am curious which one would be better fitting as a currency field ?
I'm confused as to when one would choose AsyncTask over a Handler. Say I
There are many security reasons why one would want to drop an HTTP connection
I'm trying to understand why one would use Spring Batch over a scripting language
I am looking for a good explanation of why one would use sliding expiration
I'm new to using iterators and was wondering how one would iterate through each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.