This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Print statement prints incorrectly but right?  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I just started programming and seem to have a weird problem in one of my projects.

So I am trying to swap variables num1 with num2. I created a tempVal variable to hold num1's value. When I print num1 and num2 in the beginning and end using it seems to print Number@4f4a7090, Number@52d455b8. Why is that? Is there something I am missing? It should print the values of num1 and num2, here is my code from both the SwapDemo class and Number class



 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main reason is, that you did not override toString() method, which supposed to be an overriden readable representation of an object's state. Another way to get the values num1 and num2 hold, are to call getNum() method on them within the print statement.

Now to answer your main question what is actually printed in your case. Well, since you did not override toString() method, when you pass an object to print statement, it uses toString() method's version inherited from Object class. Please read more here (<-- link to click on), what is actually gets printed in such case.
 
Steven Villarreal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense! So basically if I want to print an object from a different class I have to type the object name, let's say num1, then .getNum() which will go to the getNum() method and grab the number associated to num1?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steven Villarreal wrote:That makes sense! So basically if I want to print an object from a different class I have to type the object name, let's say num1, then .getNum() which will go to the getNum() method and grab the number associated to num1?


Yes, since in your Number class there is such method you defined yourself.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:The main reason is, that you did not override toString() method, which supposed to be an overriden readable representation of an object's state.


I wasn't clear enough on that one, sorry about that.

So in your Number class, you'd need to have method:

Once you have that, when you pass your object reference(s) in your case num1 and num2 to print method, it automatically invokes toString() method, just this time would be invoked your overriden version. Give a try.
 
Steven Villarreal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey that works too!
 
I can't take it! You are too smart for me! Here is the tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!