Strings

Strings Handling
String Handling provides a lot of concepts that can be performed on a string such as concatenating string, comparing string, substring etc.
In java, string is basically an immutable object. We will discuss about immutable string later. Let's first understand what is string and how we can create the string object.
Generally string is a sequence of characters. But in java, string is an object. String class is used to create string object. There are two ways to create String object:
1. string literal
2. new keyword
1 string literal: - String literal is created by double quote.For Example:
String s="Hello";
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 returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.For example:
String s1="Welcome";
String s2="Welcome";//no new object will be created

In the above example only one object will be created.First time JVM will find no string object with the name "Welcome" in string constant pool,so it will create a new object.Second time it will find the string with the name "Welcome" in string constant pool,so it will not create new object whether will return the reference to the same instance.
Note: String objects are stored in a special memory area known as string constant pool inside the Heap memory.
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
2. new keyword: -
String s=new String("Welcome");//creates two objects and one reference variable.
In such case, JVM will create a new String object in normal(nonpool) Heap memory and the literal "Welcome" will be placed in the string constant pool.The variable s will refer to the object in Heap(nonpool).
Immutable String
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
Once string object is created its data or state can't be changed but a new string object is created.
Let's try to understand the immutability concept by the example given below:
class Simple{
public static void main(String args[]){
String s="Anuj";
s.concat(" Bhargava");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
Output: Anuj But if we explicitely assign it to the reference variable, it will refer to "Anuj Bhargava" object.For example:
class Simple{
public static void main(String args[]){
String s="Anuj";
s=s.concat(" Bhargava");
System.out.println(s);
}
}
Output:- Anuj Bhargava
In such case, s points to the "Anuj Bhargava ". Please notice that still Anuj object is not modified.
Java uses the concept of string literal.Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.
String Comparison
We can compare two given strings on the basis of content and reference: -
It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.
There are three ways to compare String objects:
1. By equals() method
2. By = = operator
3. By compareTo() method
1. By equals() method
equals() method compares the original content of the string.It compares values of string for equality.String class provides two methods:
* public boolean equals(Object another){} compares this string to the specified object.
* public boolean equalsIgnoreCase(String another){} compares this String to another String, ignoring case.
class Simple{
public static void main(String args[]){
String s1="Anuj";
String s2="ANUJ";
String s3=new String("Anuj");
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s3));//true
}
}
C:\java>javac simple.java
C:\java>java Simple
false
true
2. By = = operator
The = = operator compares references not values.
class Simple{
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 instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
C:\java>javac simple.java
C:\java>java Simple
true
false
3. By compareTo() method:
compareTo() method compares values and returns an int which tells if the values compare less than, equal, or greater than. Suppose s1 and s2 are two string variables.If:
1. s1 == s2 :0
2. s1 > s2 :positive value
3. s1 < s2 :negative value
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
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 )
}
}
Output: 0
1
-1
String concat() Method
This method appends one String to the end of another. The method returns a String with the value of the String passed in to the method appended to the end of the String used to invoke this method.
There are two ways to concat string objects:
* By + (string concatenation) operator
* By concat() method
1 By + (string concatenation) operator
class Simple{
public static void main(String args[]){
String s="Anuj"+" Bhargava";
System.out.println(s);
}
}
C:\java>javac simple.java
C:\java>java Simple
Anuj Bhargava
2. By concat() method
concat() method concatenates the specified string to the end of current string. Syntax:public String concat(String another){}.
class Simple{
public static void main(String args[]){
String s1="Anuj ";
String s2="Bhargava";
String s3=s1.concat(s2);
System.out.println(s3);
}
}
C:\java>javac simple.java
C:\java>java Simple
Anuj Bhargava


Free Web Hosting