aspose file tools*
The moose likes Beginning Java and the fly likes Did you see this ?StringTokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Did you see this ?StringTokenizer" Watch "Did you see this ?StringTokenizer" New topic
Author

Did you see this ?StringTokenizer

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
In StringTokenizer,

did you see this.

Public StringTokenizer it extends Object implements Enumeration

.hasMoreElements and .nextElement are methods that are from Enumeration
and hope the only implementor is StringTokenizer.

.hasMoreTokens and .nextToken are methods from StringTokenizer itself means they are NATIVE to StringTokenizer ok that correct.

In this case :

.nextElement() will return Object

.nextToken() will return String.

StringTokenizer always will take a string and return a String.Agreed !

Then why .nextElement() returns an Object. ?

There are situations, Where an enumeration of objects is returned and we iterate through that ! yes possible ! agreed !

At those places we use .nextElement() ! ok !

My Question is

Enumeration interface is implemented only by StringTokenizer.that will return only strings as the name implies

How come a StringTokenizer, return enumeration of objects

and implement .nextElement() in it, It looks odd ?

Isn't it ?

Please clarify me on this.


Discussion - the powerfull way to excellence!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
When the Enumeration interface and StringTokenizer class were first written (in Java 1.0), Java did not have generics and overridden methods were not allowed to declare a return type that was a subclass of the overridden method's return type.
Because Enumeration interface was used in more places then (Iterator did not exist), it's nextElement method was declared to return an Object to allow the most flexibility for implementing classes. This meant that the StringTokenizer.nextElement method also had to declare itself to return an Object even though it only ever returned Strings.
I guess for backwards compatibility reasons this has never been changed.


Joanne
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Joanne Neal:
When the Enumeration interface and StringTokenizer class were first written (in Java 1.0), Java did not have generics and overridden methods were not allowed to declare a return type that was a subclass of the overridden method's return type.
Because Enumeration interface was used in more places then (Iterator did not exist), it's nextElement method was declared to return an Object to allow the most flexibility for implementing classes. This meant that the StringTokenizer.nextElement method also had to declare itself to return an Object even though it only ever returned Strings.
I guess for backwards compatibility reasons this has never been changed.


That was Explanatory !


Agreed ! Thanks for the efforts !



Please clarify these interpretations

Please visit the

class Properties

we have a method propertyNames() returns Enumeration.

then we can say Enumeration.hasMoreElements() and .nextElement() to iterate

thru the

list of Enumeration "Objects" (because .nextElement returns Object.)

so, i could clearly say that

Enumeration returns Objects - that should be TRUE

But all Objects returned are of type StringObject and nothing else.




Am i right !

Can anyone specify me one example of where an object of
type otherthan StringObject is returned when we say a method return type is Enumeration.



If this is true !



Then i could strongly say Enumeration is always Enumeration of StringObjects and Nothing ELSE !

Shall I ?

some one please clarify us on this



[ August 08, 2008: Message edited by: ram kumar ]
[ August 08, 2008: Message edited by: ram kumar ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
Originally posted by ram kumar:
Can anyone specify me one example of where an object of
type otherthan StringObject is returned when we say a method return type is Enumeration.


There isn't one in the public interface of the standard API, but there may be some that are used internally and even if there aren't, if Enumeration.nextElement was declared to return a String it would mean that 3rd party code wouldn't be able to define classes that implement the Enumeration interface and return something other than String.
[ August 08, 2008: Message edited by: Joanne Neal ]
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Joanne Neal:


There isn't one in the public interface of the standard API, but there may be some that are used internally and even if there aren't, if Enumeration.nextElement was declared to return a String it would mean that 3rd party code wouldn't be able to define classes that implement the Enumeration interface and return something other than String.

[ August 08, 2008: Message edited by: Joanne Neal ]


Hi Joanne,

Shall we end up saying Enumeration returns only stringObject !
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by ram kumar:


Hi Joanne,

Shall we end up saying Enumeration returns only stringObject !


No. Say for example you have a Vector of Integers, then you use Enumeration to traverse the Vector:

If you assumed the Enumeration returned a String you would be wrong. Enumerations can be generated from any Collection which could contain any type of Object. So Enumeration#nextElement() returns the Object type (until you add Generics into the mix).

When specifically talking about the StringTokenizer and Properties, the Enumerations they produce will always return Strings. But Enumerations in general can be of any type.


Steve
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
Thanks Steve. I knew that Enumeration was bound to be used internally somewhere - I just couldn't think of an example off the top of my head.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Did you see this ?StringTokenizer