笔试题
1、写出输出
public class Jtest{
int m=1;
int i=3;
void Jtest(){
m=2;
i=4;
}
public static void main(String[] args){
Jtest app=new Jtest();
System.out.println(app.m+","+app.i);
}
}结果为:1,3;
2、 写出输出
public class Jtest{
int m=1;
int i=3;
Jtest(){
m=2;
i=4;
}
public static void main(String[] args){
Jtest app=new Jtest();
System.out.println(app.m+","+app.i);
}
}结果为:1,3;
传实参:
3、public class Test {
  public static void main(String[] args) {
int a=99;

oper(a);
System.out.print(a);
}
  static void oper(int b){
  
  b=b+100;
  }
}
结果为:99;4、 写出输出
public class Test {
  public static void main(String[] args) {
String a=new String("A");
String b=new String("B");
oper(a,b);
System.out.print(a+","+b);
}
  static void oper(String c,String d){
  c.concat("B");
  d=c;
  }
}结果为:A,B;
5、写出输出
public class Test {
  public static void main(String[] args) {
StringBuffer a=new StringBuffer ("A");
StringBuffer b=new StringBuffer ("B");
oper(a,b);
System.out.print(a+","+b);
}
  static void oper(StringBuffer c,StringBuffer d){
  c.append("B");
  d=c;
  }
}结果为:AB,B;6.

