Results 1 to 8 of 8
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default returning an object from a method

    im currently reading a book and i want to clarify this,,

    the value of an object is the reference , and when you return that object you are returning the value

    my question is, AFTER you return a value(object) ,is the object deallocated from the memory?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If by deallocated you mean garbage collected then probably not.
    For the object to be garbage collected, there must be no live references to that object anymore and even then the garbage collector has to decide that it wants to run for that object to be collected.
    If the caller assigns the return value to another variable then the object is still accessed from that reference and is not eligible for garbage collection.

    P.S It is best not to mix too many issues when studying. i.e don't try to learn about garbage collection before you have finished with learning about methods.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigj View Post
    im currently reading a book and i want to clarify this,,

    the value of an object is the reference , and when you return that object you are returning the value

    my question is, AFTER you return a value(object) ,is the object deallocated from the memory?
    Think of objects as television sets; your variables or parameters are the remote controls that 'point to' the television sets. You never have a television set, just the remote control. When you return an object from a method you return a remote control. When no control for a specific object exists anymore the television set (the object) is garbage collected but as long as at least one remote control exists the television set exists as well.

    kind regards,

    Jos

  4. #4
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    hmm tnx for the respone i think i should finish the chapter first, then ill post back to this topic as soon as possible for any furhter questions... tnx tnx,
    anyway i dont have any knowledge about garbage collections yet... tnx:)

  5. #5
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    im currently studying the objects, refference, reserved word this..
    and im trying to imitate the logic behind the equals method of the String class, i just want to ask the problem with my program here

    Java Code:
    public class EqualsString {
    
        private String word;
        
        public EqualsString(String word) {
    
            setTheWord(word);
        }
        
        public boolean equals(EqualsString string) {
    
            if (this.getTheWord() == string.getTheWord()) {
    
                return true;
            }
            else {
    
                return false;
            }
       }
    
        private void setTheWord(String word) {
    
            this.word = word;
        }
    
        public EqualsString getTheWord() {
    
            EqualsString str = new EqualsString(word);
    
            return str;
        }
    }
    main:
    Java Code:
        public static void main(String[] args) {
    
            EqualsString name1 = new EqualsString("Equals");
            EqualsString name2 = new EqualsString("Equals");
    
            if (name1.equals(name2)) {
    
                System.out.println(true);
            }
            else {
    
                System.out.println(false);
            }
        }
    why is the ouput false? :confused:

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Because you compared the Strings using == instead of using the equals method.

    You did
    Java Code:
    if (this.getTheWord() == string.getTheWord())
    Instead of
    Java Code:
    if (this.getTheWord().equals(string.getTheWord()))
    You also didn't override the equals method correctly.
    Read
    Overriding the equals and hashCode methods - Java insights

  7. #7
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    about this one...

    Java Code:
    public class Operations {
    
        private int number;
    
        public Operations(int num) {
    
            setTheNumber(num);
        }
    
        private void setTheNumber(int number) {
    
            this.number = number;
        }
    
        public int getNumber() {
    
            return number;
        }
        
        public Operations add(Operations op) {
            
            int num1,
                num2;
    
            num1 = this.getNumber(); // refers to the receiving objects data members
            num2 = op.getNumber();   // refers to the argument object
    
            Operations sum = new Operations(num1 + num2);
    
            return sum;
        }
    
        public Operations subtract(Operations op) {
    
            int num1,
                num2;
    
            num1 = this.getNumber();
            num2 = op.getNumber();
    
            Operations difference = new Operations(num1 - num2);
    
            return difference;
        }
        
        // when i add this method
        public String toString() {
    
            return getNumber() + "" ;
        }
        
        public Operations divide(Operations op) {
    
            int num1,
                num2;
    
            num1 = this.getNumber();
            num2 = op.getNumber();
    
            if (num1 == 0 || num2 == 0) {
    
                System.err.print("Cannot Divide By Zero");
                System.exit(1);
            }
    
            Operations quotient = new Operations(num1 / num2);
    
            return quotient;
        }
    
        public Operations multiply(Operations op) {
    
            int num1,
                num2;
    
            num1 = this.getNumber();
            num2 = op.getNumber();
            
            Operations product = new Operations(num1 * num2);
    
            return product;
        }
    
        // Main
        public static void main(String[] args) {
    
            Operations num1,
                       num2;
    
            Operations sum,
                       diff,
                       quot,
                       prod;
    
            num1 = new Operations(10);
            num2 = new Operations(5);
    
            sum = num1.add(num2);
            diff = num1.subtract(num2);
            quot = num1.divide(num2);
            prod = num1.multiply(num2);
    
            System.out.println(sum);
            System.out.println(diff);
            System.out.println(quot);
            System.out.println(prod);
        }
    }

    when im not yet Overriding the toString() method the output is
    this:
    Java Code:
    xxTestxx.Operations@3e25a5
    xxTestxx.Operations@19821f
    xxTestxx.Operations@addbf1
    xxTestxx.Operations@42e816
    but when i include the toString() method
    the output became fine:
    Java Code:
    15
    5
    2
    50
    but i only added the toString() method... i didnt use this method in any of my predefined methods? how does it affects the correct output? :confused:

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    When you pass a variable of reference type to println, it automatically calls toString on the object to get the string to print out.

Similar Threads

  1. Returning complex data types from a web method
    By Tshegofatsom in forum Advanced Java
    Replies: 6
    Last Post: 05-15-2009, 03:30 PM
  2. Calling Object.method
    By Fedor in forum New To Java
    Replies: 1
    Last Post: 04-11-2009, 01:44 PM
  3. Return an object to use in another method
    By TidusSolan in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 08:00 PM
  4. Replies: 3
    Last Post: 12-10-2008, 01:38 AM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 09:44 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
  •