[英]What is the difference between @Immutable and @Entity(mutable=false) when using Hibernate
[英]What is difference between mutable and immutable String in java
據我所知,
可以更改可變字符串,並且不能更改不可變字符串。
在這里,我想像這樣更改String的值,
String str="Good";
str=str+" Morning";
和其他方式是,
StringBuffer str= new StringBuffer("Good");
str.append(" Morning");
在這兩種情況下,我都試圖改變str
的價值。 任何人都可以告訴我,這兩種情況有什么不同,並給我清晰的可變和不可變對象的圖片。
String str = "Good";
str = str + " Morning";
在上面的代碼中,您將創建3個String
對象。
注意:字符串總是不可變的 。 沒有,像一個可變的字符串 。 str
只是一個參考 ,最終指向“早安”。 實際上,你不是在處理1
對象。 你有3
不同的String
對象。
StringBuffer str = new StringBuffer("Good");
str.append(" Morning");
StringBuffer
包含一個字符數組。 這是不相同的String
。 上面的代碼將字符添加到現有數組中。 實際上, StringBuffer
是可變的,它的String
表示不是。
java中的可變和不可變String之間有什么區別
不可變的存在,可變的不存在。
在Java中,所有字符串都是不可變的。 當您嘗試修改String
,您真正在做的是創建一個新的。 但是,當您使用StringBuilder
,實際上是在修改內容,而不是創建新內容。
Java String
是不可變的。
在第一個示例中,您正在更改對String
的引用 ,從而為其分配兩個其他Strings
組合的值: str + " Morning"
。
相反,可以通過其方法修改StringBuilder
或StringBuffer
。
當你說str
,你應該小心你的意思:
你的意思是變量 str
嗎?
或者你的意思是str
引用的對象 ?
在您的StringBuffer
示例中,您不會更改str
的值,並且在String
示例中,您不會更改String
對象的狀態。
體驗差異的最痛苦的方式是這樣的:
static void change(String in) {
in = in + " changed";
}
static void change(StringBuffer in) {
in.append(" changed");
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("value");
String str = "value";
change(sb);
change(str);
System.out.println("StringBuffer: "+sb);
System.out.println("String: "+str);
}
Java中的字符串是不可變的 。 然而,在編程上下文中可變的意思是第一個問題。 考慮下課,
public class Dimension {
private int height;
private int width;
public Dimenstion() {
}
public void setSize(int height, int width) {
this.height = height;
this.width = width;
}
public getHeight() {
return height;
}
public getWidth() {
return width;
}
}
現在,在創建Dimension
實例后,我們總是可以更新它的屬性。 注意,如果在其他意義狀態中的任何屬性可以更新為該類的實例,那么它被認為是可變的。 我們總能做到以下,
Dimension d = new Dimension();
d.setSize(10, 20);// Dimension changed
d.setSize(10, 200);// Dimension changed
d.setSize(100, 200);// Dimension changed
讓我們以不同的方式看到我們可以在Java中創建一個String。
String str1 = "Hey!";
String str2 = "Jack";
String str3 = new String("Hey Jack!");
String str4 = new String(new char[] {'H', 'e', 'y', '!'});
String str5 = str1 + str2;
str1 = "Hi !";
// ...
所以,
str1
和str2
是在String常量池中創建的字符串文字 str3
, str4
和str5
是放置在堆內存中的字符串對象 str1 = "Hi!";
創造"Hi!"
在String常量池中,它與"Hey!"
完全不同"Hey!"
哪個str1
早先引用。 這里我們創建String文字或String對象。 兩者都不同,我建議你閱讀以下帖子,了解更多相關信息。
在任何String聲明中,有一件事是常見的, 它不會修改但會被創建或轉移到其他。
String str = "Good"; // Create the String literal in String pool
str = str + " Morning"; // Create String with concatenation of str + "Morning"
|_____________________|
|- Step 1 : Concatenate "Good" and " Morning" with StringBuilder
|- Step 2 : assign reference of created "Good Morning" String Object to str
String如何變為不可變?
它是不變的行為,意味着,一旦分配的值不能以任何其他方式更新。 String類在內部保存字符數組中的數據。 而且,類被創建為不可變的。 看一下這個策略來定義不可變類。
移動參考並不意味着您改變了它的值。 如果你可以更新String類中場景后面的字符數組,那將是可變的。 但實際上,該陣列將被初始化一次,並且在整個程序中它仍然是相同的。
為什么StringBuffer是可變的?
正如您已經猜到的,StringBuffer類本身是可變的,因為您可以直接更新它的狀態 。 與String類似,它也在字符數組中保存值,您可以通過不同的方法操作該數組, 即追加,刪除,插入等直接更改字符值數組。
在Java中,所有字符串都是不可變的 (不能更改)。 當您嘗試修改String時,您真正在做的是創建一個新的 。
按照我們可以創建字符串對象的方式
使用String文字
String str="java";
使用新關鍵字
String str = new String("java");
使用字符數組
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' }; String helloString = new String(helloArray);
字符串不變性只是意味着不可修改或不可更改
我們舉一個例子
我正在初始化String文字的值
String s="kumar";
下面我將使用hashcode()顯示位置地址的十進制表示
System.out.println(s.hashCode());
只需打印String的值即可
System.out.println("value "+s);
好的,這次我將值“kumar”初始化為s1
String s1="kumar"; // what you think is this line, takes new location in the memory ???
好的,我們通過顯示我們創建的s1對象的哈希碼來檢查
System.out.println(s1.hashCode());
好的,讓我們檢查下面的代碼
String s2=new String("Kumar");
System.out.println(s2.hashCode()); // why this gives the different address ??
好的,最后檢查下面的代碼
String s3=new String("KUMAR");
System.out.println(s3.hashCode()); // again different address ???
是的,如果你看到Strings的'和's1'具有相同的哈希碼,因為's'和's1'保持的值與'kumar'相同
讓我們考慮字符串's2'和's3'這兩個字符串哈希碼在某種意義上看起來是不同的,它們都存儲在不同的位置,因為你看到它們的值是不同的。
因為s和s1哈希碼是相同的,因為這些值是相同的並存儲在相同的位置。
示例1:嘗試以下代碼並逐行分析
public class StringImmutable {
public static void main(String[] args) {
String s="java";
System.out.println(s.hashCode());
String s1="javA";
System.out.println(s1.hashCode());
String s2=new String("Java");
System.out.println(s2.hashCode());
String s3=new String("JAVA");
System.out.println(s3.hashCode());
}
}
示例2:嘗試下面的代碼並逐行分析
public class StringImmutable {
public static void main(String[] args) {
String s="java";
s.concat(" programming"); // s can not be changed "immutablity"
System.out.println("value of s "+s);
System.out.println(" hashcode of s "+s.hashCode());
String s1="java";
String s2=s.concat(" programming"); // s1 can not be changed "immutablity" rather creates object s2
System.out.println("value of s1 "+s1);
System.out.println(" hashcode of s1 "+s1.hashCode());
System.out.println("value of s2 "+s2);
System.out.println(" hashcode of s2 "+s2.hashCode());
}
}
好的,讓我們來看看mutable和immutable之間的區別。
可變(它改變)與不可變(它不能改變)
public class StringMutableANDimmutable {
public static void main(String[] args) {
// it demonstrates immutable concept
String s="java";
s.concat(" programming"); // s can not be changed (immutablity)
System.out.println("value of s == "+s);
System.out.println(" hashcode of s == "+s.hashCode()+"\n\n");
// it demonstrates mutable concept
StringBuffer s1= new StringBuffer("java");
s1.append(" programming"); // s can be changed (mutablity)
System.out.println("value of s1 == "+s1);
System.out.println(" hashcode of s1 == "+s1.hashCode());
}
}
還有什么問題嗎? 請寫上......
我用輸出注釋修改了威廉的代碼,以便更好理解
static void changeStr(String in) {
in = in+" changed";
System.out.println("fun:"+in); //value changed
}
static void changeStrBuf(StringBuffer in) {
in.append(" changed"); //value changed
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("value");
String str = "value";
changeStrBuf(sb);
changeStr(str);
System.out.println("StringBuffer: "+sb); //value changed
System.out.println("String: "+str); // value
}
在上面的代碼中,查看main()和changeStr()中str的值,即使你在changeStr()中更改str的值,它只影響該函數,但在main函數中,值不會改變,但它不是StringBuffer的情況..
在StringBuffer中,更改的值會受到全局影響..
因此String是不可變的,StringBuffer是可變的......
在Simple中,無論你改變為String對象,只會影響到該函數。轉到String Pool。 但沒有改變......
可變變量是其值可能在適當位置變化的變量,而在不可變變量中,變量值不會發生。 修改不可變變量將重建相同的變量。
Mutable意味着您將保存對變量的相同引用並更改其內容但不可變您不能更改內容但您將聲明新引用包含變量的舊值和舊值
Ex Immutable - > String
String x = "value0ne";// adresse one x += "valueTwo"; //an other adresse {adresse two}
String x = "value0ne";// adresse one x += "valueTwo"; //an other adresse {adresse two}
對堆內存更改String x = "value0ne";// adresse one x += "valueTwo"; //an other adresse {adresse two}
adresse。
Mutable - > StringBuffer - StringBuilder StringBuilder sb = new StringBuilder(); sb.append("valueOne"); // adresse One sb.append("valueTwo"); // adresse One
StringBuilder sb = new StringBuilder(); sb.append("valueOne"); // adresse One sb.append("valueTwo"); // adresse One
我仍然在同一個地方,我希望這個評論有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.