943,609 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 926
  • Java RSS
Dec 3rd, 2007
0

generics in java

Expand Post »
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!
Similar Threads
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Dec 3rd, 2007
0

Re: generics in java

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 3rd, 2007
0

Re: generics in java

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.
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Dec 3rd, 2007
0

Re: generics in java

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 3rd, 2007
0

Re: generics in java

well then I can pass it as an Integer and a Float
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Dec 3rd, 2007
0

Re: generics in java

Click to Expand / Collapse  Quote originally posted by paradox814 ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 4th, 2007
0

Re: generics in java

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> ?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with a method
Next Thread in Java Forum Timeline: no default server available while configuring eclipse for application server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC