还有个题目
public clsss Test{
public static void main(String[]args)
{
System.out.println("hello"+args[0]);
}
}执行命令 java Test java hello cao
为什么不能运行呢,还有结果为什么不是hellojava
public clsss Test{
public static void main(String[]args)
{
System.out.println("hello"+args[0]);
}
}执行命令 java Test java hello cao
为什么不能运行呢,还有结果为什么不是hellojava
解决方案 »
- java项目https怎么做
- 脏读 脏数据 是什么意思?
- 设计一个含有一个按钮的GUI界面,当鼠标移动打按钮上时,鼠标会自动消失,并出现在另外的地方,这样鼠标永远无法捕捉到按钮。
- netbeans-6.0m9-full-windows.exe安裝出錯,請解決
- 初始化问题
- 子对象中是否包含了父对象成员?
- 高手开过来
- 怎么使用jdb?在线等待!!!详细的给100分
- 急求JRun3.0的注册码!!!
- Java jbutton 点击一次 实现这个按钮往下移动 怎么才能实现????(新手没有分 请熟手帮忙谢谢)
- 如何生成按指定概率密度的随机对象序列?
- 各位高手紧急求以下程序的源代码,拜托了。救命呀!谁先做出正确程序给分
clsss改为classpublic static void main(String[]args)
{
System.out.println("hello"+args[0]);
}
}args[0] 为第一个字符串
结果为hellohello
* Created on 2004-12-13
*
*/public class Test {
public static String stringReplace(String text) {
text = text.replace('a', 'l');
return text;
} public static String bufferReplace(StringBuffer text) {
text = text.append("c");
return text.toString();
} public static void main(String args[]) {
String textString = new String("java");
StringBuffer sb = new StringBuffer("java");
textString = stringReplace(textString);
String s = bufferReplace(sb);
System.out.println("================");
System.out.println(textString);
System.out.println(s);
}
}
因为java中的String是个特殊的类,他是不允许改变的,当你String textString=new String("java");
就建立了一个字符串对象,调用stringReplace(textString); 方法时就把textString字符串所在的空间地址赋给了
public static void stringReplace(String text){
text=text.replace('j','l');
}
中的text引用,当他调用replace方法时,其实是返回一个新的字符串类return new String();也就意味着现在text存储的地址改变了,指向了改变后的字符串.
而StringBuffer类只是在原有类的基础上修改,所以修改后的字符串仍存储在原来的地址空间上.
所以你调用stringReplace()时,原来的字符串没有被修改所以仍是原来的植,而bufferReplace()则在原来的基础上进行修改所以输出修改过的.