java strings

download java strings

of 8

description

java strings class

Transcript of java strings

Slide 1

JAVASTRING CLASSMADE BY- SHIVIKA SINGHIT, 02413503110String() String s=new String();

String(char chars[]) char chars[]={a,b,c}; String s=new String(chars);

String(char chars[],int startIndex,int numChars) char chars[]={a,b,c,d,e,f}; String s=new String(chars,2,3);

String(String strObj) String s=new String(s1);

String(byte asciiChars[]) byte ascii[]={65,66,67,68,69,70}; String s=new String(ascii);

String(byte asciiChars[],int startIndex,int numChars) byte ascii[]={65,66,67,68,69,70}; String s=new String(ascii,2,3);STRING CONSTRUCTORS String litrals

For each string litral java automatically constructs a string object.Thus we can use a string litral any place where we used a string object.

String s2=abc;int i=abc.length();

String concatenation

+ operator concatenates two string producing string object as a result.

String s1=is;String s=this + s1+ java.;

String concatenation with other data types

int age=9;String s=he is + age+ years + old;

int value in age is automatically converted to string representation and is concatenated as before. SPECIAL STRING OPERATIONSNAMEEXAMPLEOUTPUTlength()Return type-int

char chars[]={a,b,c};String s=new String(chars);s.o.p(s.length());

3

equals()Return type-booleanString s1=hello;String s2=Hello;s.o.p(s1.equals(s2));falseequalsIgnoreCase()Return type-boolean

String s1=hello;String s2=Hello;s.o.p(s1.equalsIgnoreCase(s2));

truecharAt()Return type- char

char ch;ch=abc.charAt(1);s.o.p(ch);

b

METHODS IN CLASS STRINGNAMEEXAMPLEOUTPUTindexOf()Return type-intString s=hello;s.o.p(s.indexOf(l);)

2lastIndexOf()Return type-int

String s=hello;s.o.p(s.lastIndexOf(l);)

3substring()Return type-StringString s=hello;String r=s.substring(2);s.o.p(r);

lloconcat()Return type-String

String s=one;String s1=s.concat(two);s.o.p(s1);

onetworeplace()Return type-String

String s=Hello.replace(l,w);s.o.p(s);HewwoNAMEEXAMPLEOUTPUTtrim()Return type-StringString s= hello .trim();s.o.p(s);

hellotoUpperCase()Return type-String

String s=jaVAString upper=s.toUpperCase();s.o.p(upper);JAVAtoLowerCase()Return type-String

String s=jaVAString lower=s.toLowerCase();s.o.p(lower);

javaendsWith()Return type-booleans.o.p(learning strings.endsWith(ings));

TruestartsWith()Return type-boolean

s.o.p(learning strings.startsWith(learn));

TrueStrings are immutable (can't be changed), so manipulating strings often causes many temporary string objects to be created, which can be quite inefficient.

TheStringBufferandStringBuilderclasses are used when there is a necessity to make alot of modifications to Strings of characters.

Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again with out leaving behind alot of new unused objects.

The StringBuilder class was introduced as of Java 5 and the main differencebetween the StringBuffer and StringBuilder is that StringBuilders methods are notthread safe(not Synchronised).

It is recommended to useStringBuilderwhenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.

STRING BUILDERDifference between String and StringBuffer/StringBuilder in Java

The most important difference between String and StringBuffer/StringBuilder in java is thatString object is immutable whereasStringBuffer/StringBuilder objects are mutable.

In String class its not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.Since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object.

StringBuffer and StringBuilder have the same methods with one difference and thats of synchronization.

StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isnt thread safe).

So, if you arent going to use threading then use theStringBuilderclass as itll be moreefficientthanStringBufferdue to theabsenceof synchronization.