Tuesday, March 27, 2007

SNIPPET: java.lang.String

SNIPPET: java.lang.String

The java.lang.String class is final. This class represents character strings. Every string is an instance of this class. Strings are constant, immutable, and the value of a String instance can’t be changed after assignment, although the values can be shared. String buffers though are mutable.

package api_package;

public class CharacterObjects {

//we'll use our char to make strings

static char[] slimman = {'Y','A','R','\'','D','U','A'};

static char[] fatman = {'A','T','I','K','U'};

public static void main(String[] args) {

//we'll use a constructor that takes array of char

String slimstring = new String(slimman);

String fatstring = new String(fatman);

//we'll play with U like before.let's pick the letter

char container = slimstring.charAt(6);

//now what did we pick?

System.out.println("We picked "+container);

//i was wrong. i forgot this is index 0.

//let's ask the all knowing machine for index

System.out.println(slimstring.indexOf("U"));//5

//so let's do it again

container = slimstring.charAt(5);

System.out.println("We picked "+container);

//let's join the two strings.

System.out.print("Joining the two strings gives ");

System.out.println(slimstring+" " + fatstring);

}

}

If there are any tips and tricks you’d like to share, do. Responses are always welcome.

No comments: