I am new to Java and I am trying to solve an exercise given by my professor. He has given this class
public class MioPunto {
public int x;
public int y;
public String toString() {
return ("[" + x + "," + y + "]");
}
}
and I am supposed to write another class with a main method. Into the main I have to print the coordinates without explicitly calling the "toString" method. I unconsciously solved it in this way
public class TestMioPunto{
public static void main(String[] args){
MioPunto inizio = new MioPunto();
MioPunto fine = new MioPunto();
inizio.x=10;
inizio.y=10;
fine.x=20;
fine.y=30;
System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);
}
}
and the output is
I can't understand how Java automatically called the toString method (brackets and commas), can you please explain?