下面代码打印结果:
tom-20
tom-20
问题:
1.Student s = students[i]; 改变students[i]可以改变s是不是意味着是引用的数组元素地址
2.如果1成立,那为什么students[i]=null,不能是s也未null,从而在打印引起空指针异常
package com.zhuxl.jdk.sourcecode.java.util.hashmap;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[2];
students[0] = new Student("jack1", 20);
students[1] = new Student("jack2", 20);
for (int i = 0; i < students.length; i++) {
Student s = students[i];
if (s != null) {
students[i].setName("tom");
students[i] = null;
System.out.println(s.getName() + "-" + s.getAge());
}
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}