|
Java™ by example!
|
|
|
What is a String and how do I create one?
Internally, a String encapsulates an array of characters. The reason for this encapsulation is that the String object ensures that the string will be maintained properly. It also encapsulates a number of useful methods that can be performed on a string. The most important thing to remember is that a String is immutable, which means that after you assigned your String object a value, it cannot change anymore during its lifetime. If you want to manipulate the characters, consider using the StringBuffer class! There are two ways to create a String object:
- implicitely: when you use a string literal, like "Hello, world!", Java automatically creates a String object for you.
For example:
 String s = "Hello, world!";
|
You can use all String methods on a string literal, eg.
 s = "Hello, World!".substring(7); // assigns "world!" to s
|
- explicitely: when you use the new operator to instantiate a String object.
For example:
 String s = new String("Hello, world!");
|
This will call the String class constructor that takes another String as parameter. (remember, "Hello, world!" is represented internally as a String object!). The String object has other constructors to create Strings as shown in the following testprogram:
 public class TestProg { public static void main(String args[]) { try { String s; StringBuffer sb = new StringBuffer("A StringBuffer"); // create a char array char ca[] = "A character array!".toCharArray(); // create a byte array containing the default encoding ISO-Latin-1 byte ba[] = "A byte array!".getBytes(); // create a byte array containing Unicode byte bau[] = "A Unicode byte array!".getBytes("Unicode"); // create an empty String s = new String(); System.out.println(s); // prints out nothing // create a String from a byte array s = new String(ba); System.out.println(s); // prints out "A byte array!" // create a String from a byte array from index 2 (inclusive), take 1 byte s = new String(ba, 2, 1); System.out.println(s); // prints out "byte" // create a String from a byte subarray containing Unicode s = new String(bau, "Unicode"); System.out.println(s); // prints out "A Unicode byte array!" // create a String from a byte subarray containing Unicode s = new String(bau, 0, 20, "Unicode"); System.out.println(s); // prints out "A Unicode" // create a String from a StringBuffer s = new String(sb); System.out.println(s); // prints out "A StringBuffer" // create a String from a character subarray s = new String(ca, 2, 4); System.out.println(s); // prints out "char" // create a String from a character array s = new String(ca); System.out.println(s); // prints out "A character array!" } catch(java.io.UnsupportedEncodingException e) { System.out.println(e); } } }
|
Notice that you need to catch java.io.UnsupportedEncodingException for the methods in which you have to specify an encoding.
Further Information
Author of answer: unknown
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|