Creating Strings in Java

You might have noticed that you can add strings together with the + operator in Java, just as you can in JavaScript:

 public class ch10_05  {     public static void main(String[] args)     {         double chargesDue[][] = {{1093.66, 667.19, 45.99, 890.30, 99.06},                                    {2019.00, 129.99, 19.01, 630.90, 23.17}};  System.out.println("Customer 4 owes $" + chargesDue[0][4] + " in the Eastern graphics/ccc.gif branch.");   System.out.println("Customer 4 owes $" + chargesDue[1][4] + " in the Western graphics/ccc.gif branch.");  } } 

This works because strings are supported by the built-in class String in Java. In fact, the String class is treated in a special way in Java; you can use it just as you would any built-in data type, as in this case (note that I don't have to use the new operator or call the String class's constructor here):

 public class ch10_05  {     public static void main(String[] args)     {  String welcome = "Welcome to Java";  .         .         . 

You can treat this new String variable as you would other simple variables , including printing it like this:

 public class ch10_05  {     public static void main(String[] args)     {         String welcome = "Welcome to Java";  System.out.println(welcome);  } } 

Java String Classes

In fact, really two string classes are available in Java: the String and StringBuffer classes. For most purposes, String objects are read-only because they don't allow you to change their internal data. However, you can change the internal text in the StringBuffer class. Both these classes have a great many methods built into them, which you can find in the Java documentation.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net