Strings
1. Most of the data that transmit on internet is in the form of group of
characters.
2. such group of characters are called strings.
For exmple in business order form a person enters details like his name,
credit card number address etc which are nothing but strings
** A string is a group of characters.
In C/C++ a string represents an array of characters,where the last charcter
will be ‘\0’ charcter.
In Java, a string is an object of String class.
String s=”Java”;
Here s is a variable of data type ‘String’
IS String a class or data type?
String is a class in java.lang package. But in Java, all classes are also
considered as data types . So we can take String as a data type also.
Three ways to create strings:
1. String s; //declare a string type variable
s=”Hello”;//asssing a group of characters to it.
2. String s=new String(“HEllo”);
3. char arr[]={‘c’,’h’,’a’,’i’,’r’,’s’}
String s=new String(arr);
String class Methods:
1. String concat(String s)
String s3=s1.concat(s2);
2. int length(): This methdo returns the length or number of characters of a
string.
S1.length();
3. char charAt(int i) : this method returns the cahartacter tat te specified
location.
4. int compareTo(String s) : This method is useful to compare two string
and to know which string is bigger or smaller.
What is the difference between String and StringBuffer Classes?
String class objects are immutable and hence their contents cannot be
modified.
StringBuffer class are mutable and so they can be modified.