Results 1 to 12 of 12
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default object and reference

    Hello,

    While solving some exercises, i stumbled upon 2 methods of different classes that return an object of type Item, but in the description one of them said "return the Item object" and the other one was "return a reference to the Item object" whats the difference between the two?

    Thanks in advance

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    In Java, they're the same thing; it's expanding into things like C++ where references and pointers and objects are all different. Can you show the methods?

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    the questions were above in my post and the methods i wrote are here, its an exercise without correction to it, and i wanted to know

    this method is when asking for return an Item object:
    Java Code:
    public Item get (int index)
    {
        return itemList.get(index);
    }
    and this is for returning an Item reference:
    Java Code:
    public Item getItem ()
    {
        return this.item;
    }

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Both mean the same thing

  5. #5
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    so the methods i wrote are correct, right?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    In Java, only references and primitives are passed and returned. Objects are not.

    db

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by Darryl.Burke View Post
    In Java, only references and primitives are passed and returned. Objects are not.

    db
    Ohh, so whenever they write like return an object it always returns a reference to this object ... ?

  8. #8
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Quote Originally Posted by aizen92 View Post
    so the methods i wrote are correct, right?
    Don't know business wise, but technically they are correct.

  9. #9
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Quote Originally Posted by aizen92 View Post
    Ohh, so whenever they write like return an object it always returns a reference to this object ... ?
    Yes, that's right.

  10. #10
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ahaa, I see
    well thnx guys for the help
    I appreciate it

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    These two pages on another forum's website explain it well.
    JavaRanch Campfire - Cup Size: a Story About Variables
    JavaRanch Campfire - Pass By Value, Please

    db

  12. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

Similar Threads

  1. Method, returning reference to an object
    By Saletra in forum New To Java
    Replies: 3
    Last Post: 08-23-2010, 08:22 PM
  2. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 03:45 PM
  3. Very new to Java, how to reference an object
    By tornado in forum New To Java
    Replies: 3
    Last Post: 12-05-2008, 12:51 AM
  4. Object reference to number
    By dayneo in forum New To Java
    Replies: 4
    Last Post: 05-27-2008, 01:23 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •