Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





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

 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic