Sei sulla pagina 1di 8

JAVA

STRING CLASS
MADE BY-
SHIVIKA SINGH
IT, 02413503110
String()
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
OPERATIONS
NAME EXAMPLE OUTPUT
length()
Return type-int

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

3

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

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

true
charAt()
Return type- char

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

b

METHODS IN CLASS STRING
NAME EXAMPLE OUTPUT
indexOf()
Return type-int
String s=hello;
s.o.p(s.indexOf(l);)

2
lastIndexOf()
Return type-int

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

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

llo
concat()
Return type-String

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

onetwo
replace()
Return type-String

String s=Hello.replace(l,w);
s.o.p(s);
Hewwo
NAME EXAMPLE OUTPUT
trim()
Return type-String
String s= hello .trim();
s.o.p(s);

hello
toUpperCase()
Return type-String

String s=jaVA
String upper=s.toUpperCase();
s.o.p(upper);
JAVA
toLowerCase()
Return type-String


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

java
endsWith()
Return type-boolean
s.o.p(learning
strings.endsWith(ings));

True
startsWith()
Return type-boolean

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

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

The StringBuffer and StringBuilder classes 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 difference
between the StringBuffer and StringBuilder is that StringBuilders methods are
not
thread safe(not Synchronised).

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


STRING BUILDER
Difference between String and
StringBuffer/StringBuilder in Java

The most important difference between String and StringBuffer/StringBuilder
in java is that String object is immutable
whereas StringBuffer/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 the StringBuilder class as itll
be more efficient than StringBuffer due to the absence of
synchronization.

Potrebbero piacerti anche