I have the following code:

import  java.util.*;

public class Final{

private double guess = 1;

public Final(double x){

root(x);
}

public double root(double b){
double rez = -1;
if(Math.abs(guess*guess - b) < 0.001){
rez = guess;
}
else{
guess = (2/guess + guess)/2;
rez = root(b);
}
return rez;
}

public double rooting(double d){

while(Math.abs(guess*guess - d) > 0.001){
System.out.println(guess);
guess = (2/guess + guess)/2;
}
return guess;
}


public static void main(String[] args){

System.out.println(new Final(2));

}

in other words, the two methods do the same job: they find the square root of a positive integer using the Newton's method. And when I call the methods inside the main method using the following code:

Final x  = new Final();
x.root(2);
x.rooting(2);

they both work fine. However, when calling the methods inside the constructor, using such code as:

System.out.println(new Final(2));

what I get in the screen is: Final@9cab16.

Finally, I am aware that this is perhaps out of bounds of a practical question, but I would like to know what is going on.

Anybody with an answer is very much thanked!

Your are expecting the constructor of your class to return a value. If you look at the syntax for a constructor you see that it does not return any value.
You need to create an instance of the class and call one of its methods that returns the value you want.

The String that you see printed: Final@9cab16 is the default returned by an object's toString method. Its the name of the class + @ + the value returned by the hashcode() method which is often the memory address of the object.

When executing the line of code:
System.out.println(new Final(2));
an address of the instance of the Final is printed as 9cab16 (Final@9cab16)
which tells that the instance of Final is stored at the memory: 9cab16 in hexadecimal system

NormR1 wrote:

Your are expecting the constructor of your class to return a value. If you look at the syntax for a constructor you see that it does not return any value.

Sorry Norm, that's wrong. You don't get to specify the return value because a constructor ALWAYS returns the new instance.

This question has already been answered. Start a new discussion instead.