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;
}
}
但结果却相同(10 , 10)
..
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;
}
}
不同,首次打印为0 ~ 5
,但第二次打印为1 , 1, 1....
我不知道有什么不同,
在java,Array(意思是喜欢int [] ..)使用地址??像C中的指针一样?
P.S:很抱歉缩进..上面的代码写在网上,
答案 0 :(得分:0)
回答第一段代码片段非常简单:
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
}
}
你可能会看到。两个变量都被称为&#34; var&#34;但只打印出一个。永远不会使用OBJ方法中的var变量。
现在是另一个案例:
public class test{
public static void main(String args[]){
double val[] = {0, 1, 2, 3, 4, 5}; //array is an object, variable var is a pointer to the location of that object in your heap
Obj obj = new Obj();
for(int i = 0; i < val.length; i++){
System.out.println(val[i]; // you print out every number located at the space in your heap your pointer is pointing at
}
obj.def(val);
for(int i = 0; i < val.length; i++){
System.out.println(val[i]; //prints out the overwritten values
}
}
}
public class OBJ{
public void def(double[] val){ //you give the method the pointer to the location of your numbers in heap
for(int i = 0; i < val.length; i++
val = 1; //you have the exact pointer to the exact objects in your heap therefore you overwrite them
}
}
现在到这一部分。你的var变量是指向堆中var []对象的指针(是的数组是对象)。你给你的def方法指向你的对象。因此,可以访问和覆盖对象。
简而言之。在您的第一个代码段中,您创建了两个互不连接的int变量。在你的第二个剪辑中你停止一个对象和一个指向它的变量。在这种情况下,可以通过指针访问对象。你也可以这样做:
int[] sameArray = var;
在这种情况下&#34; sameArray&#34;不会是另一个对象,而是指向var []对象的指针。
我为可怕的英语感到抱歉,我会尽快纠正它
答案 1 :(得分:0)
在java原语中,int
之类的值直接按值传递。对象,其数组属于aswell,也是通过值传递的,只是在这种情况下,引用作为值传递。这意味着您正在处理相同的数组实例。
正如您在示例中所见,传递int[]
并更改其中的值也会影响传递给它的原始int[]
。上面所写内容的真正含义是,引用作为值传递,即更改对象的引用不会反映原始值的更改。
这是一个很小的例子,评论证明了这一点。
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