Glorified classes in the Java language

Java

Java Problem Overview


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 from the compiler and/or JVM.

The ones I come up with right away are:

  • Object (obviously) as it, among other things doesn't have a super class.
  • String as the language has special support for the + operator.
  • Thread since it has this magical start() method despite the fact that there is no bytecode instruction that "forks" the execution.

I suppose all classes like these are in one way or another mentioned in the JLS. Correct me if I'm wrong.

Anyway, what other such classes exist? Is there any complete list of "glorified classes" in the Java language?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

http://www.docjar.com/docs/api/sun/misc/Unsafe.html">sun.misc.unsafe</a> is the mother of all dirty, spirit-of-the-language-breaking hacks.

Solution 4 - Java

  1. Enum. You're not allowed to subclass it, but the compiler can.
  2. Many things under java.util.concurrent can be implemented without JVM support, but they would be a lot less efficient.

Solution 5 - Java

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

Solution 6 - Java

Since the important classes were mentioned, I'll mention some interfaces:

The Iterable interface (since 1.5) - it allows an object to participate in a foreach loop:

Iterable<Foo> iterable = ...;
for (Foo foo : iterable) {

}

The Serializable interface has a very special meaning, different from a standard interface. You can define methods that will be taken into account even though they are not defined in the interface (like readResolve()). The transient keyword is the language element that affects the behaviour of Serializable implementors.

Solution 7 - Java

  1. Throwable, RuntimeException, Error AssertionError
  2. References WeakReference, SoftReference, PhantomReference
  3. Enum
  4. Annotation

Solution 8 - Java

Java array as in int[].class

Solution 9 - Java

Solution 10 - Java

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

Solution 11 - Java

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) {
   ...
}

Solution 12 - Java

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.

Solution 13 - Java

Most of those classes isn't really implemented with 'special' help from the compiler or JVM. Object does register some natives which poke around the internal JVM structures, but you can do that for your own classes as well. (I admit this is subject to semantics, "calls a native defined in the JVM" can be considered as special JVM support.)

What /is/ special is the behaviour of the 'new', and 'throw' instructions in how they initialise these internal structures.

Annotations and numbers are pretty much all-out freaky though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaioobeView Question on Stackoverflow
Solution 1 - JavaYishaiView Answer on Stackoverflow
Solution 2 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 3 - JavaDean JView Answer on Stackoverflow
Solution 4 - JavaDarronView Answer on Stackoverflow
Solution 5 - JavaBill the LizardView Answer on Stackoverflow
Solution 6 - JavaBozhoView Answer on Stackoverflow
Solution 7 - JavaemoryView Answer on Stackoverflow
Solution 8 - JavaAlexander PogrebnyakView Answer on Stackoverflow
Solution 9 - JavastarblueView Answer on Stackoverflow
Solution 10 - Javademotics2002View Answer on Stackoverflow
Solution 11 - JavaeljensoView Answer on Stackoverflow
Solution 12 - JavajosefxView Answer on Stackoverflow
Solution 13 - JavamillimooseView Answer on Stackoverflow