Most of the time, if you are using a single character value, you will use the primitivechartype. There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" thecharin aCharacterobject for this purpose. An object of typeCharactercontains a single field whose type ischar. ThisCharacterclass also offers a number of useful class (i.e., static) methods for manipulating characters.Strings are a sequence of characters and are widely used in Java programming. In the Java programming language, strings are objects. The
Stringclass has over 60 methods and 13 constructors.Most commonly, you create a string with a statement like
rather than using one of theString s = "Hello world!";Stringconstructors.The
Stringclass has many methods to find and retrieve substrings; these can then be easily reassembled into new strings using the+concatenation operator.The
Stringclass also includes a number of utility methods, among themsplit(),toLowerCase(),toUpperCase(), andvalueOf(). The latter method is indispensable in converting user input strings to numbers. TheNumbersubclasses also have methods for converting strings to numbers and vice versa.In addition to the
Stringclass, there is also aStringBuilderclass. Working withStringBuilderobjects can sometimes be more efficient than working with strings. TheStringBuilderclass offers a few methods that can be useful for strings, among themreverse(). In general, however, theStringclass has a wider variety of methods.A string can be converted to a string builder using a
StringBuilderconstructor. A string builder can be converted to a string with thetoString()method.
ATENCIÓN: La traducción de esta documentación es un esfuerzo personal y voluntario. NO es un documento oficial del propietario de la tecnología Java, Oracle, ni está patrocinado por esta empresa.
Los documentos originales y actualizados (en inglés) están disponibles en: http://docs.oracle.com/javase/tutorial/. La versión disponible en este sitio es la publicada en Marzo de 2008 (más información en: "What's new and What's Old? The History of the Tutorial").
Dirige cualquier comentario, petición, felicitación, etc. a tutorialesjava@codexion.com.
Si quieres ayudar a mantener en funcionamiento esta web, colaborar con la traducción de estos documentos o necesitas que se traduzca algún capítulo en concreto puedes invitarme a un café: