| Author |
Just was typing a LinkedList code from 'Programming With Java'
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 970
|
|
Output I got:
The first element in the List: Car@addbf1
The last element in the List: Bird@42e816
Output in Book:
The first element in the List: Benz
The last element in the List: raven
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Pavan Kumar Dittakavi
Ranch Hand
Joined: Feb 12, 2011
Posts: 95
|
|
I think, System.out.println ultimately calls the toString() method to display the contents. And since your code does not have the toString() method overridden, the one from the Object class gets picked up. That one, has the below implementation:
and hence the result. Perhaps you should override it?
Thanks,
Pavan.
|
 |
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 1098
|
|
is this really all the code from the example?
as the car and bird class do nothing but create a load of strings. There is no way the code you have pasted can produce the output expected.
is it an exercise to make it work?
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 970
|
|
Wendy Gibbons wrote:is this really all the code from the example?
as the car and bird class do nothing but create a load of strings. There is no way the code you have pasted can produce the output expected.
is it an exercise to make it work?
Yes its on page 350 Books name is Programming with Java(A Primer 3e) by E.BalaguruSamy
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5807
|
|
Vishal Hegde wrote:
Wendy Gibbons wrote:is this really all the code from the example?
as the car and bird class do nothing but create a load of strings. There is no way the code you have pasted can produce the output expected.
is it an exercise to make it work?
Yes its on page 350 Books name is Programming with Java(A Primer 3e) by E.BalaguruSamy
Well, if it is an exercise to make it work then I would expect that just typing it in does not give the desired output. The whole point in that case is that you are supposed to figure out what's missing. Presumably the topics covered in that section will give you a big hint.
If, on the other hand, that's not the case, that is, if you're supposed to be able to just type it and run it an get the same results, then either the book has an error or you made a mistake.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4734
|
|
Pavan Kumar Dittakavi wrote:I think, System.out.println ultimately calls the toString() method...
<Friday pedant>
You're not quite right. It actually uses String.valueOf(Object) (as do most 'print' methods), which will, indeed, call the object's toString() method as long as it is not null.
</Friday pedant>
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
 |
|
|
subject: Just was typing a LinkedList code from 'Programming With Java'
|
|
|