Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer question

 
Sumi Das
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a String IS an Object.
 
Sumi Das
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 22114
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Yes. It is. The purpose of the method is to satisfy the method required by the Enumeration interface. It is not bringing any new functionality to the class. The JavaDoc even states this.

In terms of implementation, one of the methods probably calls the other.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sumi Das
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic