0

There are many predefined classes in java. They have there own methods some of which cannot be overridden. I just want to know if there is any way for us to access code of those classes.

Example: In java.util.Arrays , there is sort(int[] a).

How can we know which algorithm it implements and how it implements and how is it better than our normal implementation?

Thank you.

Rohan
  • 11
  • 1
  • Look for src.zip inside the JDK. – Kayaman Jul 24 '14 at 16:02
  • 1
    The JDK comes with the source; but you can also find it online: http://www.docjar.com/html/api/java/util/Arrays.java.html. I usually point my IDE to the src.zip file, that way I can switch between my code and the java lib code easily. – Chris K Jul 24 '14 at 16:02
  • the thing is the OP will mess up his arrays class doing that. – Deepanshu J bedi Jul 24 '14 at 16:13
  • @DeepanshuBedi surely that depends on the IDE, I've had no trouble using IntelliJ. I assume that the new Arrays implementation is not in a package called java.util.Arrays. – Chris K Jul 24 '14 at 16:17

2 Answers2

1

Firstly, it's a good idea to assume that the JVM developer's version is the best. They've worked hard on it for years.

Secondly, as far as I can tell, much of the java.util implementations are JVM specific. OpenJDK's java.util.Arrays source code, for example, can be found here.

Looks like they worked hard on the algorithm used, see the comments:

/**
 * 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);
}
djhaskin987
  • 9,741
  • 4
  • 50
  • 86
  • 1
    As an interesting aside that adds to this answer, some methods have Java source as you mentioned above but they get intercepted and replaced by the JVM at run time. This lets the JVM use assembler code that is more optimized than anything that can be written in Java, for example a lot of the Math functions are intrinsics. For more see http://stackoverflow.com/questions/19892322/when-will-jvm-use-intrinsics. When this happens, it is another reason why using the JDK library is a good idea. – Chris K Jul 24 '14 at 16:11
0

Methods in the JDK have several advantages:

  • do not reinvent the wheel
  • overly tested (good luck to find a bug in them today)
  • show you a proper way to achieve what you are trying to do

For the implementation, you can:

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Non-fixed bug in Spring since ages: It assumes `Thread.currentThread().getContextClassLoader()` to be not `null` when it _may_ be `null` (Though only if JVM is started by a dirty caller) – AlexR Jul 24 '14 at 16:09
  • 1
    There are plenty of bugs everywhere. Yet there are probably less **unknown** bugs in good old libraries that have been used billion of times than in your newly written code. – Jean Logeart Jul 24 '14 at 16:13
  • I totally agree with you in the message, just not the point that Java is bug-free as suggested by your point 2 ;) – AlexR Jul 24 '14 at 16:19