Professional Documents
Culture Documents
String
String
n object.
(2)String class is used to create string object.
(3)String object are immutable(not changable)if ane reference variables
changes
the value of the object , it will be affected to all the reference v
ariables.
*how to create String object->
->there are two ways to create string object
1->By string literal
2->By new keyword
1->String literal->String literal is created by double quotes.
String s1 = "hello";
String s2 = "welcome";
why java use concept of String literal->
to make java memory effecient(because no new objects are created
if it exists already in String contant pool )
2->By new keyword->
String s = new String("welcome");
ex.1->public class Simple {
public static void main(String[] args) {
String s = "sachin";
s.concat("tendulkar");
System.out.println(s); // sachin
}
}
ex.2->
public class Simple {
public static void main(String[] args) {
String s = "sachin";
s = s.concat("tendulkar");
System.out.println(s); //sachintendulkar
}
}
*String comparison in java->
there are three ways to compare String objects
(1) By equals() method
(2) By == operator
(3) By compareTo() method
(1) By equals() method->(1)this method compare the original content of String.
(2)it compares value of String for equality
(3)String class provide two methods
(a) equals(Object another){}
(b) equlasIgnoreCase(String another){}
|->compare this String to anothr String ignoring
case.
(2)By == operator->this operator compare references not value
(3)By compareTo() method-> this method compares values and returns int.
s1==s2
s1>s2
s1<s2
: 0
: positive value
: negative value
ex->
public class Demo1
{
public static void main(String[] args)
{
String a="ram";
String b="ram";
String s1="sita";
String c=new String("ram");
String d=new String("ram");
String d1 = new String("bharat");
System.out.println(a==b);
System.out.println(c==d);
System.out.println(a==c);
System.out.println(a==s1);
System.out.println(a==d1);
System.out.println(d==d1);
System.out.println(a.compareTo(b));
System.out.println(c.compareTo(d));
System.out.println(a.compareTo(d));
System.out.println(c.compareTo(d));
System.out.println(a.compareTo(s1));
System.out.println(a.compareTo(d1));
System.out.println(d.compareTo(d1));
System.out.println(c.equals(d));
System.out.println(a.equals(c));
System.out.println(c.equals(d));
System.out.println(a.equals(b));
System.out.println(a.equals(s1));
System.out.println(a.equals(d1));
System.out.println(d.equals(d1));
}
}
************method of String class***********
charAt(int index)->Returns the char value at the specified index.
compareTo(String anotherString)->Compares two strings lexicographically.
compareToIgnoreCase(String str)->Compares two strings lexicographically, ignorin
g case differences.
concat(String str)->Concatenates the specified string to the end of this string.
contains(CharSequence s)->Returns true if and only if this string contains the s
pecified sequence of char values.
.
substring(int beginIndex)->Returns a new string that is a substring of this stri
ng.
substring(int beginIndex, int endIndex)->Returns a new string that is a substrin
g of this string.
toCharArray()->Converts this string to a new character array.
toLowerCase()->Converts all of the characters in this String to lower case using
the rules of the default locale.
toUpperCase()->Converts all of the characters in this String to upper case using
the rules of the default locale.
String toString()->This object (which is already a string!) is itself returned.
*StringBuffer class->StringBuffer class is used to create mutable(midifiable) St
ring. so we can modified
String during execution.
note->StringBuffer class is thread safe i.e multiple thread can not access it si
mulaneously
so it is thred safe and will result in order.
but String is mubtable it can be safely be shared b/w many threads which i
s very important
for multithreading.
ex->
public class A {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("welcome");
sb1.append("java");
System.ot.println(sb1) // now original string is changed o/
p->hellojava
sb2.insert(1,"my");
System.out.println(sb2)// wmyelcome
}
}
->diff b/w StringBuffer and StringBuilder
(1) StringBuffer and StringBuilder are used to create mutable String.
(2) StringBuffer is synchronized whereas StringBuilder is not synchronized.
(3) StringBuilder is faster than StringBuffer.
*StringTokenizer->it is a utility class used to create break up string.this clas
s comes from
"java.util" package.
ex-> StringTokenizer st = new StringTokenizer("this is my india");
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
o/p-> this
is
my
india