Results 1 to 8 of 8
- 01-08-2010, 07:12 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
- 01-08-2010, 07:18 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 01-08-2010, 07:21 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-08-2010, 07:24 AM #4
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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:)
- 01-08-2010, 09:51 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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
main: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; } }
why is the ouput false? :confused: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); } }
- 01-08-2010, 09:57 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Because you compared the Strings using == instead of using the equals method.
You didInstead ofJava Code:if (this.getTheWord() == string.getTheWord())
You also didn't override the equals method correctly.Java Code:if (this.getTheWord().equals(string.getTheWord()))
Read
Overriding the equals and hashCode methods - Java insights
- 01-08-2010, 12:21 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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:
but when i include the toString() methodJava Code:xxTestxx.Operations@3e25a5 xxTestxx.Operations@19821f xxTestxx.Operations@addbf1 xxTestxx.Operations@42e816
the output became fine:
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:Java Code:15 5 2 50
- 01-08-2010, 12:39 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Returning complex data types from a web method
By Tshegofatsom in forum Advanced JavaReplies: 6Last Post: 05-15-2009, 03:30 PM -
Calling Object.method
By Fedor in forum New To JavaReplies: 1Last Post: 04-11-2009, 01:44 PM -
Return an object to use in another method
By TidusSolan in forum New To JavaReplies: 3Last Post: 03-19-2009, 08:00 PM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 01:38 AM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks