Hello,

Quick and easy question:

I'd like to learn a little by investigating the source code behind the predefined Java methods/classes/etc. For example, right now I'm learning to work with TreeSet and I'd like to know exactly what's going on behind the class-specific methods such as 'first', 'last', 'pollFirst', 'pollLast', etc.

Is there somewhere I can go to find these? Thank you!

EDIT: Also, while I'm here, what is this statement doing (using a random, probably incorrect line of code as an example)?

Object obj = (Object)toString();

In particular, what does this statement mean exactly?

(Object)

Thanks again!

Dani AI

Generated

As asked, two simple ways exist to read the implementation of Java classes: open the JDK source that ships with the JDK distribution, or browse the OpenJDK source online. pointed to the OpenJDK source — that is the canonical place for reference implementations — and / were right that modern IDEs will jump straight to source when it is available.

Practical steps: most JDK installs include a src.zip (look under JAVA_HOME) or provide a separate "source" package. If the IDE opens only a stub or a decompiled view, attach the JDK source in project settings (Common workflow: NetBeans: Navigate -> Go to Source; IntelliJ: Ctrl/Cmd+Click; Eclipse: Open Declaration / right‑click JRE System Library -> Attach Source). If source is not present locally, the OpenJDK repositories publish the same files (see the OpenJDK site).

TreeSet specifics: TreeSet is a thin wrapper around a TreeMap, so methods like first, last, pollFirst, pollLast delegate to the backing map. The poll variants remove and return the boundary element or return null when empty. For exact behavior (comparator handling, nulls, concurrent modification rules), inspect TreeSet.java and TreeMap.java in the JDK source.

Clarifying the cast: a cast like (Object) only tells the compiler to treat the expression as that reference type; it does not change the runtime object. Upcasts are implicit; downcasts are checked at runtime and can throw ClassCastException. Example:

String s = "txt";
Object o = s;            // safe upcast
Integer n = (Integer) o; // runtime ClassCastException

Note: some core methods are implemented natively inside the JVM and will not appear as Java source; those are separate from the JDK Java-level classes.

Recommended Answers

All 7 Replies

toString() returns a String representation of the Object it's called upon, or, if you have that code exactly, the toString method from within that same class.
(Object) just parses that what follows to the type Object. since a String (and actually every Java Object) will pass a is-a test when checked for Object, this parse is possible.

toString() returns a String representation of the Object it's called upon, or, if you have that code exactly, the toString method from within that same class.
(Object) just parses that what follows to the type Object. since a String (and actually every Java Object) will pass a is-a test when checked for Object, this parse is possible.

Bump, sorry you answered my second question wonderfully, but I'd still like to know where to find the source code for the predefined Java functions.

Thanks for any further help!

Bump, sorry you answered my second question wonderfully, but I'd still like to know where to find the source code for the predefined Java functions.

Thanks for any further help!

Hmm well, in netbeans IDE i know you can right click on a predefined function->Navigate->Go to Source (Ctrl+Shift+B)

To see the actual source, download the OpenJDK source. The OpenJDK code is functionally identical to that of the new Oracle Java 7.x - for source of Oracle 6.x you may have to go through a hassle to get it.

James not necessary. Professional IDEs like IntelliJ, Eclipse and even NetBeans have option to check on source of even JDK methods calls as cOrRuPtG3n3t!x already indicated for NetBeans.
For IntelliJ it is Ctrl (Windows or Unix)/CMD(MAc OS) + left mouse click on the method. this will open class to which method belongs. If class source code available it will shot it to you, otherwise it will ask if you would like to download it (on temporary cache bases)
I'm sure someone from Eclipse world can chip in and reveal Eclipse way how to get this working

Cheers, never knew that, used it for seeing code of libs i have, never realised it worked on core Java stuff.

Cheers, never knew that, used it for seeing code of libs i have, never realised it worked on core Java stuff.

No problem. They do not teach this stuff at school :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.