我不知道';我不知道javainit方法

我不知道';我不知道javainit方法,java,Java,但是,结果是相同的 public class test{ public static void main(String args[]){ int val = 10; System.out.println(val); Obj obj = new Obj(); obj.def(val); System.out.println(val) } } public class OBJ{ public void

但是,结果是相同的

public class test{
    public static void main(String args[]){
       int val = 10;
       System.out.println(val);
       Obj obj = new Obj();
       obj.def(val);
       System.out.println(val)
    }
 }
 public class OBJ{
    public void def(int val){
       val = 1;
    }
 }
公共类测试{
公共静态void main(字符串参数[]){
双val[]={0,1,2,3,4,5};
Obj Obj=新Obj();
对于(int i=0;i
这是不同的,第一次打印是
0~5
,但是第二次打印是
1,1,1….
我不知道有什么不同

在java中,数组(它的意思是像int[])使用地址??像C中的指针


注:很抱歉缩进..上面的代码是在web中编写的,

Ansare非常简单地查看第一个代码片段:

public class test{
    public static void main(String args[]){
       double val[] = {0, 1, 2, 3, 4, 5};
       Obj obj = new Obj();
       for(int i = 0; i < val.length; i++){
          System.out.println(val[i];      
       }   
       obj.def(val);
       for(int i = 0; i < val.length; i++){
          System.out.println(val[i];      
       }

    }
 }
 public class OBJ{
    public void def(double[] val){
       for(int i = 0; i < val.length; i++){
          val[i] = 1;
       }
 }
正如您可能看到的。两个变量都称为“var”,但只打印出一个。OBJ方法中的var变量从未使用过

另一种情况是:

  public class test{
    public static void main(String args[]){
       int val = 10;                        //variable var in main method
       System.out.println(val);             // prints var of main method
       Obj obj = new Obj();
       obj.def(val);
       System.out.println(val)             // prints var of main method
  }
 }
  public class OBJ{
   public void def(int val){
      val = 1;                // variable val in OBJ method
   }
 }
在本例中,“samarray”不是另一个对象,而是指向var[]对象的指针


很抱歉我的英语很糟糕,我会尽快纠正它,在java原语中,像
int
是直接通过值传递的。数组所属的对象也是通过值传递的,只是在这种情况下引用是作为值传递的。这意味着您正在处理中数组的同一实例你的案子

正如您在示例中看到的,传递
int[]
并更改其中的值也会影响传递给它的原始
int[]
。上面所写内容的真正含义是,引用作为值传递,即更改对象的引用不会反映原始值的更改

下面是一个很小的例子,上面的注释说明了这一点

int[] sameArray = var;

当你测试一些代码时,复制和粘贴你的测试代码而不是试图在这里重写它通常是非常聪明的。如果你不是在一台可以复制代码的PC上,那就等到你在那里。因为Java是区分大小写的,我不得不问你,OBJ类与你的问题相关吗,你没有在测试类中使用它反之亦然?
int[] sameArray = var;
public class TestObj {
    private int val;

    public TestObj(int value) {
        this.val = value;
    }

    public static void main(String[] args) {
        int value = 1;
        int [] values = {1,2,3,4};
        TestObj obj = new TestObj(15);
        System.out.print("Print single int: ");
        print(value); 
        System.out.println("Print int array:");
        print(values);
        System.out.print("Print TestObj val: ");
        print(obj);
        System.out.print("Changing single int value: ");
        changeValue(value); //  no effect
        print(value);
        System.out.println("Changing array int values: ");
        changeValues(values); // effected
        print(values);
        System.out.println("Changing array value of reference: ");
        changeRefValues(values); // no effect
        print(values);
        //
        System.out.println("Changing val of TestObj");
        changeVal(obj); // effected
        print(obj);
        System.out.println("Changing TestObj value of reference");
        changeRef(obj); // not effected
        print(obj);
    }

    static void changeValue(int value){
        value *= 2; // Primitives are directly passed as value, so this wont effect the passed value. 
    }

    static void changeValues(int[] values){
        for(int i = 0;i<values.length;++i) {
            values[i] *= 2; //You are working on the value of the reference that is passed. The passed int[] is effected
        }
    }

    static void changeRefValues(int[] values){
        values = new int[]{0,0,0,0}; // you change the value of the reference that is passed. The passed int[] is not effected
    }

    static void changeVal(TestObj obj) {
        obj.val *= 2; // You are working on the value of the reference that is passed. The passed TestObj is effected
    }

    static void changeRef(TestObj obj) {
        obj = new TestObj(30); // You change the reference, but since it is passed as value it has no effect on the passed TestObj
    }

    // Only used to print values from here
    static void print(int[] values) {
        for (int i : values) {
            print(i);
        }
    }

    static void print(int i) {
        System.out.println(i);
    }

    static void print(TestObj obj) {
        System.out.println(obj.val);
    }

}
Print single int: 1
Print int array:
1
2
3
4
Print TestObj val: 15
Changing single int value: 1
Changing array int values: 
2
4
6
8
Changing array value of reference: 
2
4
6
8
Changing val of TestObj
30
Changing TestObj value of reference
30