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

  • Home
  • SEARCH
  • 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 979151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:10:30+00:00 2026-05-16T04:10:30+00:00

val filesHere = (new java.io.File(.)).listFiles val filesHere2 = (new java.io.File(.)).listFiles scala> filesHere == filesHere2

  • 0
val filesHere = (new java.io.File(".")).listFiles
val filesHere2 = (new java.io.File(".")).listFiles

scala> filesHere == filesHere2
res0: Boolean = false

That is quite counter intuitive. I would rather expect that filesHere and filesHere2 are equal.

This is certainly due to a semantics mismatch between Java and Scala, e.g., about arrays or (files) equality. Clearly, I am missing something here!

  • 1 1 Answer
  • 1 View
  • 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-05-16T04:10:31+00:00Added an answer on May 16, 2026 at 4:10 am

    You may want to read through here:

    http://ofps.oreilly.com/titles/9780596155957/AdvancedObjectOrientedProgramming.html#EqualityOfObjects

    but it appears that if you did:
    filesHere.sameElements(filesHere2) that it should be true.

    The javadoc for this is here:
    http://www.scala-lang.org/api/2.6.0/scala/IterableProxy.html#sameElements%28Iterable%5BB%5D%29

    UPDATE:

    A couple of snippets from the first link that may be helpful:

    In Java, C++, and C# the == operator tests for reference, not value equality. In contrast, Ruby’s == operator tests for value equality. Whatever language you’re used to, make sure to remember that in Scala, == is testing for value equality.

    In reference to == not working as expected on Lists:

    While this may seem like an inconsistency, encouraging an explicit test of the equality of two mutable data structures is a conservative approach on the part of the language designers. In the long run, it should save you from unexpected results in your conditionals.

    UPDATE 2:

    Based on comments from Raphael I looked at the specification, and it was dated two days ago for the most recent update, and I see these at http://www.scala-lang.org/files/archive/nightly/pdfs/ScalaReference.pdf:

    Method equals: (Any)Boolean is structural equality, where two instances
    are equal if they both belong to the case class in question and they have equal
    (with respect to equals) constructor arguments.
    
    class AnyRef extends Any {
        def equals(that: Any): Boolean = this eq that
        final def eq(that: AnyRef): Boolean = . . . // reference equality
    

    So, it appears that the definition hasn’t changed in Scala 2.10.2, as the specification seems to be consistent. If the behavior is different, then if you write a unit test it should be sent as a bug for Scala.

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

Sidebar

Related Questions

Consider this code: val foo = if(true) new java.lang.Double(4) else new java.lang.Integer(4) The inferred
val files = new File(data).list() val filtered = files.filter(name => name.contains(txn)) val masterList =
class A (names: String*) { val namesBuffer: ListBuffer[String] = new ListBuffer[String] } I was
You have: val array = new Array[Array[Cell]](height, width) How do you initialize all elements
$('#mapid').val($(.firstbox ul li:nth-child( + mapi + )).class()); is returning an undefined error that loops
val scalaToolsSnapshots = Scala-Tools Maven2 Snapshots Repository at http://scala-tools.org/repo-snapshots val specs = org.scala-tools.testing %
I have val maxId = new ObjectId(...) and I want to query something like
if ($('#requestForm').val == ) { alert('Please select at least one filter'); //return false; }
In Scala REPL: val input = <outerTag xmlns=http://xyz> <innerTag> </innerTag> </outerTag> input\\@innerTag => <innerTag
val m: java.util.Map[String, Int] = ... m.foreach { entry => val (key, value) =

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.