| Author |
How to pass Byte and Integer to Generics?
|
Slaxmi Raj
Ranch Hand
Joined: Apr 20, 2012
Posts: 40
|
|
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
Joined: Jun 02, 2012
Posts: 186
|
|
|
|
 |
Slaxmi Raj
Ranch Hand
Joined: Apr 20, 2012
Posts: 40
|
|
Thank you for your reply and please tell why we create
New Byte("25")
,and it not accepting implictly.?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4904
|
|
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
Joined: Apr 20, 2012
Posts: 40
|
|
Thank you so much ,my doubt was clear..
thank you.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4904
|
|
Slaxmi Raj wrote:thank you.
You're welcome.
Winston
|
 |
 |
|
|
subject: How to pass Byte and Integer to Generics?
|
|
|