Implicitly calling a method in a class from main

rafc

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 enter image description here I can't understand how Java automatically called the toString method (brackets and commas), can you please explain?

T.J. Crowder

Java calls toString when you use + on a String and an object.

So your

System.out.println("Inizio: " + inizio + "\n" + "Fine: " + fine);

is the same as

System.out.println("Inizio: " + inizio.toString() + "\n" + "Fine: " + fine.toString());

except that if inizio or fine is null, the first won't give you an error (you'll see null in the string instead) but the second will.

From the JLS's section on the String Concatenation operator:

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.

which refers to the String Conversion section, which says (after discussing converting primitives to string):

...

Now only reference values need to be considered:

  • If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

  • Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java calling a void method in a different class to a main class?

From Javascript

Calling a class method from the constructor

From Javascript

Calling a method from another method in the same class

From Java

calling String method from different class to main class

From Java

Calling method of Another class from run() method

From Java

calling another method from the main method in java

From Java

Calling Java Main method from static block

From

Calling activity class method from Service class

From Dev

Calling method from State class

From Dev

Calling a default method from Interface in Main Class

From Dev

Calling a parametrized method from LibraryCollection class to main Class

From Dev

Calling a function from a class method

From Dev

Calling function from a class from the main code

From Dev

Calling a method from a class

From Dev

Calling a class from another class with main method

From Dev

Calling Main Method in Java Class From JSP

From Dev

In which class, main () method calling is done?

From Dev

calling a method from the main to output a phrase in java

From Dev

Calling a class function from main with parameters

From Dev

Calling method from a different class

From Dev

coffeescript class - calling calling one class method from an other

From Dev

Calling and passing to a method from class

From Dev

Printing an Integer and their half calling it from main method

From Dev

Calling a method from one class in another class

From Dev

calling a method from tkinter class

From Dev

Calling a function from a class in main

From Dev

Calling main method within the class python

From Dev

Calling an instance method from a class method - Ruby

From Dev

Calling array method in main class

Related Related

HotTag

Archive