generics in java

Reply

Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

generics in java

 
0
  #1
Dec 3rd, 2007
I am using generics in java, and I want to know how I can test to see if I have a int or a float? Is there some sort of type-testing? I'm sure there is, I just need someone to help point me in the right direction. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: generics in java

 
0
  #2
Dec 3rd, 2007
huh?
What are you trying to do?
If you have to worry about the exact type of a genericised argument to a method the decision to use generics at that stage was wrong.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: generics in java

 
0
  #3
Dec 3rd, 2007
well I have either an int or a float all the calculations are the same except for the constructor, where I need to call either java.util.Random nextInt or nextFloat
Last edited by paradox814; Dec 3rd, 2007 at 2:03 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,454
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: generics in java

 
0
  #4
Dec 3rd, 2007
The question still makes no sense. You can't generically pass a primitive as a parameter, so you would need separate signature for each anyway, which alleviates the ambiguity.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: generics in java

 
0
  #5
Dec 3rd, 2007
well then I can pass it as an Integer and a Float
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,454
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: generics in java

 
0
  #6
Dec 3rd, 2007
Originally Posted by paradox814 View Post
well then I can pass it as an Integer and a Float
Yes, and that is a different story. You repeatedly said you were working with int and float, which are not the same thing.
You can use the "instanceof" operator to check the type of an object variable.

(I think you also mean "abstraction" in your original question - not generics, based on the question. Generics are a type parameterization mechanism introduced in Java 1.5)
Last edited by Ezzaral; Dec 3rd, 2007 at 2:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: generics in java

 
0
  #7
Dec 4th, 2007
If your method implementation can work with both integer and floating point numbers, why handle them differently?
If it can't, why use generics to template away the difference in the method signature?

I assume you pass in an argument of type <E extends Number> ?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC