Skip to main content

Why String is Immutable in java ?

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:
By string literal
By new keyword


String literal is created by double quote.For Example:
String s="Hello";

String s1="Hello sir";
String s2="Hello sir";


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.

 Note: String objects are stored in a special memory area known as string constant pool inside the Heap memory.

 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).


class Example{
 public static void main(String args[]){
   String s="Hello";
   s.concat(" Sir");//concat() method appends the string at the end
   System.out.println(s);//will print Hello because strings are immutable objects
 }
}

Why string objects are immutable in java?

Because 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. 

Comments

Popular posts from this blog

Find 2nd Largest Number in java ?

import java.util.Scanner; public class SocondLargest {     public static void main(String[] args) {         int secondlargest = Integer.MIN_VALUE;         int largest = Integer.MIN_VALUE;         System.out.println(largest);         Scanner input = new Scanner(System.in);         System.out.println("Enter array values: ");         int arr[] = new int[5];         for (int i = 0; i < arr.length; i++) {             arr[i] = input.nextInt();             if (largest < arr[i]) {                 secondlargest = largest;                 largest = arr[i];  ...

Check the number is Armstrong or not ??

import java.util.Scanner; public class Armstrong { public static void main(String[] args) { System.out.println("Enter the number ..."); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int temp=n; int sum=0 ; while(n>0){  int d=n%10;  sum=sum+d*d*d;  n=n/10; } if(temp==sum){ System.out.println("armstrong"); } else{ System.out.println("not armstrong"); }       sc.close(); } }

Custom exceptional Handling in java

class InvalidAgeException extends Exception { private static final long serialVersionUID = 1L; InvalidAgeException(String s) { super(s); } } public class Custom { public static void age(int age) throws InvalidAgeException { if (age < 18) throw new InvalidAgeException("not valid"); else System.out.println("welcom to vote"); } public static void main(String[] args) { try { age(19); } catch (Exception ex) { System.out.println("exception occured " + ex); } } }