Glorified classes in the Java language

前端 未结 13 1806
一生所求
一生所求 2021-01-29 20:32

Some classes in the standard Java API are treated slightly different from other classes. I\'m talking about those classes that couldn\'t be implemented without special support f

相关标签:
13条回答
  • 2021-01-29 20:55

    Class, of course. It has its own literals (a distinction it shares with String, BTW) and is the starting point of all that reflection magic.

    0 讨论(0)
  • 2021-01-29 20:56

    Not sure about this. But I cannot think of a way to manually implement IO objects.

    0 讨论(0)
  • 2021-01-29 20:58

    There are a lot of different answers, so I thought it would be useful to collect them all (and add some):

    Classes

    • AutoBoxing classes - the compiler only allows for specific classes
    • Class - has its own literals (int.class for instance). I would also add its generic typing without creating new instances.
    • String - with it's overloaded +-operator and the support of literals
    • Enum - the only class that can be used in a switch statement (soon a privilege to be given to String as well). It does other things as well (automatic static method creation, serialization handling, etc.), but those could theoretically be accomplished with code - it is just a lot of boilerplate, and some of the constraints could not be enforced in subclasses (e.g. the special subclassing rules) but what you could never accomplish without the priviledged status of an enum is include it in a switch statement.
    • Object - the root of all objects (and I would add its clone and finalize methods are not something you could implement)
    • References: WeakReference, SoftReference, PhantomReference
    • Thread - the language doesn't give you a specific instruction to start a thread, rather it magically applies it to the start() method.
    • Throwable - the root of all classes that can work with throw, throws and catch, as well as the compiler understanding of Exception vs. RuntimeException and Error.
    • NullPointerException and other exceptions such as ArrayIndexOutOfBounds which can be thrown by other bytecode instructions than athrow.

    Interfaces

    • Iterable - the only interface that can be used in an enhanced for loop

    Honorable mentions goes to:

    • java.lang.reflect.Array - creating a new array as defined by a Class object would not be possible.
    • Annotations They are a special language feature that behaves like an interface at runtime. You certainly couldn't define another Annotation interface, just like you can't define a replacement for Object. However, you could implement all of their functionality and just have another way to retrieve them (and a whole bunch of boilerplate) rather than reflection. In fact, there were many XML based and javadoc tag based implementations before annotations were introduced.
    • ClassLoader - it certainly has a privileged relationship with the JVM as there is no language way to load a class, although there is a bytecode way, so it is like Array in that way. It also has the special privilege of being called back by the JVM, although that is an implementation detail.
    • Serializable - you could implement the functionality via reflection, but it has its own privileged keyword and you would spend a lot of time getting intimate with the SecurityManager in some scenarios.

    Note: I left out of the list things that provide JNI (such as IO) because you could always implement your own JNI call if you were so inclined. However, native calls that interact with the JVM in privileged ways are different.

    Arrays are debatable - they inherit Object, have an understood hierarchy (Object[] is a supertype of String[]), but they are a language feature, not a defined class on its own.

    0 讨论(0)
  • 2021-01-29 21:00

    Well since the special handling of assert has been mentioned. Here are some more Exception types which have special treatment by the jvm:

    • NullPointerException
    • ArithmeticException.
    • StackOverflowException
    • All kinds of OutOfMemoryErrors
    • ...

    The exceptions are not special, but the jvm uses them in special cases, so you can't implement them yourself without writing your own jvm. I'm sure that there are more special exceptions around.

    0 讨论(0)
  • 2021-01-29 21:01

    There is some magic in the System class.

    System.arraycopy is a hook into native code

    public static native void arraycopy(Object array1, int start1, 
      Object array2, int start2, int length);
    

    but...

    /**
     * Private version of the arraycopy method used by the jit
     * for reference arraycopies
     */
    private static void arraycopy(Object[] A1, int offset1,
      Object[] A2, int offset2, int length) {
       ...
    }
    
    0 讨论(0)
  • 2021-01-29 21:05

    All of the Number classes have a little bit of magic in the form of Autoboxing.

    0 讨论(0)
提交回复
热议问题