-11

I know in java we can use the .equals method to compare if two strings are equal. But what if the 2 strings are extremely long such as the following:

String s1 = "t was her sister Josephine who told her, in broken sentences; veiled hints that revealed in half concealing. Her husband's friend Richards was there, too, near her. It was he who had been in the newspaper office when intelligence of the railroad disaster was received, with Brently Mallard's name leading the"

String s2 = "t was her sister Josephine who told her, in broken sentences; veiled hints that revealed in half concealing. Her husband's friend Richards was there, too, near her. It was he who had been in the newspaper office when intelligence of the railroad disaster was received, with Brently Mallard's name leading the"


if s1.equals(s2) return true;

will .equals still work if the strings being compared are huge? is there any limit? I need to know what is the BEST way to compare 2 long strings.

jack asmack
  • 9
  • 1
  • 4
  • what happened when you tried it? – ControlAltDel May 10 '16 at 14:24
  • 1
    Yes it will work, the sentence you have given is not at all long for it to handle. – Pradeep Simha May 10 '16 at 14:25
  • 1
    *is there any limit?* yes .... if String is longer then 3 chars then equals return random value ... Seriously? String.equals is well defined ... String length limitation, too ... just add 1 + 1 – Selvin May 10 '16 at 14:25
  • 1
    Unless you developing for very very small embedded devices ... you should probably try to get a good dose of "reality sense". It seems that you very much rely on *assumptions* about reality. What I am saying: there is the LMAX system out there, running on the JVM, that has an **average** latency for internal handing of requests of 100 microseconds. And that system processes tons and tons of requests per second. In other words: the above comparison isn't expensive in 2016; by no means. – GhostCat May 10 '16 at 14:36

2 Answers2

3

will .equals still work if the strings being compared are huge?

Yes

is there any limit?

Only the maximum size of a Java string (2^31 - 1 characters) or limit imposed by the size of the heap.

(But those are limits on the size of the strings themselves not on the equals method.)

I need to know what is the BEST way to compare 2 long strings.

Using String.equals(Object)

If you have multiple strings and / or you want to compare them multiple times, then using String.hashCode() in conjunction with String.equals may improve performance. (It depends on the nature of the strings being compared; i.e. the probability that the pairs are equal versus "nearly" equal.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

Yes it will work. Might take some time (most probably will not even take a second). But don't underestimate the compiling power of modern devices. It will compare in a blink of an eye.

daxgirl
  • 762
  • 4
  • 10
  • but are there any limits to the equals method ? – jack asmack May 10 '16 at 14:37
  • I need to know what is the BEST way to compare 2 long strings. – jack asmack May 10 '16 at 14:39
  • You can use equals safely. – daxgirl May 10 '16 at 14:40
  • 1
    "might", "some time", "most probably", "not even a second", "in a blink of an eye"... This is so vague I'm not sure it should be posted as an answer. – m69's been on strike for years May 10 '16 at 15:12
  • OK. Let me put it straight and less vague. Yes it will work. The limitations if any are enormous. Any imaginable string will be parsed and compared very fast. If it blocks the ui Thread, may be ran on background thread. It should not. – daxgirl May 10 '16 at 15:15
  • In any case you can always down vote. Which is quite more appropriate than stating your opinion. – daxgirl May 10 '16 at 15:16
  • 3
    I thought a comment instead of a downvote was more likely to lead to an improved answer. No offense intended. (Some people even want to make comments mandatory when downvoting; see http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes ) – m69's been on strike for years May 10 '16 at 15:44
  • No offence taken. You may down vote if you find answer unsatisfactory. I do take your comment under advisement. And will be more concrete next time. Thank you! – daxgirl May 10 '16 at 15:45
  • Thank you for your remarks. Will keep in mind for the future. Please delete at your discretion if the moderation team finds necessary. – daxgirl May 10 '16 at 17:07