Strings
-> String is a pre-defined class available in [Link] package
-> String we can use as a data type also (Referenced Data Type)
Note: Every java class can be used as a referenced data type
-> String is used to store group of characters
Ex : String name = "abc" ;
-> String is immutable in java ( can't be modified )
-> We can create String object in 2 ways
// approach - 1 (string literal)
String name = "flm";
// approach -2 (using new operator)
String str = new String ("flm");
String Constant Pool
-> It is special memory in JVM to store String objects
-> It will not allow us to create duplicate objects
String s1 = "hi" ;
String s2 = "hi" ;
-> s1 and s2 objects are having same content hence only one object will be created and two
variables will be pointed to same object.
class StringDemo {
public static void main(String[ ] args) {
String s1 = "hi" ;
String s2 = "hi" ;
if ( s1 == s2 ) {
[Link] (" Both are same ");
}else {
[Link](" Both are not same" );
}
=> If we create String objects using 'new' operator always new object will be created in Heap area.
class StringDemo {
public static void main(String[ ] args) {
String s1 = new String ("hello") ;
String s2 = new String ("hello") ;
if ( s1 == s2 ) {
[Link] (" Both are same ");
}else {
[Link](" Both are not same" );
Note: In Strings == will compare address of the objects not content.
Q) How many objects will be created ?
String s1 = "flm" ; // 1 obj
String s2 = "flm"; // 1 obj
String s3 = new String("flm"); // 2 objs
String s4 = new String("flm"); // 3 objs
String s5 = new String("hello"); // 5 objs
String s6 = new String("hi"); // 7 objs
s1 == s2 =====> true
s2 == s3 =====> false
s3 == s4 =====> false
s5 == s6 ====> false
String class Methods
charAt ( ) => To get a character based on given index
String s1 = "flm";
[Link]([Link](0));
length ( ) => To get size of string ( [Link] characters available in String )
String s1 = "flm";
[Link]([Link]( ) );
concat ( ) => To join two strings (appending)
String s1 = "Frontlines";
String s2 = "Edutech;
String s3 = [Link](s2);
// String s4 = s1 + s2 ;
[Link](s3);
equals ( ) => To compare content of two Strings
String s1 = "hi";
String s2 = "hello";
[Link]( [Link](s2) );
Note: In Strings, == operator will compare address of string objects where as 'equals( ) ' method will
compare content of the objects.
replace ( ) => To repalce chars with another chars
String s1 = "hyderabad";
String s2 = [Link]("bad", "good");
[Link](s2);
toUpperCase ( ) => To convert string to uppercase string
[Link]( ) ;
toLowerCase ( ) => To convert String to lowercase String
[Link] ( ) ;
indexOf ( ) => To get first occurance of char
[Link] ( [Link]('a') ) ;
lastIndexOf ( ) => to get last occurance of char
[Link] ( [Link]('a') ) ;
Note: If given char is not available then it will return '-1'
substring ( ) => It is used to get some part of the string. It will take start index & end index.
start-index : inclusive
end-index : exclusive
[Link]( [Link](0,5)
Note: If we don't give end index, it will print from start index to last index.
split ( ) => It is used to split the string based on delimiter (seperator)
String s2 = "hi@hello@how are@you";
String [ ] arr = [Link] ("@");
[Link]([Link](arr));
valueOf ( ) -> It is used convert any type value into String type
int a = 10 ;
int b = 20 ;
a + b ===> 30
String s1 = "10";
String s2 = "20";
s1 + s2 ==> "1020"
[Link] (a) + [Link] (b) ==> 1020
"10" + "20" ==> 1020
Note: valueOf ( ) is a static method in String class. Static methods will be called using classname.
Non-Static methods will be called using Object.
startsWith ( ) => It is used to check given String is starting with particular char(s) or not
String str = "flm";
[Link]("f") ; =====> true
[Link]("z") ; =====> false
endsWith ( ) => It is used to check given String is ending particular char(s) or not
String str = "flm";
[Link]("lm"); ===> true
[Link]("good") ===> false
trim ( ) ==> It is used to remove starting and ending spaces of String
String str = " hello ";
[Link] ( );
intern ( ) => It is used to access the object from scp
String s1 = "hi";
String s2 = [Link] ( );
s1 == s2 ===> true
toString ( ) => It is used to convert object into string format.
toCharArray ( ) : This method is used to convert String to char array
String s1 = "java";
char arr[ ] = [Link] ( );
===========================================
class StringReverse {
public static void main(String[ ] args){
String s = "java";
String rev = "";
for( int i = [Link]( ) - 1 ; i >=0 ; i-- ){
rev = rev + [Link] ( i ) ; //avaj
}
}
----------------------------------------------------------
class StringReverse {
public static void main(String[ ] args){
String s = args[0];
String rev = "";
for( int i = [Link]( ) - 1 ; i >=0 ; i-- ){
rev = rev + [Link] ( i ) ;
[Link](rev);
-----------------------------------------------------------------------------
class Palindrome {
public static void main(String[ ] args){
String s = args [0] ;
String s1 = "";
for(int i = [Link] ( ) -1 ; i >=0 ; i -- ){
s1 = s1 + [Link] (i);
}
if( [Link](s1) ){
[Link]("Palindrome");
}else{
[Link]("Not Palindrome");
}
------------------------------------------------------------------------
class StringReverse {
public static void main(String[ ] args){
String s = "java";
String rev = "";
for( int i = [Link]( ) - 1 ; i >=0 ; i-- ){
rev = rev + [Link] ( i ) ; //avaj
}
}
}
----------------------------------------------------------
class StringReverse {
public static void main(String[ ] args){
String s = args[0];
String rev = "";
for( int i = [Link]( ) - 1 ; i >=0 ; i-- ){
rev = rev + [Link] ( i ) ;
}
[Link](rev);
-----------------------------------------------------------------------------
class Palindrome {
public static void main(String[ ] args){
String s = args [0] ;
String s1 = "";
for(int i = [Link] ( ) -1 ; i >=0 ; i -- ){
s1 = s1 + [Link] (i);
if( [Link](s1) ){
[Link]("Palindrome");
}else{
[Link]("Not Palindrome");
}
}
}