Things to know about Strings



Things to know about Strings

• Strings are a class

• This means that they are objects and have methods

• So you can create a new string like String ss = new String("hello"); -- just as you create any object.

• Java also, rather nicely, pretends that it is a primitive data type only as far as initializing it. You can make a string by saying String ss = "hello"; just like int x = 5;

But int is not an object and does not have variables and functions in it like a string does.

• A string is not a character array. (You can't access letters by using s[i])

• A string is drawn as a character array in order to visualize it

• A string is immutable – you cannot change it once it's been made.

• Implications: any time you try to change a string, you end up creating a new one in a new piece of memory. The old string gets discarded and eventually cleaned up by the Java garbage collector.

o illustrate this: the following lines actually require the creation of 3 strings in memory.

o String ss = "Hello"; ss = ss + " Jim";

• In the past, we were told to use StringBuilder class to do a lot of string manipulation. Now we are told that for most cases the String class is sufficient (unless you notice the program being a bit slow or using a lot of memory).

• ss.replace("0","zero") will make a new string that has all of the 0 replaced with "zero". BUT it will not put the new string into ss.

You have to write s2 = ss.replace("0", "zero"); or ss = ss.replace("0", "zero");

• There is no method to make a string of one character, but it is easy to write one:

static String fill(char c, int len){

// create a char array of "len" characters

char[] fill = new char[len];

// Fill the char array with all "c"s

Arrays.fill(fill, c);

// Create string using the char array

return new String(fill);

}

• Converting string ( ( numbers normally involves wrapper classes



How to convert numbers to strings etc:

1. numbers to strings (I am using int and Integer here, but it's the same with any number type)

a. add "" to the number:

int num = 506; String s = num + "";

b. use valueOf method //valueOf is a common instance method in many classes

String s = s.valueOf(num);

c. use toString //Integer.toString(int) is a static method of the Integer class

String s = Integer.toString(num);

2. Change string to number (e.g. an integer)

a. use valueOf() //Integer.valueOf(String) is a static method

// returns Integer object

int n = Integer.valueOf("72");

b. use parseInt() //Integer.parseInt(String) is a static method

//returns int primitive data type

int n = Integer.parseInt("72");

The difference is that valueOf() returns an Integer, and parseInt() returns an int (a primitive type).

3. Changing primitive type to wrapper class

a. Double dd = Double.valueOf(5.6);

b. Double dd = new Double(5.6);

4. Changing wrapper class to primitive type

a. Integer ii = new Integer(-38);

int i = ii.intValue( ) //will return the int value of the Integer object

5. Finding ASCII number of characters:

a. char cc = 'A';

int num = Integer.valueOf(cc)

6. Change ASCII number to Character:

a. int num=69;

char cc = (char) num;

7. Change string to character array

a. String s = "abcABCabcABC";

char[] charArray;

charArray = s.toCharArray();

8. Change character array to string

a. String s = new String(charArray);

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download