string is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
- char[] ch={‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’e’,’r’};
- String s=new String(ch); ssame as:
- String s=”progemar”;
- Java String class gives a lot of way(method) s to apply missions on string such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring()
- The lang.String class applies Serializable, Comparable and CharSequence borderlines.CharSequence Borderline
The CharSequence borderline is used to represent sequence of characters. It is applied by String, String Buffer and String Designer classes. It means, we can produce string in java by using these 3 classes.
The java String is can be change i.e. it cannot be changed. Whenever we change any string, a new instance is produced. For can be change string, you can use StringBuffer and StringDesigner classes.
There are two way(method) s to produce String object:
- By string literal
- By new keyword
String Literal
Java String literal is produced by using double quotes. For Example:
- String s=”welcome”;
Each time you produce a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a footnote to the pooled instance is returned. If string doesn’t exist in the pool, a new string instance is produced and placed in the pool. For example:
- String s1=”Welcome to progemarcoding.com”;
- String s2=”Welcome to progemarcoding.com “;//will not produce new instance
By new keyword
- String s=new String(“Welcome to progemarcoding.com “);//produces two objects and one footnote variable
In such case, JVM will produce 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 heap (non pool).
Java String Example
public class StringExample{
public static void main(String args[]){
String s1=”java”;//creating string by java string literal
char ch[]={‘s’,’t’,’r’,’i’,’n’,’g’,’s’};
String s2=new String(ch);//converting char array to string
String s3=new String(“example”);//creating java string by new keyword System.out.println(s1);
System.out.println(s2); System.out.println(s3);
}}
OUTPUT
strings
example
Can be change String in Java
In java, string objects are can be change. Can be change simply means unmodifiable or unchangeable. Once string object is produced its data or state can’t be changed but a new string object is produced. Let’s try to understand the cann’t be changeabliy concept by the example given below:
class Testcan be changestring{
public static void main(String args[]){ String s=”sanjan”;
s.concat(” pandit”);//concat() way(method) Â appends the string at the end System.out.println(s);//will print Sachin because strings are can be change objects
} }
Output:Sachin
class Testcan be changestring1{
public static void main(String args[]){ String s=”sanjan”;
s=s.concat(” pandit”); System.out.println(s);
} }
Output:
sanjan pindit