Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass Byte and Integer to Generics?

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello , here i created simple generic ,i passed one integer and one byte, it will not take byte's value.When compile, it says
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor Gen1<Byte>(int) is undefined

please any one tell me how to pass byte and integer values to generics.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply and please tell why we create
New Byte("25")
,and it not accepting implictly.?
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:Thank you for your reply and please tell why we create
New Byte("25")
,and it not accepting implictly.?

Because all integer literals (unless they have an 'L' after them) are ints. The compiler can work out that for
new Gen1<Integer>(99);
it needs to autobox the 99, but
new Gen1<Byte>(25);
requires both a narrowing conversion AND boxing, and that it can't do.

And BTW, you don't need to use new Byte(25) (and certainly not new Byte("25")); use Byte.valueOf(25). Much better.

Winston
 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much ,my doubt was clear..
thank you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:thank you.

You're welcome.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic