$30 off During Our Annual Pro Sale. View Details »

CSE110 Lecture 24

CSE110 Lecture 24

Principles of Programming with Java
References
(202006)

Javier Gonzalez-Sanchez
PRO

June 22, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110
    Principles of Programming
    with Java
    Lecture 24:
    References
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    Office Hours: By appointment

    View Slide

  2. References

    View Slide

  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3
    Assignment | Primitive Types
    • The act of assignment takes a copy of a value and
    stores it in a variable
    • For primitive types:

    View Slide

  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4
    References
    • An object reference variable holds the memory
    address of an object
    • Rather than dealing with arbitrary addresses, we
    often depict a reference graphically as a “pointer”
    to an object
    • For object references, assignment copies the
    memory location:
    jane john jane john

    View Slide

  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5
    Assignment | References

    View Slide

  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6
    Arrays

    View Slide

  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7
    Assignment | References

    View Slide

  8. Arrays of Objects

    View Slide

  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9
    Arrays of Objects
    • The elements of an array can be object references.
    • Suppose that we have the Student class
    Student[] list = new Student[5];
    //The following causes NullPointerException
    int id = list[0].getID();
    //We need to instantiate each object first:
    list[0] = new Student(“Bob”);
    list[1] = new Student(“Mary”);
    list[2] = new Student(“Peter”);
    // ...
    // OR we can use the Initializer:
    Student[] list = {new Student(“Bob”),
    new Student(“Mary”),
    new Student(“Peter”) };

    View Slide

  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10
    Arrays of Objects

    View Slide

  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11
    Arrays of Objects

    View Slide

  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12
    Reference
    Chapter 6

    View Slide

  13. CSE110 - Principles of Programming
    Javier Gonzalez-Sanchez
    [email protected]
    Summer 2020
    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

    View Slide