Advertisement
public String toString()
class A
{
public static void main(String... ar)
{
A ob = new A();
System.out.println(ob); //object ob is printed, hence toString() method is implicitly called here
}
}
A@70dea4e
Advertisement
class A
{
int i;
public String toString()
{
return "We have created an object";
}
public static void main(String... ar)
{
A ob1 = new A();
ob1.i=10;
System.out.println(ob1);
A ob2 = new A();
ob2.i=20;
System.out.println(ob2);
}
}
We have created an object
We have created an object
class Database
{
String name;
String city;
int age;
float donation;
public void putName(String str)
{
name = str;
}
public void putCity(String str)
{
city = str;
}
public void putAge(int i)
{
age = i;
}
public void putDonation(Float f)
{
donation = f;
}
public String toString() //Overriding toString() method
{
return name + " from " + city + " aged " + age + " has donated - " + donation + "$";
}
public static void main(String... ar)
{
Database ob1 = new Database();
ob1.putName("Howard");
ob1.putCity("New York");
ob1.putAge(40);
ob1.putDonation(1000.50f);
System.out.println(ob1); //Printing the first object directly
Database ob2 = new Database();
ob2.putName("Steve");
ob2.putCity("Sydney");
ob2.putAge(30);
ob2.putDonation(500.50f);
System.out.println(ob2); //Printing the second object directly
}
}
Howard from New York aged 40 has donated - 1000.5$
Steve from Sydney aged 30 has donated - 500.5$
class A
{
public static void main(String... ar)
{
Integer i = new Integer(40);
Float f = new Float(10.4);
Double d = new Double(2222.2222);
System.out.println(i);
System.out.println(f);
System.out.println(d);
}
}
40
10.4
2222.2222
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement