Java String
Java String
In java string is basically an object that represents sequence of char values. An array
of characters works same as Java string. For example:
String s=”hksdjfhdskg”;
Interview question:
What is string?
Object
Program:
System.out.println(s1);//
Example:
char[] c= {'a','b','c','d','e'};
String s=new String(c);
System.out.println(s);
Example:
String s1=”welcome”;
String s2=”welcome”;
Each time you create a string literal, the JVM checks the "string constant pool" first. If
the string already exists in the pool, a reference to the pooled instance is returned. If
the string doesn't exist in the pool, a new string instance is created and placed in the
pool.
In the above example, only one object will be created. Firstly, JVM will not find any
string object with the value "Welcome" in string constant pool that is why it will
create a new object. After that it will find the string with the value "Welcome" in the
pool, it will not create a new object but will return the reference to the same
instance.
Program:
Equals() Method:
Program:
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
equalsIgnoreCase:
Program:
String s1="Sachin";
String s2="SACHIN";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true
The String class compareTo() method compares values lexicographically and returns
an integer value that describes if first string is less than, equal to or greater than
second string.
Program:
How it works is it ASCII based?
Substring in Java:
A part of String is called substring.
1. public String substring(int startIndex):
This method returns new String object containing the substring of the given
string from specified startIndex (inclusive). The method throws an
IndexOutOfBoundException when the startIndex is larger than the length of
String or less than zero.
2. public String substring(int startIndex, int endIndex):
This method returns new String object containing the substring of the given
string from specified startIndex to endIndex. The method throws an
IndexOutOfBoundException when the startIndex is less than zero or startIndex
is greater than endIndex or endIndex is greater than length of String.
3. startIndex: inclusive
4. endIndex: exclusive
Program:
String s="hello";
System.out.println(s.substring(0,2)); //returns he as a substring
Program:
public static void main(String args[]){
String s="SachinTendulkar";
System.out.println("Original String: " + s);
System.out.println("Substring starting from index 6: "
+s.substring(6));//Tendulkar
System.out.println("Substring starting from index 0 to 6: "+s.substring(0,6));
//Sachin
}
//System.out.println(s1[0]);
//System.out.println(s1[1]);
String [] s3=s1.split(" ");
//System.out.println(s3[0]);
//System.out.println(s3[1]);
//System.out.println(s3[2]);
//System.out.println(s3[3]);
//System.out.println(s3[4]);
System.out.println(s3[5]);
}
Java String toUpperCase() and toLowerCase() method:
}
StringBuffer:
Java StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it can be
changed.
Program:
public static void main(String ar[])
{
StringBuffer s=new StringBuffer("Hello");
s.append(" World");
System.out.println(s);