static void operate(String str) {
str += "def";
}
static void mapAdd(Map<String, String> map) {
map.put("aa", "aa");
}
public static void main(String[] args) {
String str1 = new String("abc");
String str2 = "abc";
operate(str1);
// str1指向了堆中new出的对象abc,像operate传递时,应该传递的是str1的引用
// 在方法中操作后,str1应该指向到操作后的新的对象,也就是abcdef
System.out.println(str1);
operate(str2);
// str2为栈中的对象,传递的就是abc数值,方法操作完后,新的值仅仅是方法中的局部变量
// 值未带出到main方法中来,所以str2还应该是abc
System.out.println(str2);
Map<String, String> map = new HashMap<String, String>();
map.put("bb", "bb");
// 这里没有疑问,mapAdd中传递的map为对象,传递的是引用,操作后map指向了操作后堆中的map值
// 操作后,main方法中map中有两队值。
mapAdd(map);
for (String str : map.keySet()) {
System.out.println(str);
}
}
------ Solution ------------------------------------- after -------
str1 when the object reference is passed to a method that can change the properties of this object , and can return the appropriate change , but the object reference string point is never becomes .
As you wrote back that no new object variable to change its value can be written like this :
class a (int i)
{
i = i +1
}
void main:
int b = 3
a A = new a ();
A.add (b); sys (b)
------ Solution ------------------------- -------------------
okay with heap or pool
value can not be modified because the String object
str + = "def";
This sentence will be executed after a new object on the heap , the value is abcdef, and then update the reference str , while str1 not changed
------ For reference only -------- -------------------------------
above this sentence, can be explained by the map passed back you, please explain the different transmission and map passed str1 , thank you.
------ For reference only -------------------------------------- -
static void operate(StringBuffer buffer) {
System.out.println(buffer==buffer1);
buffer.append("def");
System.out.println(buffer==buffer1);
}
static void operate(String str) {
System.out.println(str==str2);
str+="def";
System.out.println(str==str2);
}
static void mapAdd(Map<String,String> map) {
map.put("aa","aa");
}
static StringBuffer buffer1=new StringBuffer("abc");
static String str2="abc";
public static void main(String[] args) {
operate(buffer1);
// str1指向了堆中new出的对象abc,像operate传递时,应该传递的是str1的引用
// 在方法中操作后,str1应该指向到操作后的新的对象,也就是abcdef
System.out.println(buffer1);
operate(str2);
// str2为栈中的对象,传递的就是abc数值,方法操作完后,新的值仅仅是方法中的局部变量
// 值未带出到main方法中来,所以str2还应该是abc
System.out.println(str2);
Map<String,String> map=new HashMap<String,String>();
map.put("bb","bb");
// 这里没有疑问,mapAdd中传递的map为对象,传递的是引用,操作后map指向了操作后堆中的map值
// 操作后,main方法中map中有两队值。
mapAdd(map);
for (String str : map.keySet()) {
System.out.println(str);
}
}
run this you will understand
------ For reference only --------------------------- ------------
then str points to a new abcdef, a method after , abcdef GC can be recycled , in fact, can not be here, the most critical is the String object caused by changes in value . If it is out of the ordinary new object , then passing , in a position to modify the original object value , such a method , the modified values are still the original address is saved , it is passed by value , it can be understood , Daniel
- ----- For reference only ---------------------------------------
so str points to a new abcdef, a method after , abcdef GC can be recycled , in fact, the key here is the value of the String object is immutable caused . If it is out of the ordinary new object , then passing , in a position to modify the original object value , such a method , the modified values are still the original address is saved , it is passed by value , it can be understood , Daniel
This is the meaning
------ For reference only ------------------------------- --------
OK, thank you , pass it Results posted

没有评论:
发表评论