访问java中预定义类的代码

时间:2014-07-24 16:00:39

标签: java algorithm

java中有许多预定义的类。他们有自己的方法,其中一些不能被覆盖。 我只是想知道我们是否有办法访问这些类的代码

示例: java.util.Arrays 中,有sort(int [] a)。

我们如何知道它实现了哪种算法及其实现方式,以及它比我们的正常实现更好?

谢谢。

2 个答案:

答案 0 :(得分:1)

首先,假设JVM开发人员的版本是最好的,这是一个好主意。他们多年来一直努力工作。

其次,据我所知,许多java.util实现都是特定于JVM的。例如,可以找到OpenJDK的java.util.Arrays源代码here

看起来他们在使用的算法上努力工作,请参阅评论:

/**
 * Sorts the specified array into ascending numerical order.
 *
 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
 * offers O(n log(n)) performance on many data sets that cause other
 * quicksorts to degrade to quadratic performance, and is typically
 * faster than traditional (one-pivot) Quicksort implementations.
 *
 * @param a the array to be sorted
 */
public static void sort(int[] a) {
    DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}

答案 1 :(得分:0)

JDK中的方法有几个优点:

  • 不要重新发明轮子
  • 过度测试(今天好好找到它们中的错误)
  • 向您展示实现您想要做的事情的正确方法

对于实施,您可以: