Is number comparison faster than string comparison?

前端 未结 5 1770
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 02:36

I heard that hashing (ie converting a string or object to a number) is used for strings and such because it is easier to compare numbers than strings. If true, what is the reaso

5条回答
  •  野的像风
    2021-02-04 03:13

    Comparing primitive numbers is definitely faster than comparing strings because it's just one computer instruction while comparing strings in Java is a method. But hashing in Java is used for a different reason, Object.hashCode() is used in hash tables for quick search in collections.

提交回复
热议问题