Xiaotong Sun
Xiaotong Sun

Reputation: 11

StringBuilder and CharSequence incompatible

I'm a beginner in Java language and now following the Oracle Java tutorial to gain more insight of this language. When I'm studying the Interface part, I got confused when I tried to do the exercise:

"Write a class that implements the CharSequence interface found in the java.lang package. Your implementation should return the string backwards. Select one of the sentences from this book to use as the data. Write a small main method to test your class; make sure to call all four methods."

When I'm doing this, I found something strange. At first, I download the java.lang.CharSequence interface source code and add it as an interface file in my home directory, and my implementation of the 'subSequence' complains that the StringBuilder cannot be casted to CharSequence type.

Here is the code:

public CharSequence subSequence(int start, int end) {
    if (start < 0) {
        throw new StringIndexOutOfBoundsException(start);
    }
    if (end > s.length()) {
        throw new StringIndexOutOfBoundsException(end);
    }
    if (start > end) {
        throw new StringIndexOutOfBoundsException(start - end);
    }
    StringBuilder sub = 
        new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));
    return sub.reverse();
}

But when I delete my local file of CharSequence interface, the java compiler finds out that I'm implementing the 'java.lang.CharSequence' interface instead of a local package, then the compiling error disappears.

Just curious about this. What's the difference between the local interface file and the library interface file? And what causes the compiling error in the beginning?

Upvotes: 0

Views: 780

Answers (2)

RealSkeptic
RealSkeptic

Reputation: 34628

In theory, you can create your own class or interface, which have an identical name to one that exists in the JRE already.

You can even create it in a package called java.lang.

But when Java wants to check type compatibility, it checks several things:

  1. The name is the same
  2. The package is the same
  3. The class loader is the same

If you actually set CharSequence's package to java.lang, then at runtime, when it checks the StringBuilder object, it looks for a java.lang.CharSequence from the class loader that loaded java.lang.StringBuilder itself. Your java.lang.CharSequence is not loaded by the same class loader, therefore it's not the same type.

However, if your error is at the compiler level, then it probably means that you have placed the CharSequence in a package with a different name (or in the default package). Otherwise, it would probably have been silent at compile time.

EDIT: In fact, it seems that at runtime the "local" java.lang.CharSequence is not even loaded, whereas the compiler can see it.

Upvotes: 1

SLaks
SLaks

Reputation: 887443

There is no such thing as a "local interface file".

You made a completely new, unrelated interface that happens to have the same name as an interface in the JRE.

You use types defined in the JRE directly from their packages, without downloading anything.

Upvotes: 3

Related Questions