• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper's String Constructor  RSS feed

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is not working and i fail to understand why!!!


I presume that java will invoke autoboxing
when i change the display method to accept one argument,it works.
please help me out or point me in the correct direction.

Thanks in advance

 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when i change the display method to accept one argument,it works.


Can you show us how you change it ?
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I this code when we invoke method as it will give compiler error.
But when we invoke it using it run fines. I also don't know why auto boxing is not work here. Anybody know?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing helps to turn primitives into wrappers. It doesn't work with Strings.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boxing is intended to map to-and-from primitive types but String
is not a primitive. Consider this. If an int were passed to a method
that accepts only a String, boxing does not convert the int to a String.
I will convert it to an Integer type, however.

Jim ... ...
 
Sheriff
Posts: 21291
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jim Hoglund wrote:I will convert it to an Integer type, however.


Auto-boxing and auto-unboxing only occurs if it is necessary. If you pass an int and there is no method that takes an int,or an Integer (or broader like long, Number) the compiler will just refuse to compile the class.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!