This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
hi all, heres a pretty basic question but it has been confusing me. I assumed when i saw the toString method i could pass it an integer and it would give me a string but i haven't been getting it right. i've been trying
can someone set me striaght with a simple example of how this method works. i thank you
String toString() This object (which is already a string!) is itself returned.
also from String API
Overrides: toString in class Object
from that I'm sure you can tell your own Objects can override the toString method in every class's daddy , Object, if you need a String representation of your own Object. now in order to get a String representing the int you speak of try this.
static String valueOf(int i) Returns the string representation of the int argument.
check the API theres a valueOf method for other primitives as well, and also one that takes an Object
hi billo Compile you code giving me error as following
1: not an expression statement at (a) variable new can not use because it is reserve word. 2: method toString(int) not found (a) 3: object type required.int is primitive type so you can not use it call toString method I hope that I can answer your question
First of all, you can't use "new" as a variable name since it is a keyword. Second of all, int is primitive type and not an Object, so you can't call toString() on a variable with type int. With that said, you can use the Integer class to get the String representation of the int variable. You should become intimately familiar with the API docs so you can look up such information in the future.