GeeCON Prague 2014*
The moose likes Java in General and the fly likes Changing Number, String to more general type Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Changing Number, String to more general type Object" Watch "Changing Number, String to more general type Object" New topic
Author

Changing Number, String to more general type Object

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844

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: 24187
    
  34

Ideas for what? Do you have a question?


[Jess in Action][AskingGoodQuestions]
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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: 4181
    
  21

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: 844
that is really great. thank you.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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: 19697
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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: 844
ok i tried the code. the first option works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing Number, String to more general type Object