• Post Reply Bookmark Topic Watch Topic
  • New Topic

autoboxing and type conversion  RSS feed

 
sathya reddy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code when int is passed to the constructor it is autoboxed internally by calling the
But when a string format is passed, it throws a compile time error like this one
I guess this should not happen cause there are overloaded version of in Integer wrapper class.can anybody explain whats happening here internally?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The met() method doesn't take a String as its parameter. Remember that Java® is a typesafe language; if you say that method takes an Integer as its parameter, then you have to pass it an Integer. You are not calling Integer#valueOf anywhere, but trying to pass a String directly. And there is no way for the javac tool to tell whether that String would even match the format of an int/Integer.
By the way: I hope you never use method names like met() in real life; that name is very uninformative.
 
sathya reddy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate....ill keep in mind to use more informative names next time.....
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!