aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
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: 45

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: 187
Slaxmi Raj
Ranch Hand

Joined: Apr 20, 2012
Posts: 45
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: 8396
    
  23

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: 45
Thank you so much ,my doubt was clear..
thank you.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8396
    
  23

Slaxmi Raj wrote:thank you.

You're welcome.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass Byte and Integer to Generics?