| Author |
Changing Number, String to more general type Object
|
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
changed to
because i have a common 1 method, which update's these Set's. it is basically iterator. part of code shown below.
here i can specify only 1 type as Object.
Any idea's ?
|
SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
Ideas for what? Do you have a question?
|
[Jess in Action][AskingGoodQuestions]
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
|
My real question is, would it be fine to represent either String or Number as 1 common type as Object as shown in my changed code.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Amandeep Singh wrote:My real question is, would it be fine to represent either String or Number as 1 common type as Object as shown in my changed code.
You will be better off referring to the Sets using their proper types. In the method that needs the Iterator, pass them in as Set<? extends Object>:
So you can maintain type safety elsewhere.
|
Steve
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
|
that is really great. thank you.
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
i would prefer to use the enhanced for loop.
now my question is, how to ensure the null check before using the enhanced for loop?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Do you mean this:
Note that I removed "extends Object". This is because "?" and "? extends Object" are basically the same since Object is the root of all class hierarchies. Similarly, if you ever see "? super Object" you can replace it with "Object".
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
to check for null, traditional method. thanks
this is informative.
Note that I removed "extends Object". This is because "?" and "? extends Object" are basically the same since Object is the root of all class hierarchies. Similarly, if you ever see "? super Object" you can replace it with "Object".
you mean i should use objects only or ? .
or
|
 |
Amandeep Singh
Ranch Hand
Joined: Jul 17, 2008
Posts: 837
|
|
|
ok i tried the code. the first option works.
|
 |
 |
|
|
subject: Changing Number, String to more general type Object
|
|
|