|
Joe Pluta wrote, in part: >Instead, you should have a generic toString and then went on and gave some useful caveats. The method "toString" is special. Basically, you should override "toString" for any object that needs and deserves a "standard" output format to a String. As a rule of thumb, you should do a "toString" if the following tests are met: 1. You want or need some sensibly formatted output of the object to appear in a System.out.println type statement (including the servlet equivalents). 2. You have a specific format that "everyone" wants as the String version of the object (which is a generalization of the first rule). For instance, if you had an object like CHexadecimalInteger, then you would almost certainly have a toString method that returned: "0x"+((new Integer(myint)).toHexString()); as the implementation of "toString" for CHexadecimalInteger where "toHexString()" is the possibly fictitious routine that takes an ordinary integer (the CHexadecimalInteger object's "myint" instance variable) and turns it into a hexadecimal output string. PS: There is a real function in Java somewhere that gives a hex string from an integer, but if it isn't this one, it is one like it. Another good example of "toString" is the output you get from an exception object. You'll see it in fragments like this: } /* end 'try' block */ catch (IOException e) { System.err.println ("I/O Exception : "+e.toString()); } Larry W. Loen - Senior Java and AS/400 Performance Analyst Dept HP4, Rochester MN +--- | This is the JAVA/400 Mailing List! | To submit a new message, send your mail to JAVA400-L@midrange.com. | To subscribe to this list send email to JAVA400-L-SUB@midrange.com. | To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com. | Questions should be directed to the list owner: joe@zappie.net +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.