my dog learned polymorphism*
The moose likes Beginning Java and the fly likes toString question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "toString question" Watch "toString question" New topic
Author

toString question

sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
going thr JUST JAVA book
public class Fruit{
int grams;
int cals_per_gram;
public static void main (String[]args{
Fruit f= new Fruit();
System.out.println("f="+f.toString());
}
}
when run this code i got this f= Fruit@73d6a5
can anyone explain to me why @73da5 at the end.
thanks in advance
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Take a look at the API for class Object. The Object.toString() method is inherited by your class Fruit. You should override this method and return a more descriptive string representation of your class. Oh yes, the number is the base 16 representation of your object's hashcode.
-Barry
[ December 19, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
To get the output you were expecting you should overload your toString method. Add code to your Fruit class that looks like this:


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"sun flower"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString question
 
Similar Threads
class design problem
Unable to understand this behavior...wht is happening??
static context ><
Two questions
Constructor not found error