JAVA DAY 4
JAVA DAY 4
7.String
CharSequence Interface
The CharSequence interface is used to represent the sequence of
characters.
Example Explanation
Only one object will be created. Firstly, JVM will not find any
string object with the value "Welcome" in the 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.
Uses
● To make more memory efficiently
new keyword
Note:
While using new keyword JVM will create a new string object in
normal (non-pool) heap memory, and the literal "Welcome" will be
placed in the string constant pool. The variable s will refer to the
object in a heap (non-pool).
Example
public class StringExample
{
public static void main(String args[])
{
String s1="java";// Java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
● Comparison methods
equals() Method
The String class equals() method compares the original content of the
string. It compares values of string for equality. String class provides
the following two methods:
Example
class Teststringcomparison1
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
String s4="Saurav";
String s5="Java";
String s6="JAVA";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
System.out.println(s5.equals(s6));//false
System.out.println(s5.equalsIgnoreCase(s6));//true
}
}
Using == operator
Example
class Teststringcomparison3
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same inst
ance)
System.out.println(s1==s3);//false(because s3 refers to instance c
reated in nonpool)
}
}
compareTo() method
Example
class Teststringcomparison4{
public static void main(String args[]){
String s1="Java";
String s2="Java";
String s3="Programming";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
● String Methods
//substring
String s4="java is simple";
System.out.println(s4.substring(2));
System.out.println(s4.substring(0, 1));
System.out.println(s4.substring(0, 2));
System.out.println(s4.substring(0, 3));
System.out.println(s4.substring(1, 7));
//trim()
String s7=" hello ";
System.out.println(s7.trim());
System.out.println(s7);
//char at
String s11=" morning ";
String s12="Hello";
System.out.println(s11.charAt(4));
System.out.println(s12.charAt(1));
//length()
String s13=" hello ";
String s14="hello";
System.out.println(s13.length());
System.out.println(s14.length());
//value of
int a=10,b=20;
System.out.println(a+b);
String s=String.valueOf(a);
System.out.println(s+10);
//replace of
String s15="Java is a programming language.";
String rep=s15.replace("Java", "c");
System.out.println(rep);
//indexof
String s6="java is object oriented";
System.out.println(s6.indexOf('i'));
System.out.println(s6.lastIndexOf('i'));
//contains
String z="umbrella";
System.out.println(z.contains("rel"));
//intern
String s = new String("Hello World");
String s1 = new String("Hello World");
System.out.println(s1 == s); // prints false
String s2 = new String("Welcome to JavaTpoint").intern();
String s3 = new String("Welcome to JavaTpoint").intern();
System.out.println(s3 == s2); // prints true
}
}
StringBuffer Class
Example
class StringBufferExample
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Hello ");
//append
sb.append("Java");//now original string is changed
System.out.println(sb);
//insert
sb.insert(1,"Java");
System.out.println(sb);
//replace
sb.replace(1,3,"Java");
System.out.println(sb);
//delete
sb.delete(1,3);
System.out.println(sb);
//reverse
sb.reverse();
System.out.println(sb);
//capacity
}
}
StringBuilder Class
Example
class StringBuilderExample
{
public static void main(String args[])
{
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);
sb.insert(1,"Java");
System.out.println(sb);
sb.replace(1,3,"Java");
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb);
sb.reverse();
System.out.println(sb);
}
}
Difference between String and StringBuffer
StringBuffer StringBuilder
StringBuffer is synchronised i.e. StringBuilder
thread safe. It means two threads is non-synchronized i.e. not thread
can't call the methods of safe. It means two threads can call
StringBuffer simultaneously the methods of StringBuilder
simultaneously.
StringBuffer is less efficient than StringBuilder is more efficient than
StringBuilder. StringBuffer.
StringBuffer was introduced in StringBuilder was introduced in
Java 1.0 Java 1.5