##字串是不可變的意味著我們不能改變物件本身,但是我們可以改變物件的參考。該字串被設定為最終字串,以不允許其他人擴展它並破壞其不變性。
public class StringImmutableDemo { public static void main(String[] args) { String st1 = "Tutorials"; String st2 = "Point"; System.out.println("The hascode of st1 = " + st1.hashCode()); System.out.println("The hascode of st2 = " + st2.hashCode()); st1 = st1 + st2; System.out.println("The Hashcode after st1 is changed : "+ st1.hashCode()); } }
The hascode of st1 = -594386763 The hascode of st2 = 77292912 The Hashcode after st1 is changed : 962735579
以上是為什麼在Java中String類別是不可變的或是final的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!