Have you ever tried to print an object using System.out.println() ?
Most probably you must have tried printing a String or an Integer value which prints its value.
For example, let say for the following code
It will print "Tom".
or
will print 10.
But what will it print in following scenario.
Let say there is a class Circle with a int variable of radius.
Now let say you make an object of class Circle and print it.
What should I expect to get printed now? Radius of the Circle or any other thing.
When you run this code, it will print the following.
Why this? What is that number after @ sign and why that name of class.
When any object is printed under System.out.println(), it prints its response of toString().
{
}
}
Now when you print the object of class Circle, output will be different.
Output: Radius=24
Most probably you must have tried printing a String or an Integer value which prints its value.
For example, let say for the following code
String s="Tom";System.out.println(s);
It will print "Tom".
or
int i=10;System.out.println(i);
will print 10.
But what will it print in following scenario.
Let say there is a class Circle with a int variable of radius.
class Circle
{
int radius;
Circle(int radius)
{
this.radius=radius;
}
}
Now let say you make an object of class Circle and print it.
Circle c= new Circle(10);
System.out.println(c);
What should I expect to get printed now? Radius of the Circle or any other thing.
When you run this code, it will print the following.
Circle@15db9742
Why this? What is that number after @ sign and why that name of class.
When any object is printed under System.out.println(), it prints its response of toString().
toString() method
java.lang.Object- Alpha class or super class for all the class that exist in Java have a method with following signature.
public String toString()
By default java.lang.Object is designed to return the following
NameofClass@Hexcode(hashCode)
Hence the output,
Circle@15db9742
Then why is it printing it differently for String and Integer?
Simple!! They have overridden the toString() method.
Let say I want the output of Circle object when printed as "Radius=radiusValue", I will have to override the toString() method.
class Circle
{
int radius;
Circle(int radius)
{
this.radius=radius;
}
public String toString()
{
String x="Radius="+radius;
return x;
}
}
Now when you print the object of class Circle, output will be different.
Circle c=new Circle(24);
System.out.pritnln(c);
Output: Radius=24
Tricky.
ReplyDeleteSalaam!
Can you help me with XML's?
I have 2 XML:-
1) a template of XML
2) RAW XML from page
both XML have same script only content within the script differs.
eg:- 123 (This is present in both XML only the 123 keep changing to different numbers in RAW XML)
problem:- every time i have to copy data from RAW XML & paste in template to create some output.
Seeking:- Can we generate a code so that i can auto copy paste the desired data from RAW to template?
Thanks,
Faizan
Yes that can be achieved. I will need to see the template and raw xml demo. Sorry for delay in reply
ReplyDeletejazak Allahu khair....i'll share you the both XML's
ReplyDelete