• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Did you see this ?StringTokenizer

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic