-1

I'm using Java/Eclipse, and I wonder how to check how the methods do things.

Like, when I write

Math.sqrt(16)

It returns the value 4, but how? How does this method actually do that? What's the logic behind it? Is there a way to look them?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

If you using any kind of IDE like InteliJ IDEA or Geany, you can simply have access to a imported or in-built method by clicking on the method name while pressing the control(CTRL) key. Then you can see what is inside that code. If there are more methods in that code, do the same for them too. Then you will be able to understand how that method is working.

When considering it in Object oriented programming for your example, here we didn't create a object to work the method. It means Math class has a static method named sqrt which can take a number as an argument and its return type is declared as number too.

  • Thank you but in Eclipse it's not working as it supposed to do. CTRL+Click is opening that class, yes, but its opening compiled form of the code. So I can't understand anything. It says "The JAR file C:\Program Files\Java\jre-10.0.2\lib\jrt-fs.jar has no source attachment", if you have any idea how to solve this please write down below. I'll try it on IntelliJ IDEA too. – Umut Emre Önder Jan 08 '21 at 21:24
  • 1
    @UmutEmreÖnder It looks like you only have a JRE installed, you need the JDK to see the source. However Math.sqrt is implemented as a `native` method so even the JDK won't help for that particular method. It is also annotated with `@HotSpotIntrinsicCandidate` so the JIT may convert it directly to machine instructions. – greg-449 Jan 08 '21 at 21:29
  • Actually I was telling to you about receiving the view of inside of the user-defined code. Even though you can't see the native methods, surely you can see user-defined methods and inside of its code. Try it with yourself by creating your own method which can be declared in another class or another package. You may need to import those classes (If they are not in the same package) to your working class to use that method and CTRL + Click also work for this situation. – shalitha anuradha Jan 26 '21 at 07:55
0

The standard library is implemented within the JDK. OpenJDK is under the GNU general public license, and it is the implementation of the JDK upon which most other implementations are built. You can find its source code on GitHub here. It's a bit difficult to navigate, but the implementation of the sqrt function can ultimately be found here. It is extensively documented. More information about the sqrt implementation and other Java math implementations can be found within the answers to this question.

Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20