Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

*String->(1)generally String is a sequence of characters but in java,String is a

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.

endsWith(String suffix)->Tests if this string ends with the specified suffix.


equals(Object anObject)->Compares this string to the specified object.
equalsIgnoreCase(String anotherString)->Compares this String to another String,
ignoring case considerations.
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)->Copies characters
from this string into the destination character array.
hashCode()->Returns a hash code for this string.
indexOf(int ch)->Returns the index within this string of the first occurrence of
the specified character.
indexOf(int ch, int fromIndex)->Returns the index within this string of the firs
t occurrence of the specified character, starting the search at the specified in
dex.
indexOf(String str)->Returns the index within this string of the first occurrenc
e of the specified substring.
indexOf(String str, int fromIndex)->Returns the index within this string of the
first occurrence of the specified substring, starting at the specified index.
isEmpty()->Returns true if, and only if, length() is 0.
lastIndexOf(int ch)->Returns the index within this string of the last occurrence
of the specified character.
lastIndexOf(int ch, int fromIndex)->Returns the index within this string of the
last occurrence of the specified character, searching backward starting at the s
pecified index.
lastIndexOf(String str)->Returns the index within this string of the last occurr
ence of the specified substring.
lastIndexOf(String str, int fromIndex)->Returns the index within this string of
the last occurrence of the specified substring, searching backward starting at t
he specified index.
length()->Returns the length of this string.
replace(char oldChar, char newChar)->Returns a new string resulting from replaci
ng all occurrences of oldChar in this string with newChar.
replace(CharSequence target, CharSequence replacement)->Replaces each substring
of this string that matches the literal target sequence with the specified liter
al replacement sequence.
replaceAll(String regex, String replacement)->Replaces each substring of this st
ring that matches the given regular expression with the given replacement.
split(String regex)->Splits this string around matches of the given regular expr
ession.
startsWith(String prefix)->Tests if this string starts with the specified prefix

.
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

You might also like