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

StringTokenizer question

Sumi Das
Greenhorn

Joined: Jun 07, 2008
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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.


"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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

a String IS an Object.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sumi Das
Greenhorn

Joined: Jun 07, 2008
Posts: 4
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

Joined: Aug 31, 2004
Posts: 11343

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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jun 07, 2008
Posts: 4
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer question