View RSS Feed

Java String

String representation of Objects

Rate this Entry
by , 04-26-2012 at 06:25 PM (379 Views)
Tostring() method is implemented by all java objects that will be returning String object, which will be describing that object in a best manner. For instance, red colored object can be printed out by using:
Java Code:
Color color = new Color( 255, 0, 0 );
String colorStr = color.toString();
Console.println( colorStr );
Printout will be:
Java Code:
java.awt.Color[r=255,g=0,b=0]
In such a case, there is no need to call tostring() method explicitly. A version is present in the printIn() method in which an object is taken as an argument. This version calls tostring() method implicitly for the print result.

When a string object is concatenated with other object type, conversion to String happens implicitly. For instance, type:
Java Code:
Color color = new Color( 255, 0, 0 );
String str = "My color looks like: " + color;
Console.println( str );
Following will be obtained, on the console:

My color looks like: java.awt.Color[r=255,g=0,b=0]

Submit "String representation of Objects" to Facebook Submit "String representation of Objects" to Digg Submit "String representation of Objects" to del.icio.us Submit "String representation of Objects" to StumbleUpon Submit "String representation of Objects" to Google

Categories
String

Comments