Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

View how a method is implemented in Java  RSS feed

 
kesh tom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, I want to see the how the actual method is written for remove(), get() and so on when I call it on an Array List. I tried to read the documentation but it is only telling me what a method does, not show how it is written.

Is there any way I can view it? Any websites? Or maybe need to pull out a jar file from the JDK itself? If yes, which jar file? There is quite a few jar files there. Please advice.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kesh tom wrote:For example, I want to see the how the actual method is written for remove(), get() and so on when I call it on an Array List. I tried to read the documentation but it is only telling me what a method does, not show how it is written.

Is there any way I can view it? Any websites? Or maybe need to pull out a jar file from the JDK itself? If yes, which jar file? There is quite a few jar files there. Please advice.


Java source code is provided as a "src.zip" file, on the root directory of the JDK.

Henry
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a file in your JDK install directory called "src.zip" and that's where the source code is for methods in the standard API. So yeah... not a JAR file but a ZIP file.
 
kesh tom
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it. Thanks.

Wanted to use it to understand how its written instead of blindly using the method. Having said that, the src looks really heavy extending from class to class and so many implementations. To understand a method, I seem to have to trace back a few classes. Not entirely sure if this is going to be the prudent way to approach this compared to looking at some sorting algorithms for example.

Learnt Data structures/ algorithms in year one and kinda just 'shelved' that knowledge since. Turns out, it seems to be viewed as more important than the knowledge of a particular language or that you know many languages.
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kesh tom wrote:... To understand a method, I seem to have to trace back a few classes ...

There is an easier and a visual way to do this : Javadocs.
I understand it wont give you the actual source code, but, it does show you relationships between structures such as classes packages, subclasses, etc..
Here's the Javadocs for java7
https://docs.oracle.com/javase/7/docs/api/

Here's the javadocs for java8
https://docs.oracle.com/javase/8/docs/api/
 
salvin francis
Bartender
Posts: 1588
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an example, If you look at arraylist's remove method:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#remove-int-
public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractList<E>
Parameters:
index - the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())


It tells you that the method exists in that exact signature since The ArrayList class implements List interface as well as extends AbstractList. If you click List, it will show you list's java doc and also show all All Known Implementing Classes: AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
So, you would expect this same method to also exist in Stack, Vector, LinkList, etc... This builds up your knowledge about the structure and relationships. As per the actual implementation, you can look at the java source as mentioned by others.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know, if you use eclipse you can ctrl+t on the class, which is a very useful shortcut. It will open the class (if you have the source attached). For things like List etc it will work if you have jdk installed.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kesh tom wrote:Wanted to use it to understand how its written instead of blindly using the method.

Why? Is there something wrong with the documentation? The whole point of using objects is that you're not supposed to know how they work.

Indeed, in most cases you shouldn't even care whether you're calling ArrayList.remove(), LinkedList.remove() or Vector.remove().

Now if you're just interested in looking at how someone else (and in the case of Java base classes, probably an expert) implemented a particular function, then fine; but whatever you do DON'T make any assumptions about how a method is written - and in particular, don't "optimise" your code based on how you think it's written - because it could change completely in the next release.

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I completely agree with Winston -- this is not a useful exercise. There are far better things you could be doing with your time.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you kesh tom you saved my time of creating another thread.
Winston Gutkowski wrote:Why? Is there something wrong with the documentation? The whole point of using objects is that you're not supposed to know how they work.
Even couple of months ago I was looking for how and where JVM(JVM working) store variables and objects and in which memory like method area, heap area, stack, PC register etc. Then this forum made me realize I don't really need to know this. Even my seniors were asking me to know how the methods of collection class like ArrayList are actually implemented by using source code which is in src.zip. I also saw how ArrayList increases its size. As default size of ArrayList is 10 If I add 11th then in JDK 8 which I'm using it uses a formula whenever you add a new element in ArrayList it calls method to make sure ArrayList's capacity I traced the size of ArrayList in Eclipse Mars1.0 IDE by debugging and when I added 11th element in it then the size of ArrayList became 15 instead 16 If you calculate newCapacity = (10*3)/2 + = 30/2 + 1 = 15 + 1 = 16 (I don't know whether I'm correct or not ). I even saw It uses one dimensional array of Object type which is transient. Some other Collection types uses two dimensional and all. I had even googled about it and found JDK 6 uses formula and JDK 7 something else as
Winston Gutkowski wrote:because it could change completely in the next release
may be they've changed it in new version. I don't know whether all of them results same or not.

BUT I really just don't get that Why on this earth I've been asked to know how these methods are implemented internally? How and where JVM stores what? when I'm not going to code and design JVM and all internal implementation of those methods. Why these topics are given so importance at least in India, may be I'm wrong, may not be in whole India but at least the people(Seniors) around me.
As
Bear Bibeault wrote:I completely agree with Winston -- this is not a useful exercise. There are far better things you could be doing with your time.
yes there are so many things I could have perused rather than spending time in reading and understanding these things. A lot of things are there in Java to learn.
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:BUT I really just don't get that Why on this earth I've been asked to know how these methods are implemented internally?


Sometimes it's instructive to look at other people's code, because you might learn techniques you didn't already know. Of course if that's what you want then you should try to look at competent people's code and not at any old code, so you'd think the JVM code would be a good start. And mostly that's true, although I expect that Sturgeon's law applies there too and most of it is crap (i.e. ordinary code without any flashes of brilliance).

But requiring you to know specific things about one of Oracle's implementations? Well, after being around this forum for a long time I've noticed that it's common for people to use teaching techniques which make no sense to me. (Sometimes I feel like telling the poor struggling beginners "Look, do what your teacher says now or you won't pass the course, but in real life you won't actually write code like that.") So probably this is another one of those techniques.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sometimes it's instructive to look at other people's code, because you might learn techniques you didn't already know.
Agreed ! This makes sense
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!