Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Saturday, 10 October 2015

Why String is immutable in Java

 Earthcare Foundation NGO     10:55     Java, Java Interview Questions     No comments   

This question can be answered by knowing the benefits of immutable classes. So, i will try to explain about immutable classes that will explicitly explain why strings are immutable in Java.
Immutable classes are those classes that cannot be modified after creation. If we want to modify immutable class then it will result in new immutable object.Immutable objects are very important in concurrency and they makes the programs efficient.
First we will see how to create an immutable object.As we know there are following step by which any object can be made as immutable:

1) Don't provide mutator methods for any field.
2) Make all the fields final and private
3) Don't allow subclasses by declaring the class final
4) Return deep cloned objects with copied content for all mutable fields in class
So we will apply these steps to create a immutable object.

public final class ImmutableClass{
private final String name;
private final String designation;
public ImmutableClass(String name,String designation){
this.name=name;
this.designation=designation;
}
public String getName(){
return name;
}
public String getDesignation(){
return designation;
}
}

So above is an example of an immutable class.
But there can be a case when we have to include an mutable class(such as Date) within an immutable class, Then what can we do? We will make it as final member but it can be modified internally. So in this case the best practise is to return the copy of that object instead of original object. FOr this we can clone that object and return it.
For Example

public final ImmutableClassWithMutableObject{
private final Date date;
public ImmutableClassWithMutableObject(Date date){
this.date=new Date(date.getTime());
}
public Date getDate(){
return (Date)date.clone();
}
}
So above is a program of immutable class when it contains a mutable class.

Advantages of immutable class:
1)They are thread safe so no need for synchronization
2)Immutable objects can be shared among threads without synchronization
3)Due to no synchronization performance of the application increases
4)They can be stored in cache and reused as Immutable objects Strings.

Disadvantage:
There is a huge disadvantage of creating garbage.Since immutable objects are use and throw so they create a huge garbage which reduces the performance of the application.

So above is all about immutable object.
So, now i think that everyone has understood why String is immutable in Java.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ▼  October (6)
      • Top 5 free and best blogging platforms
      • WordPress:How to create a free blog in 15 minutes
      • Great Topics for a Blog
      • PL/SQL Tutorial
      • Why String is immutable in Java
      • Java program to print patterns
    • ►  May (10)
    • ►  March (20)
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates