my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How to pass Byte and Integer to Generics? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to pass Byte and Integer to Generics?" Watch "How to pass Byte and Integer to Generics?" New topic
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: 7536
    
  18

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: 7536
    
  18

Slaxmi Raj wrote:thank you.

You're welcome.

Winston
 
Don't get me started about those stupid light bulbs.
 
subject: How to pass Byte and Integer to Generics?
 
Similar Threads
Generics
Generics
Generics
Examlab generics question
Generics