Strings in Java, a brief Introduction
Definition
A String represents a sequence of characters, so the easiest way to represent a sequence of
characters in java is by using a character array.
Example:-
char charArray[] = new char[4];
charArray[0] = ‘J’;
charArray[1] = ‘A’;
charArray[2] = ‘V’;
charArray[3] = ‘A’;
But strings created as character arrays as above do not support a range of operations we
may like to perform on strings. So strings are implemented in JAVA as objects of two classes’
viz. String and String Buffer.
String Class
Example
String Name;
Name = new String(“Asif”);
Or
String Name = new String(“Asif”);
String Arrays
An array of strings can be created as follows.
String listOfStudents = new String[4];
The above array will store three string constants.
Following program demonstrates use of length(), tolOwerCase(), toUpperCase() functions. Run it and
check the output.
public class MyString {
public static void main(String args[]){
String s = new String();
s="hello world";
[Link](s);
//Display Length of the string
[Link]("Length of the string is " + [Link]());
//Create Upper case string
String new_s;
new_s=[Link]();
[Link]("Upper case string is " + new_s);
String t = new String("SRINAGAR");
[Link](t);
//Change to lower case
t=[Link]();
[Link](t);
}