menu

String aplikasi kunci

oleh Mikhail Vorontsov

Artikel ini adalah normal ini pada sintaksis gula di Jawa 7 diperkenalkan pada kunci String tombol aplikasi yang meliputi detail operator.

Mari kita metode berikut:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
  public   int  switchtest  (  akhir   String  s )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kunci   ( s )  & amp; amp; nbsp; & Amp; Gamble; Nbsp;  { & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kasus   & amp; amp; quot; A & amp; quot;    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; AA amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   11  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kasus   & amp; amp; quot; B & amp; amp; quot;    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; BB & amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   22  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;   bawaan   & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; CC & amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   33  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; }  }  
 public int switchtest (akhir String s) {switch (s) {case "a": System.out.println ("aa" ); Putar 11; Kasus "B": System.out.println ("BB"); Putar 22; default: System.out.println ("CC"); 33 hasil; }} 

Dengan mengubah javac (Java kembali decompiled) kode berikut:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
  public   int  switchtest  (  String  var1 )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp;  byte  var3  =   -   1  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kunci   ( var1.  kode hash   (  )  )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kasus   97    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  jika   ( var1.  sama   (   & amp; quot; & amp; quot;  )  )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; var3  =   0  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; }  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Istirahat  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kasus   98    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  jika   ( var1.  sama   (   & amp; amp; quot; B & amp; quot;  )   )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; var3  =   1  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; }  & amp; amp; nbsp; & Amp; Gamble; Nbsp; }  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kunci   ( var3 )   { & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kasus   0    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; AA amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   11  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp;  kasus   1    & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; BB & amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   22  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp;   bawaan   & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  Sistem .  .  println   (  & amp; amp; quot; CC & amp; quot;  )  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp; & Amp; Gamble; Nbsp;  kembali   33  ;  & amp; amp; nbsp; & Amp; Gamble; Nbsp; }  }  
 public int switchtest (String var1) {byte var3 = -1; switch (di var1.hashco ()) {case 97: if (var1.equals ("a")) {var3 = 0; } Untuk memecahkan; Kasus 98: (("b") var1.equals) adalah {var3 = 1; }} Beralih (var3) {case 0: System.out.println ("AA"); Putar 11; Kasus 1: System.out.println ("BB"); Putar 22; default: System.out.println ("CC"); 33 hasil; }} 

Kode yang dihasilkan terdiri dari dua bagian:

  • Terjemahan oleh string berbeda int pertama kunci pernyataan dalam setiap kasus diterapkan. Berikut int berdasarkan
  • nyata kunci .

Pertama kunci berisi aslinya setiap terpisah String.hashCode kasus kunci string tag. Kode hash pertandingan dengan kode hash yang sama setelah satu set sebanding setiap kesetaraan interval. Dalam kebanyakan kasus penuh String.Equals akan berakhir dengan, sehingga kunci tag digunakan dua string kita cenderung mengatakan kode hash yang sama. Mengapa Anda tidak menggunakan

Setelah melihat kode yang dihasilkan menjadi jelas Empty sebagai saklar label: Pertama dimulai perhitungan kode hash Kode> kunci

mendasari int Apa yang bisa Anda katakan tentang kinerja? Dalam artikel saya sebelumnya saya, seorang Anda dapat menemukan satu (untuk situasi yang paling umum adalah baik-baik saja) diterapkan sebagai peta tertentu dengan ukuran tabel 20.

Akhirnya, harus mencatat String.hashCode Java implisit bagian dari spesifikasi bahasa dalam Melaksanakan  kemudian> String , transisi .class berisi file tidak dapat diubah tanpa melanggar.

aplikasi kunci String di Jawa Kinerja Tuning Guide untuk keluar pertama.

Leave a Reply

Your email address will not be published. Required fields are marked *