解决方案 »

  1.   

    1、写出输出 
    public class Jtest{ 
    int m=1; 
    int i=3; 
    void Jtest(){ 
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest(); 
    System.out.println(app.m+","+app.i); //没有改变成员变量的值,所以输出是1,3,因为void修饰了之后jtest就不是构造函数了

    } 结果为:1,3; 
    2、 写出输出 
    public class Jtest{ 
    int m=1; 
    int i=3; 
    Jtest(){ 
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest(); 
    System.out.println(app.m+","+app.i); 

    } 结果为:2,4;这个结果就是2,4,因为Jtest是构造函数 
    传实参: 
    3、public class Test { 
      public static void main(String[] args) { 
    int a=99; oper(a); 
    System.out.print(a); 

      static void oper(int b){ 
      
      b=b+100; 
      } 

    结果为:99; 型参b不会改变a的值
    4、 写出输出 
    public class Test { 
      public static void main(String[] args) { 
    String a=new String("A"); 
    String b=new String("B"); 
    oper(a,b); 
    System.out.print(a+","+b); 

      static void oper(String c,String d){ 
      c.concat("B"); 
      d=c; 
      } 

    A,B是常量不会改变。所以仍然是A,B
    public class Test { 
      public static void main(String[] args) { 
    StringBuffer a=new StringBuffer ("A"); 
    StringBuffer b=new StringBuffer ("B"); 
    oper(a,b); 
    System.out.print(a+","+b); 

      static void oper(StringBuffer c,StringBuffer d){ 
      c.append("B"); 
      d=c; 
      } 

    StringBuffer可以改变内容,当c指向a对象的时候,并且为a对象添加了字符‘B’,这样a引用的时候就可以到达AB,而d付给c,仅仅是形参的指代发生了变化,不会影响到实参b的。如果再加上d.append,实参a还是会变化
      

  2.   

    对于第1,2个问题,楼主要记住一点:识别构造函数的重要一点就是“构造函数没有返回值”第3个问题,b是类方法oper(int b)的参数,第4个问题,c,d是类方法oper(StringBuffer c,StringBuffer d)的参数,
    这两个问题都是形参的问题,注意,记住:“形参的改变不影响外部变量的值”。第5个问题有点特殊,上面3,4个问题的形参问题是针对类型为基本类型时候,当为引用类型时(类似指针)时,
    它所指向的为同一个对象,所以形参改变它也会改变。
      

  3.   

    第一题:public class Jtest{ 
    int m=1; 
    int i=3; 
    void Jtest(){ 
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest(); 
    app.Jtest();
    System.out.println(app.m+","+app.i); 


    你没有调用类Jtest中的Jtest方法,自然m和i的值不会变化,想让m=2,i=4就加上红色那句第二题:public class Jtest{ 
    int m=1; 
    int i=3; 
    Jtest(){  //去掉了第一题中的void,你这个Jtest()现在由普通方法变为构造函数了
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest();  //生成一个类的对象会自动调用该类的构造函数,此时改变m和i的值
    System.out.println(app.m+","+app.i); 

    } 第三题:public class Test { 
      public static void main(String[] args) { 
    int a=99; oper(a);   //值传递,传递的是a的副本,改变的也是a的副本,a本身不变
    System.out.print(a); 

      static void oper(int b){ 
      
      b=b+100; 
      } 
    } 第四题:public class Test { 
      public static void main(String[] args) { 
    String a=new String("A"); 
    String b=new String("B"); 
    oper(a,b);  //此处虽然是引用传递,但是java规定String的内容不能改变
    System.out.print(a+","+b); 

      static void oper(String c,String d){ 
      c.concat("B"); 
      d=c; 
      } 
    } 第五题:public class Test { 
      public static void main(String[] args) { 
    StringBuffer a=new StringBuffer ("A"); 
    StringBuffer b=new StringBuffer ("B"); 
    oper(a,b);  //传递引用,StringBuffer为可变字符串
    System.out.print(a+","+b); 

      static void oper(StringBuffer c,StringBuffer d){ 
      c.append("B"); 
      d=c;  //引用的指向变化不影响实参的变化
      } 

      

  4.   

    关于最后一题,为了便于理解,可以再加一句:public class Test { 
      public static void main(String[] args) { 
    StringBuffer a=new StringBuffer ("A"); 
    StringBuffer b=new StringBuffer ("B"); 
    oper(a,b); 
    System.out.print(a+","+b); 

      static void oper(StringBuffer c,StringBuffer d){ 
      c.append("B"); 
      d=c;  //d指向c指向的值即a
      d.append("C"); //这是在改变a的值了
      } 

    输出:ABC,B理解了吧?
      

  5.   

    其实区别就在于 “构造参数”与“非构造参数”之间的区别:构造参数一般的“造型”:public ClassName(){
        ...;
    }
      

  6.   

    java都是值传递,没有引用传递,java2核心技术基础篇,有一节专门讲这个内容,可以参考一下。
      

  7.   

    Core Java 
    第七版的八版都是在Chapter 4. Objects and Classes中.方法参数一节
    Method Parameters
    Let us review the computer science terms that describe how parameters can be passed to a method (or a function) in a programming language. The term call by value means that the method gets just the value that the caller provides. In contrast, call by reference means that the method gets the location of the variable that the caller provides. Thus, a method can modify the value stored in a variable that is passed by reference but not in one that is passed by value. These "call by . . . " terms are standard computer science terminology that describe the behavior of method parameters in various programming languages, not just Java. (In fact, there is also a call by name that is mainly of historical interest, being employed in the Algol programming language, one of the oldest high-level languages.)The Java programming language always uses call by value. That means that the method gets a copy of all parameter values. In particular, the method cannot modify the contents of any parameter variables that are passed to it.
      

  8.   

    have learned a lot, thanks very much
      

  9.   

    笔试题 
    1、写出输出 
    public class Jtest{ 
    int m=1; //默认修饰符
    int i=3; //默认修饰符
    void Jtest(){ //这个是一般的方法,不是构造函数,这个类的构造函数是默认的,默认的构造函数的公有的
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest(); 
    System.out.println(app.m+","+app.i); //默认的修饰是在同一个包中可见的,所在这里可以访问,访问的时候,并没有改变m,i的值

    } 结果为:1,3; 
    2、 写出输出 
    public class Jtest{ 
    int m=1; 
    int i=3; 
    Jtest(){ 
    m=2; 
    i=4; 

    public static void main(String[] args){ 
    Jtest app=new Jtest(); //new这个app对象的时候,就会给m,i进行初始化,也就是m,i的值改变了,这个构造器的修饰符是缺省的,也就是default类型
    System.out.println(app.m+","+app.i); //这里输出的是2,4

    } 结果为:1,3; 
    传实参: 
    3、public class Test { 
      public static void main(String[] args) { 
    int a=99; oper(a); 
    System.out.print(a); //这里的a也是一个局部变量,不回被b的值改变的

      static void oper(int b){ //这里的b是一个局部变量,是不会改变外面的值的
      
      b=b+100; 
      } 

    结果为:99; 4、 写出输出 
    public class Test { 
      public static void main(String[] args) { 
    String a=new String("A"); 
    String b=new String("B"); 
    oper(a,b); 
    System.out.print(a+","+b);// 和上面的一样,就是考虑变量的可见范围

      static void oper(String c,String d){ 
      c.concat("B"); 
      d=c; 
      } 
    } 结果为:A,B; 
    5、写出输出 
    public class Test { 
      public static void main(String[] args) { 
    StringBuffer a=new StringBuffer ("A"); 
    StringBuffer b=new StringBuffer ("B"); //StringBuffer是一个容器,也就是你把这个容器拿到别的地方装好了东西,再拿回来,它里面还是有东西的
    oper(a,b); //这里面的操作就可能会改变a,b这两个容器
    System.out.print(a+","+b); 

      static void oper(StringBuffer c,StringBuffer d){ 
      c.append("B"); //在原来的值尾部追加了一个字符串B,就是改变了这个引用对象指向的内存地址里的值
      d=c; //d是局部的对象,局部的引用变量,是不回改变原来引用变量的引用地址的,所以原来b中的内存地址所指向的值就不回改变
      } 
    } 结果为:AB,B; 
      

  10.   


    你看JAVA 编程思想附录A好了,我看的是第2版,英文的。具体页数忘记了,高中时候看的。
      

  11.   

    thinking in java 第1遍看不懂啊
    我看core java 是预习 ,看thinking in java 是复习  **!