简体   繁体   中英

I Have a problem with understanding some Java code

The Code:

package com.keyoti.rapidSpell;

import java.util.Comparator;

// Referenced classes of package com.keyoti.rapidSpell:
//            RapidSpellChecker

class RapidSpellChecker$CompareL
    implements Comparator
{

    public int compare(Object a, Object b)
    {
        return (int)(100D * (suggestionScore2b(topWord, (String)b) - suggestionScore2b(topWord, (String)a)));
    }

    public void with(String w)
    {
        topWord = w;
    }

    private String topWord;

    RapidSpellChecker$CompareL()
    {
    }
}

This is the one the many classes in the application.

What does the $ sign in class RapidSpellChecker$CompareL implements Comparator signify?Is it simply the class name or has some significance?

I suspect this is decompiled code. (See at the bottom for more information.) The $ shows that it's a nested class within RapidSpellChecker . So the code would originally have looked something like this:

public class RapidSpellChecker
{
    // Other code withing RapidSpellChecker

    static class CompareL implements Comparator
    {
        // Code for compare, with etc
    }
}

I've shown this as a static nested class, because the code you've shown doesn't have any implicit reference to an instance of RapidSpellChecker . If it did, the original code would have been like this:

public class RapidSpellChecker
{
    // Other code withing RapidSpellChecker

    class CompareL implements Comparator
    {
        // Code for compare, with etc
    }
}

In this case it's an inner class .

See the Java tutorial on nested classes for more information.


EDIT: I originally thought this was invalid code; that you couldn't use $ in an identifier in Java to start with. It turns out I'm wrong. From the Java Language Specification, section 3.8 :

The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

So it's valid, just discouraged.

That's a nested class. When the Java compiler compiles a class with nested classes, it separates all of them in different.class files.

class A {
  class B {
  }
}

gives A.class and A$B.class

You can use $ in a variable name if you want. In a variable name it has no special significance.

$ is also typically used to indicate inner classes when you compile using javac

If you compile

class A {
    class B {
    }
}

You'll see A.class created and B.class.

For fun and amusement, you could create confusing looking "JQuery"-esque code in Java (you need the static import to use the $ static method). See the example below:

import static thisPackage.*;

public class $ {
    public static $ $(String s) { return new $(s); }
    public $ fadeIn(int fade) { return this; }
    public $ slideUp(int slide) { return this; }
    public $ delay(int ms) { return this; }
    public $(String s) { }

    public static void main(String[] args) {
       $("#foo").slideUp(300).delay(800).fadeIn(400);
    }
} 

Implementing this with a DOM library underneath would be a fun project!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM