• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get Original Object

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a class
public class Node {
private Object val;
public Object getObj() {
return this.val;
}

//Below will be the other accesor methods
.........
}
now if I have a list of Node , all with diferent objects in them, lets say a new Float() object, new Integer() Object, and a new String() Object. How can I call toString on these Objects to return the original values instead of the hasg code values, cause that is what happens when i do a
x.getObj().toString()
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to cast it to String or Float or whatever. not sure if you can do it in one statement. this should work though.
private String y;
y = (String)x.getObj();
y.toString();

if you don't know the object type, there is a way to find out what it is but i forget how. someone else will know though.
[ January 19, 2004: Message edited by: Randall Twede ]
[ January 19, 2004: Message edited by: Randall Twede ]
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I call toString on these Objects to return the original values instead of the hasg code values
There is no magic in the toString() method, -- it will do exacltly what you will tell it to do. If you don't tell anything, the toString() method of the parent class will be invoked. If the parent class happens to be Object, you will get back the name of the class, plus @, plus the hexadecimal representation of the object hash code. To get something more meningful, simply override the toString() method of your object to return the textual representation of your object as you see fit.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eugene,
he doesnt want to do toString() on the Node object, rather on its member variable named val.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RT: he doesnt want to do toString() on the Node object, rather on its member variable named val.
Ok, but that variable is an object, not a primitive. Therefore the toString() method can be overrriden and called to return a custom-defined textual representation. For example:
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think what you are looking for is called Run-time Type Identification and is covered in chapter 12 of bruce eckel's free online book "Thinking in java" at least it was in the 2nd edition
if(x instanceof Float)
((Float)x).toString();
[ January 19, 2004: Message edited by: Randall Twede ]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iwont post the entire chapter, but maybe this will be helpful:
 
reply
    Bookmark Topic Watch Topic
  • New Topic