本文介绍了Java语言中的美化课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准Java API中的某些类与其他类略有不同。我在谈论那些没有编译器和/或JVM特殊支持就无法实现的类。

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.

我立刻想出的是:


  • 对象(显然)因为它除了其他东西之外没有超级class。

  • String 因为该语言对+运算符有特殊支持。

  • Thread 因为它有这个神奇的start()方法,尽管事实上没有forks执行的字节码指令。

  • 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.

我认为像JLS这样的所有类都是以这种或那种方式提到的。如果我错了,请纠正我。

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

无论如何,还有其他类似的类吗? Java语言中是否有完整的荣耀类列表?

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

推荐答案

有很多不同的答案,所以我认为收集它们(并添加一些)会很有用:

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


  • AutoBoxing 类 - 编译器只允许特定的类

  • - 有自己的文字(例如int.class) )。我还会在不创建新实例的情况下添加其通用类型。

  • 字符串 - 使用它重载+ -operator并支持文字

  • 枚举 - 唯一可以在switch语句中使用的类(很快也会赋予String一个特权)。它还做其他事情(自动静态方法创建,序列化处理等),但理论上可以用代码完成 - 它只是很多样板,并且一些约束不能在子类中强制执行(例如特殊的子类化规则)但是如果没有enum的特权状态你永远无法实现的是将它包含在switch语句中。

  • 对象 - 所有对象的根(我会添加它的克隆和finalize方法不是你可以实现的东西)

  • 引用:WeakReference,SoftReference,PhantomReference

  • 线程 - 该语言没有为您提供启动线程的具体指令,而是将其神奇地应用于start()方法。

  • Throwable - 可以使用throw,throws和catch的所有类的根,以及编译器对Exception与RuntimeException和Er的理解ror。

  • NullPointerException 以及其他异常,例如ArrayIndexOutOfBounds,可以被其他字节码指令抛出而不是来自。

  • 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.

  • Iterable - 唯一可以使用的界面在增强的for循环中

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

  • java.lang.reflect。数组 - 创建一个由Class对象定义的新数组是不可能的。

  • 注释它们是一种特殊的语言功能,在运行时的行为类似于接口。您当然无法定义另一个Annotation接口,就像您无法定义Object的替换。但是,您可以实现它们的所有功能,只需要另一种方法来检索它们(以及一大堆样板)而不是反射。实际上,在引入注释之前,有许多基于XML和javadoc标记的实现。

  • ClassLoader - 它肯定与JVM具有特权关系,因为它存在没有语言方式来加载一个类,虽然有一个字节码方式,所以它就像Array一样。它还具有被JVM回调的特权,尽管这是一个实现细节。

  • 可序列化 - 您可以通过反射实现功能,但是它有自己的特权关键字,在某些情况下你会花很多时间与SecurityManager建立亲密关系。

  • 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.

注意:我离开了列出了提供JNI(例如IO)的东西,因为如果您愿意,可以随时实现自己的JNI调用。但是,以特权方式与JVM交互的本机调用是不同的。

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.

数组是有争议的 - 它们继承了Object,具有理解的层次结构(Object []是一种超类型String []),但它们是语言特性,而不是自己定义的类。

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.

这篇关于Java语言中的美化课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:46