Java code
public class My { public static void main(String[] args){
String str1="a";
String[] str2={"a"};
changes(str1);
changes(str2);
System.out.println(str1);
System.out.println(str2[0]);
}
public static void changes(String a){
a=new String("hello world");
}
public static void changes(String[] a){
a[0]=new String("hello world");
}
}打印出来的是: a hello worldstr1为什么没有改变呢????
public class My { public static void main(String[] args){
String str1="a";
String[] str2={"a"};
changes(str1);
changes(str2);
System.out.println(str1);
System.out.println(str2[0]);
}
public static void changes(String a){
a=new String("hello world");
}
public static void changes(String[] a){
a[0]=new String("hello world");
}
}打印出来的是: a hello worldstr1为什么没有改变呢????
解决方案 »
- 如何给jframe中的components也加上和jframe一样的事件监听
- 请问下谁有javax.media包
- 已知E:\uplod目录下有一个.txt文件,怎么把它转化成Byte数组呢?(急)
- 进来看看,搞的我莫名其妙,不知道大家有没有遇到过
- HttpSessionBindingListener 和HttpSessionListener有什么区别??能不能介绍一本书??谢谢!!
- 急!!如何用udp协议传输大文件??谢谢
- 请教:java里正弦,开方之类的函数在那个包里?
- 呵呵我很菜,由System.out.println()想到的不知对不对!!!!
- 请问哪里有thinking in C++和thinking in java的电子书下载?
- java中int 强制转换成byte 的具体怎么转?
- 为什么不能更新access数据库
- 寻用dtree.js实现的无限树性菜单(有复选框勾选)
另外new的a对应的才是:"hello world"!1.
str1--->"a"
a-->"hello world"
2.
str2是数组,他的数组中的第一个值将会变!
String str1="a";
changes(str1);
public static void changes(String a){
a=new String("hello world");
}我给str1初始化后已经调用了changes()让str1改变它的值了.
为什么就不变呢??
String str1="a"; str1-->"a"(这个明白吧?不明白就没办法说下去了)changes(str1);
public static void changes(String a){
a=new String("hello world");
}
a先-->"a" 然后 a-->"hello world" 但此时 str1仍-->"a"
------------------------------------------------------------------
String[] str2={"a"};str2-->{"a"}changes(str1);
public static void changes(String a){
a=new String("hello world");
}
同上 a-->{"a"}(这个{"a"}还是上面那个,即此时a和str2同时-->{"a"})
然后a[0]=new String("hello world") 即{"a"}被修改成{"hello world"}
str2也-->{"hello world"}所以会有上面那种结果
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification. Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown. A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String. The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).
String[] str2 = { "a" }; changes(str1);
changes(str2); System.out.println(str1);
System.out.println(str2[0]); } //简单地说当参数是引用类型传进去后,引用的地址不会改变,但这地址里的内容可以改变
public static void changes(String a) {
a = new String("hello world");//此方法说明引用的地址不会改变
} public static void changes(String[] a) {
a[0] = new String("hello world");//此方法说明地址里的内容可以改变,a[0]引用是a的内容
}
}
//创建str1到"a"的引用,也就是"a"在内存中的地址
changes(str1);
//把str1的值拷贝给 a ,也就是a现在也是到"a"的引用
//a = new String("hello world");
//将 a 的值指向一个新的对象 "hello world"//所以str1的值是不会变的,依然是到"a"的引用,也就是"a"在内存中的地址 public static void changes(String a){
a = new String("hello world");
}
| str1 | ------> | "a" |
+--------+ +--> +-----------------+
|
-------------------------------------------
+--------+ |
| str1 | ---+
+--------+ +--------+ +-----------------+ +-----------------+
| str2 | ------> | str2[0] | ------> | "a" |
+--------+ +--> +-----------------+ +-----------------+
|
----------------------------------------------------------------------
+--------+ |
| str2 | ---+
+--------+
方法执行完后:str1 = "hello world"; +--------+ +-----------------+
| str1 | ------> | "a" |
+--------+ +-----------------+
----------------------------------------------------------------------
+--------+ +-----------------+
| str1 | ------> | "hello world" |
+--------+ +-----------------+
str2[0] = "hello world"; +--------+ +-----------------+ +-----------------+
| str2 | ------> | str2[0] | ------> | "hello world" |
+--------+ +--> +-----------------+ +-----------------+
|
----------------------------------------------------------------------
+--------+ |
| str2 | ---+
+--------+
退出方法后: +--------+ +-----------------+
| str1 | ------> | "a" |
+--------+ +-----------------+ +--------+ +-----------------+ +-----------------+
| str2 | ------> | str2[0] | ------> | "hello world" |
+--------+ +-----------------+ +-----------------+
Java code
public class My { public static void main(String[] args){
String str1="a";
String[] str2={"a"};
changes(str1); //str1为实参
changes(str2); //str2为实参
System.out.println(str1);
System.out.println(str2[0]);
}
public static void changes(String a){ //a为形参
a=new String("hello world");
}
public static void changes(String[] a){ //a为形参 a[0]=new String("hello world");
}
}
StringBuilder[] str2 = {new StringBuilder("a")}; changes(str1);
changes(str2); System.out.println(str1);
System.out.println(str2[0]); } public static void changes(StringBuilder a) {
a = new StringBuilder("hello world");
} public static void changes(StringBuilder[] a) {
a[0] = new StringBuilder("hello world");
}
}