- String Handling
- How to create string objects?
- String literal
- new keyword
- Why Java uses the concept of String literal?
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.
String
Generally string is a sequence of characters. But in java, string is an object. String class is used to create string object.
How to create String object?
There are two ways to create String object:
|
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. |
Why java uses concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool). |
2) By 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 JavaIn 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:
Output:Sachin As you can see in the above figure that two objects are created but s reference variable still refers to "Sachin" not to "Sachin Tendulkar". But if we explicitely assign it to the reference variable, it will refer to "Sachin Tendulkar" object.For example:
Output:Sachin Tendulkar Why string objects are immutable in java?
|
String Concatenation in Java
Concating strings form a new string i.e. the combination of multiple strings.
There are two ways to concat string objects:
- By + (string concatenation) operator
- By concat() method
1) By + (string concatenation) operator
String concatenation operator is used to add strings.For Example: |
- //Example of string concatenation operator
- class Simple{
- public static void main(String args[]){
- String s="Sachin"+" Tendulkar";
- System.out.println(s);//Sachin Tendulkar
- }
- }
Output:Sachin Tendulkar
The compiler transforms this to:
- String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();
String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.String concatenation operator produces a new string by appending the second operand onto the end of the first operand.The string concatenation operator can concat not only string but primitive values also.For Example: |
- class Simple{
- public static void main(String args[]){
- String s=50+30+"Sachin"+40+40;
- System.out.println(s);//80Sachin4040
- }
- }
Output:80Sachin4040
Note:If either operand is a string, the resulting operation will be string concatenation. If both operands are numbers, the operator will perform an addition. |
2) By concat() method
concat() method concatenates the specified string to the end of current string. |
Syntax:public String concat(String another){} |
- //<b><i>Example of concat(String) method</i></b>
- class Simple{
- public static void main(String args[]){
- String s1="Sachin ";
- String s2="Tendulkar";
- String s3=s1.concat(s2);
- System.out.println(s3);//Sachin Tendulkar
- }
- }
Output:Sachin Tendulkar
Substring in Java
A part of string is called substring. In other words, substring is a subset of another string.
In case of substring startIndex starts from 0 and endIndex starts from 1 or startIndex is inclusive and endIndex is exclusive.
You can get substring from the given String object by one of the two methods:
- public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
- 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.
In case of string: |
|
Example of java substring
- //Example of substring() method
- class Simple{
- public static void main(String args[]){
- String s="Sachin Tendulkar";
- System.out.println(s.substring(6));//Tendulkar
- System.out.println(s.substring(0,6));//Sachin
- }
- }
Output:Tendulkar Sachin
0 comments:
Post a Comment