java tostring method

java tostring method tutorial explained

#java #tostring #method #tutorial #explained

//*
public class main {

public static void main(string[] args) {

// tostring() = special method that all objects inherit,
//that returns a string that "textually represents" an object.
//can be used both implicitly and explicitly

car car = new car();


system.out.println(car.tostring());

// or

system.out.println(car);


}
}
//*
public class car {


string make = "ford";
string model = "mustang";
string color = "red";
int year = 2021;

public string tostring() {

return make +"\n"+model+"\n"+color+"\n"+year;

}
}
//*

java tostring method