-1
 class TestCourse {

    public static void main(String[] args) {
        Course course1 = new Course("Data Structures");
        Course course2 = new Course("Database Systems");

        course1.addStudent("Peter Jones");
        course1.addStudent("Kim Smith");
        String a1 = new String("Anne Kennedy");
        System.out.println(a1);
        course1.addStudent(a1);

        course2.addStudent("Peter Jones");
        course2.addStudent("Steve Smith");

        System.out.println("Number of students in course1: " + course1.getNumberOfStudents());
        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++)
            System.out.print(students[i] + ", ");

        System.out.println();
        System.out.println("Number of students in course2: " + course2.getNumberOfStudents());

        String a2 = new String("Anne Kennedy");
        // should be false
        System.out.println(a2 == a1);
        course1.dropStudent(a2);
        // String[] students2 = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++)

            System.out.print(students[i] + ", ");

    }
}

class Course {
    private String courseName;
    private String[] students = new String[100];
    private int numberOfStudents;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    public void addStudent(String student) {
        students[numberOfStudents] = student;
        numberOfStudents++;
    }

    public String[] getStudents() {
        return students;
    }

    public int getNumberOfStudents() {
        return numberOfStudents;
    }

    public String getCourseName() {
        return courseName;
    }

    public void dropStudent(String student) {
        for (int i = 0; i < numberOfStudents; i++) {
            if (students[i] == student) { // should refer to different memory
                                            // address but is true? how come?
                while (i < numberOfStudents) {
                    students[i] = students[i + 1];
                    i++;
                }
            }
        }
        numberOfStudents--;
    }
}

if (students[i] == student) to my knowledge students[i]should point to a different addresses in memory than student therefore should be false, however I am getting true and don't understand why it is true in this case? Could someone please help elaborate?

Anand Undavia
  • 3,493
  • 5
  • 19
  • 33
s.h
  • 1
  • You can store references to the same string in `student` and `students[i]`. That's the only way that would return true. – Andy Turner Mar 02 '17 at 13:09
  • Some of the strings are interned and therefore those will return `true` from an `==` comparison. – Lew Bloch Mar 02 '17 at 14:31
  • It's not interning the strings in this case - strings a1 and a2 are both created with `new String(...)` that results in a new String instance – david a. Mar 02 '17 at 14:38
  • do you mean String pool? – CSK Mar 02 '17 at 15:44
  • Simplify your test case to its minimal form that reproduces the problem. As you say, two `String` instances created with `new` should not be identical. I don't believe they are, and that your test is invalid. But if it were simpler, I'd be willing to try it out. – erickson Mar 02 '17 at 15:57
  • Not a duplicate. This question is asking why it appears a string pool is being used even though code allocates distinct instances with `new`. – erickson Mar 02 '17 at 16:33

1 Answers1

-1

The references stored in student and students[i] are not the same and the result of the comparison is false - the if block is not executed. What you see happens because the dropStudent() method decreases numberOfStudents every time it is called, regardless the result of that comparison.

david a.
  • 5,283
  • 22
  • 24