aspose file tools*
The moose likes Java in General and the fly likes Just was typing a LinkedList code from 'Programming With Java' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Just was typing a LinkedList code from Watch "Just was typing a LinkedList code from New topic
Author

Just was typing a LinkedList code from 'Programming With Java'

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1056






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: 104

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: 1107

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: 1056

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: 6109
    
    6

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: 8418
    
  23

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?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Just was typing a LinkedList code from 'Programming With Java'