what is String Objects?

A String class type deals specifically with sequences of character data and provides language-level support for initializing them. The String class provides a variety of methods to operate on String objects.

You've already seen string literals in examples like the HelloWorld program. When you write a statement such as

System.out.println("Hello, world");



the compiler actually creates a String object initialized to the value of the specified string literal and passes that String object as the argument to the println method.

You don't need to specify the length of a String object when you create it. You can create a new String object and initialize it all in one statement, as shown in this example:

class StringsDemo
{
public static void main(String[] args)
{
String myName = "Petronius";

myName = myName + " Arbiter";
System.out.println("Name = " + myName);
}
}



Here we declare a String variable called myName and initialize it with an object reference to a string literal. Following initialization, we use the String concatenation operator (+) to make a new String object with new contents and store a reference to this new string object into the variable. Finally, we print the value of myName on the standard output stream. The output when you run this program is

Name = Petronius Arbiter



The concatenation operator can also be used in the short-hand += form, to assign the concatenation of the original string and the given string, back to the original string reference. Here's an upgraded program:

class BetterStringsDemo
{
public static void main(String[] args)
{
String myName = "Petronius";
String occupation = "Reorganization Specialist";

myName += " Arbiter";

myName += " ";
myName += "(" + occupation + ")";
System.out.println("Name = " + myName);
}
}



Now when you run the program, you get this output:

Name = Petronius Arbiter (Reorganization Specialist)



String objects have a length method that returns the number of characters in the string. Characters are indexed from 0 tHRough length()-1, and can be accessed with the charAt method, which takes an integer index and returns the character at that index. In this regard a string is similar to an array of characters, but String objects are not arrays of characters and you cannot assign an array of characters to a String reference. You can, however, construct a new String object from an array of characters by passing the array as an argument to a String constructor. You can also obtain an array of characters with the same contents as a string using the toCharArray method.

String objects are read-only, or immutable: The contents of a String never change. When you see statements like

str = "redwood";
// ... do something with str ..
str = "oak";

the second assignment gives a new value to the variable str, which is an object reference to a different string object with the contents "oak". Every time you perform operations that seem to modify a String object, such as the += operation in BetterStringsDemo, you actually get a new read-only String object, while the original String object remains unchanged.


The concatenation operator converts primitive values to strings using the toString method of the corresponding wrapper class. This conversion doesn't give you any control over the format of the resulting string. Formatted conversion can be done with the java.util.Formatter class. A Formatter can write its output to a string, a file, or some other output device. For convenience the System.out.printf method (for "print formatted") uses a formatter to write to System.out. Formatted output uses a format string together with a set of values to format. The format string contains normal text together with format specifiers that tell the formatter how you want the subsequent values to be formatted. For example, you can print the value of Math.PI to three decimal places using

System.out.printf("The value of Math.PI is %.3f %n", Math.PI);

which prints

The value of Math.PI is 3.142

whereas using println and string concatenation you'd get:

The value of Math.PI is 3.141592653589793

A format specifier consists of at least two parts: it starts with a % character, and it ends with a conversion indicator. The conversion identifies both the type of the value to format, and the basic form. For example, %f says to format a floating-point value in the usual decimal form, as used for Math.PI above, whereas %e says to format a floating point value in scientific notationfor example, 3.142e+00. Integer values can be formatted by %d for normal decimal, or %x for hexadecimal form. A string can be formatted by %s.

A format specifier can provide additional formatting information. You can provide a width that indicates the minimum number of characters to printuseful for aligning columns of data. If the formatted value has fewer characters than the width, it is padded with spaces to fit the minimum size. This lets you align values easily. Some conversions also allow a precision value to be given, which is written as . followed by a non-negative number. For floating-point values using %f the precision indicates how many decimal places to round toin the example above the value of Math.PI is rounded to 3 decimal places because of the .3 in the format specifier. If both a width and precision are given they are written in the form width.precision. Additional flags in the format specifier can, among other things, request zero padding (instead of spaces) or left-justification (the default is right justification).


We Are Founder..