Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Converting Anything to String

Summary: Converting any data to strings is easy. You can do almost everything with concatenation, but can get more control using some of the alternatives.

Converting numbers to strings - See Converting Numbers to Strings

There are many specialized issues with regard to converting numbers (precision, exponents, currency, locale, ...), which are covered in Converting Numbers to Strings.

Summary of conversion alternatives

  • Concatenation (+): Concatenate anything to a string and it will automatically be converted to string.
  • toString() is universally used as the method for converting any object into a string.
  • printf() was added in Java 5 to use a format for conversion. Altho it's primarily used with numbers, it also can be applied to objects of other classes. This isn't discussed here.
  • No conversion required. Some common system methods will take any type and convert it, eg, System.out.println().

Concatenation (+)

The most common idiom to convert something to a string is to concatenate it with a string. If you just want the value with no additional text, concatenate it with the empty string, one with no characters in it ("").

If either operand of a concatenation is a string, the other operand is converted to string, regardless of whether it is a primitive or object type.

Card c = ...;   // Assume Card is a class that defines a playing card.
String s;
. . .
s = c;             // ILLEGAL
s = "" + c;        // Might assign "Three of Hearts" to s
s = c + " is trouble";  // Assigns "Three of Hearts is trouble" to s.

This conversion to string is made by calling the object's toString() method.

toString() method - Define it for your classes

When Java needs to convert an object to a String, it calls the object's toString() method. Because every class (object type) has the class Object as an ancestor, every class inherits Object's toString() method. This will do something to generate a string from an object, but it will not always be very useful. If the child class doesn't override toString(), the default probably won't print anything interesting. Just as many of the Java library classes override toString() to produce something more useful, you should also override toString() in your classes.

Enums

[There should be something here about Java 5's enums.]

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.