Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 29 March 2017

Java String

 What is String ?

String in java is a sequence of characters. String is an object in java that represents sequence of characters and enclosed with the double quotes ( " ").

For example : String s = " Java ";

In the above example "Java" is a string with 4 characters.

String class are store in lang package. The java.lang.String class is basically used to create an object of string.

Character array work same as string.

For example : char array[] = {'j','a','v','a'};
                        String s = new String(array);

                        is same as : 
                                                String s = "java";

String class implements Comparable, Serializable, and CharSequence interfaces.


How to create String object

You can create string object in java by two ways, these are:

  1. By string literal
  2. By new keyword.

1) String Literal

You can create java string literal by using double quotes (" ") only.

For example : String s = "Java Programming";

Whenever you will create string object through string literal , It will go into special memory area called "String Constant Pool". Each time we create string literal, JVM checks first the string constant pool. If the string already exists in the pool, a reference to the pooled instance is returned. If string does not exists in the pool, a new string instance is created and placed in the pool.

For example:

String s = "Hello";
String ss = "Hello";//will not create new instance


Java String Constant Pool


In the above example only one object will be created "Hello" because string constant pool does not contains duplicate string object and it will return the reference to the same instance. If there is no duplicate objects then JVM create new objects e.g 

String s = "Hello";//will go into the constant pool
String ss = "Welcome";//will go into the constant pool

Both string objects will go into the constant pool because there is no duplicate objects. 

 


2) By new Keyword

You can create string object by new keyword.

For example: 

String s = new String("Java Program");//creates 2 objects

In the above example there is two object is created, first through new keyword(in heap memory) and second is through literal "Java Program"(will go in string constant pool). but priority goes to heap memory i.e reference variable 's' will refer to heap memory(new keyword).


String Example

In this example, we are creating string object through literal an new keyword.

class StringDemo
{
public static void main(String args[])
{
String s1 = "HelloJava";//Using string literal
String s2 = new String("Java Programming");//Using new keyword
System.out.println(s1);
System.out.println(s2);
}
};

output : HelloJava
              Java Programming

Immutable String in Java

There is two words, Mutable and Immutable

Immutable i.e Unchangeable or Unmodifiable .

Mutable i.e changeable or modifiable 

String is a immutable class in java. Once string object is created, its data or state cannot be changed but a new string object is created because string object is immutable. But StringBuffer and StringBuilder is mutable in java, we will learn in next chapter.

For example :

class ImmutableExample
{
public static void main(String args[])
{
String s = "United";
s.concat("States");//concat() method append string at the end
System.out.println(s);//print United because string is immutable
}
}

output : United

In the above example, Here United is not changed but a new object is created with UnitedStates, that is why string is immutable in java.
Immutable String
In the above diagram, two objects are create but  reference variable s refer to "United" object not "United States" objects because we cannot change string, once it created.


Q. Why String Literal is used in Java ?

Ans. To make memory efficient (because string constant pool does not take duplicate string objects).

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate