如题
解决方案 »
- char与int转化的问题
- [求助]请高手们帮我看看,谢谢了~
- 关于环境变量的一些问题!
- 小程序监听不灵,求教!!!!!!
- 关于静态方法NEW对象的问题
- 通过jdbc如何连接远程数据库?(不使用odbc桥)
- 在线等待:如何把vector清空,再用该清空vector是,索引是从0开始,而不是紧接着上次清空是的索引?
- java.mail的小问题,请帮忙。谢谢
- ■■请问各位使用JAVA都做些什么项目,为什么用JAVA那么高的薪水,你们创造了那么高的经济效益吗?■■
- 我想横着打,但他总是竖着打印。我已经设置了pageformat.setOrientaion(PageFormat.LANDSCAPES) ,问什么不行?
- 100分问一个Eclipse3.0最菜的问题
- loadClass问题请教....
extends Object
implements Serializable, Comparable, CharSequence
String其实是个常量类型,他的任何一个对象都是不变的,它的所有的方法也是顶多返回一个新的串给你,而自身是永远不变的。
如果还想不明白,把你不明白的代码贴出来,让大家帮你解释解释。:)
class Student implements Cloneable
{
private String name ;
private String num;
public Student(String name , String num)
{
this.name = name;
this.num = num;
}
public Student modify(String name)
{
this.name =name;
return this;
}
public String toString()
{
return name + " "+num ;
}
}
public class StringTest
{
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
Integer inte= str;
inte = new Integer(100);
}
public static void test(Student stu)
{
stu = stu.modify("zeiku");
}
public static void main(String []args)
{
String str = new String("Hello");
test(str);
prt(""+str);
Integer inte = new Integer(1000);
test(inte);
prt(""+inte.intValue());
Student stu = new Student("zhudonhua","3030612060");
test(stu);
prt(""+stu);
}
public static void prt(String str)
{
System.out.println(str);
}
}
看看,这是为什么???
{
private String name ;
private String num;
public Student(String name , String num)
{
this.name = name;
this.num = num;
}
public Student modify(String name)
{
this.name =name;
return this;
}
public String toString()
{
return name + " "+num ;
}
}
public class StringTest
{
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
str = new Integer(100);
}
public static void test(Student stu)
{
stu = stu.modify("zeiku");
}
public static void main(String []args)
{
String str = new String("Hello");
test(str);
prt(""+str);
Integer inte = new Integer(1000);
test(inte);
prt(""+inte.intValue());
Student stu = new Student("zhudonhua","3030612060");
test(stu);
prt(""+stu);
}
public static void prt(String str)
{
System.out.println(str);
}
}我自己定义一个类,但是他打印出的结果?????
运行结果:
Hello
1000
zeiku 3030612060
为什么最后一行不是zhudonhua 3030612060??????
{
stu = stu.modify("zeiku");
}你在这里不是已经把name改了吗?
Hello
1000
为什么不变啊??
我在
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
str = new Integer(100);
}
也改了啊!!!
这句是改变了参数str引用的对象,这句执行完以后,str引用到一个新对String对象,而并非改变了str原来所引用的那个对象的内容。而又因为局部修改参数引用并不影外部的调用者,所以外面原来的那个变量名(引用)还是引用着它原来引用的那个String对象"Hello"。
str = new Integer(100);
一样的道理,里面修改了引用,不影响外面那个引用本来所引用的对象。如果楼主熟悉C语言的指针,理解这些东西应该很容易。
这就是为什么你可以通过:
str = str + "World";
来利用原来str所引用的对象再加“World”一起构造一个新对象并让str引用到这个新对象,却无法改变原来那个str所引用的对象本身。
但StringBuffer类可以,这个类有许多可以改变自己的函数。