Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

Javaالدالة hashCode() في جافا

تعريفها

ترجع قيمة الـ hash code للـ String الذي قام باستدعائها.



الخوارزمية التي يطبقها مترجم لغة جافا لحساب قيمة الـ hash code:

Hash Code = s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1].



بناؤها

public int hashCode()
public int hashCode()


باراميترات

  • لا تقبل أي باراميتر.


قيمة الإرجاع

ترجع رقم يمثل قيمة الـ hash code للـ String الذي قام باستدعائها.



مثال

public class Main {
public static void main(String[] args) {
String s = "welcome to harmash.com";
System.out.println( "The hascode of s is: " + s.hashCode() );
}
}
public class Main { public static void main(String[] args) { String s = "welcome to harmash.com"; System.out.println( "The hascode of s is: " + s.hashCode() ); } }

سنحصل على النتيجة التالية عند التشغيل.

The hashcode of s is: 1813959918
The hashcode of s is: 1813959918