Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded println functions  RSS feed

 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering, we have many overloaded println functions in System.Out. Couldnt we have had just one println function which had an Object as argument?

Object class has toString() method anyways which automatically prints the String representation, and we could always use the instanceof to find out the underlying type(if required).


On second thought, it could've even been var-args obj. This would take care of 1 or many input parameters.
 
Stephan van Hulst
Saloon Keeper
Posts: 7797
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() is defined on the Object class, and since every reference type is a subtype of Object, it works without instanceof.

Since Java 5, it should also work for primitives, because primitives can be boxed to Objects. The remaining methods are just left-overs from before Java 5. They likely wouldn't have existed if Java code treat primitives as Objects from the very start.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!