The SE6 javadocs for the StringTokenizer class state that the declared return value for method nextElement( ) is Object. However, by testing with the instanceof operator I have discovered that it actually returns a String.
Is this an error in Sun's javadocs ? Or is there a valid reason for this ?
The API documentation also explains that nextElement "Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface."
The return value is a String reference upcast to type Object.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Jun 07, 2008
First, I know a String is an Object. While all Strings are Objects. All Objects are not Strings.
Second, I don't see how the return value could be a String upcast to an Object when a simple conditional test with the instanceof operator demonstrates that the returned value is actually String. The point of my question is that there seems to be a discrepency between what the javadocs say the return value is and what it ACTUALLY returns. If one takes the time to invoke the method it can be demonstrated that it actually returns a String value. Thus, there seems to be an error in the javadocs.
The StringTokenizer class already has a nextToken() method that returns a String value. Why would the method nextElement() also return a String value. This is redundant.
Originally posted by Sumi Das: ...I don't see how the return value could be a String upcast to an Object when a simple conditional test with the instanceof operator demonstrates that the returned value is actually String...
Remember, objects have types (e.g., String), and references to those objects also have types (e.g., Object). A reference type can match the object's true runtime type, or it can be any supertype of that object (that is, an "upcast").
The instanceof operator returns true if "the reference could be cast (�15.16) to the ReferenceType without raising a ClassCastException." In this example, we have a reference upcast to type Object, but the true runtime type of the object is String. Because the object is really a String, the reference (of type Object) can be downcast to type String without raising a ClassCastException. This is exactly what the instanceof operator is telling us.
Originally posted by Sumi Das: ... Why would the method nextElement() also return a String value...
Again, there is a difference between the object type and the reference type.
nextElement returns a reference of type Object (not String), although that reference points to a String object. This is because StringTokenizer implements Enumeration<Object>, so nextElement must return a reference of type Object (even though in this case, that reference is to a String object).
Originally posted by Henry Wong: ... In terms of implementation, one of the methods probably calls the other...
Indeed, that's true. The nextElement method is simply...
Joined: Jun 07, 2008
Many thanks to all for setting me straight on this issue. I never considered the difference between the type of the object and the type of the reference. Also, thanks for the link to the Java Language Specs. I see that it can be very useful in trying to understand the lanaguage.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com