Java Reference
In-Depth Information
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 char-
acters in the string. Characters are indexed from 0 tHRough length()-1 ,
and can be accessed with the charAt method, which takes an integer in-
dex 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 ref-
erence. You can, however, construct a new String object from an array
of characters by passing the array as an argument to a String construct-
or. You can also obtain an array of characters with the same contents as
a string using the toCharArray method.
 
Search WWH ::




Custom Search