Java String

挺无聊的题目真的难倒不少人,咳

public class Hello {
    public static void main(String[] args) {
        String s = "123";
        System.out.println(s);
        m1(s);
        System.out.println(s);

        int i = 123;
        System.out.println(i);
        m2(i);
        System.out.println(i);
    }

    private static void m1(String s) {
        s = "321";
    }

    private static void m2(int i) {
        i = 321;
    }
}

运行结果是什么?

Thursday, April 6th, 2006 06:33pm

Comments

19 Comments | Add comment | TrackBack URI
Posted by 二月娱乐网 at April 7, 2006 1:17 am

学习


Posted by Yining at April 7, 2006 10:10 am

m1是不是应该为 s = “321″? ;)


Posted by zendragon at April 8, 2006 12:32 am

哦,是啊,我写错。改了


Posted by SunQ at April 16, 2006 7:43 pm

这个要不会java就白学了,成绩虽然不好,但还知道第一个是321,第二个是123的
感觉在搞mp3…哈哈哈。。mpg123/mpg321


Posted by zendragon at April 28, 2006 8:20 pm

SunQ, 这个可能是看学得够不够细致的问题。很遗憾,你的答案正是多数人的错误答案。都说了,挺无聊的问题。


Posted by faintbear at April 30, 2006 4:29 pm

都是123吧
值传递的问题。。


Posted by limon at July 25, 2006 11:39 pm

都是123, 所谓按应用传递其实也是传值,只不过传的是地址而已,m1里S的值,也就是其本身所指地址的改变和main里的S毫无关系。只有通过传递的地址来操作对象的成员才会起到所谓传引用的作用。
C#里有ref这样的参数来按引用的引用来传递参数,这样就能完成SunQ所说的效果了
最近在看JAVA,不知道JAVA里有没有类似ref的方法?


Posted by homonia at July 30, 2006 9:59 pm

123
321
123
123


Posted by penglonghua at December 3, 2006 8:58 pm

123
123
123
123


Posted by Eros at January 5, 2007 8:06 pm

123
123
123
123

in Java, all arguments are passed by value.

so do C.


Posted by das at February 6, 2007 7:13 pm

全部都是123


Posted by Steven at February 9, 2007 2:05 pm

123
123
123
123
如果m1(String s)改為…
private static void m1(String s) {
this.s = “321″;
}
則輸出為
123
321
123
123


Posted by Steven at February 9, 2007 2:06 pm

this.s = \”321\”;


Posted by Steven at February 9, 2007 2:07 pm

this.s = ‘321′;


Posted by Steven at February 9, 2007 2:07 pm

為什麼引號都不能正常顯示….


Posted by Steven at February 9, 2007 2:10 pm

剛剛測一下發現我說錯了
不能在static裡用this
Sorry….


Posted by Steven at February 9, 2007 2:17 pm

public class test {
  public static void main(String[] args) {
    mFunction mf = new mFunction();
    System.out.println(mf.s);
    mf.m1(mf.s);
    System.out.println(mf.s);

    System.out.println(mf.i);
    mf.m2(mf.i);
    System.out.println(mf.i);
  }
}
class mFunction {
  String s = “123″;
  int i = 123;
public void m1(String s) {
    this.s = “321″;
  }

  public void m2(int i) {
    i = 321;
  }
}


Posted by Hunt at April 28, 2007 1:18 am

123
123
123
123


Posted by Test at May 10, 2007 1:40 am

Hello

Bye


RSS feed for comments on this post.

Leave a comment

Or use your OpenID:

Google
 
Web erning.net