Print This Article Post Comment Add To Favorites Email to Friends Ezine Ready

String Class In Java To Provide The Immutability

By: Infocampus Home | Computers-and-Technology



In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.

A Java string is therefore a series of characters collected together, like the word "Java", or the phrase "practice makes perfect". Create a string in the code by writing its chars out between double quotes. String str = "Java"; This picture shows the string object in memory, made up of the individual chars J a v a.

Java String is class that java vendors have written and put into java API . It is present in java.lang package.best core java training in bangalore It is a immutable class,means once we created its object we can not modify that,if we will try to modify new object will be created.
Why string is immutable?

Performance: By making Strings immutable it is possible to cache them more aggressively

Security: Parameters are typically represented as string in network connections, database connection urls, usernames/passwords etc. If it were mutable, these parameters could be easily changed.

Synchronization and concurrency: It make String immutable automatically makes them thread safe thereby solving the synchronization issues.

Caching: When compiler optimizes String objects, it sees that if two objects have same value (a="test", and b="test") and thus you need only one string object (for both a and b, these two will point to the same object).java/j2ee classes bangalore

Class loading: String is used as arguments for class loading. If mutable, it could result in wrong class being loaded (because mutable objects change their state).

How string is immutable?
Strings are considered immutable in Java because when we assign new value to a string then the new value is not allocated at the same address space in memory where the older value was stored.

Instead, the old value is discarded and the space it took in the memory is cleaned up by the JVM's garbage collector and new space is allocated in the memory where new value of the string is stored.

JDK provides two classes to support mutable strings: StringBuffer and StringBuilder.java course in bangalore

String Buffer

A StringBuffer or StringBuilder object is just like any ordinary object, which are stored in the heap.
StringBuffer class is used to create a mutable string object. It represents growable and writable character sequence. As we know that String objects are immutable, so if we do a lot of changes with String objects, we will end up with a lot of memory leak. So StringBuffer class is used when we have to make lot of modifications to our string. It is also thread safe i.e multiple threads cannot access it simultaneously.
String Builder

StringBuilder is identical to StringBuffer except for one important difference it is not synchronized, which means it is not thread safe.java training center bangalore Its because StringBuilder methods are not synchronised.
In other words, if multiple threads are accessing a StringBuilder instance at the same time, its integrity cannot be guaranteed. However, for a single thread program (most commonly), doing away with the overhead of synchronization makes the StringBuilder faster.

Difference between string , string buffer and string builder:
- String is immutable whereas StringBuffer and StringBuider are mutable classes.
- StringBuffer is thread safe and synchronized whereas StringBuilder is not, thats why StringBuilder is more faster than StringBuffer.
- String concat ,+ operator internally uses StringBuffer or StringBuilder class.
- For String manipulations in non-multi threaded environment, we should use Advance java training in bangalore StringBuilder else use StringBuffer class.
This is the short description about the String class.To get the more knowledge and to get the job as java developer easily join Infocampus Software Training Institute.



Article Source: http://www.eArticlesOnline.com

About the Author:
Infocampus provides best core java training in bangalore with the 100% job assistance.Attend 4 days free demo classes for java course .Learn OOPs concept,packages,thread,spring,hibernate etc in Core and Advanced Java Institute .Training is given by the 10+ years experienced trainers in java training center .Infocampus provides working on real time project.Pay fees in installaments for java/j2ee classes .Complete practical session is available for Advance Java Training.To get the free demo classes and for the 100% placement assistance contact at : 9738001024 or enquire at : http://www.infocampus.co.in/java-training-bangalore.html

Tags: , , , ,

Please Rate this Article

 

Not yet Rated

Click the XML Icon Above to Receive Computers-and-Technology Articles Via RSS!

Recent Related Articles From Computers-and-Technology

  • Common Wordpress Issues | Fix Wordpress Site
    By: 24/7 wordpress support | wordpress website repair | Oct 17th 2017
    We’re going to talk briefly about security and WordPress error, concepts to help you keep your site safe. Read

  • Brief Description About Applet
    By: Infocampus | Oct 16th 2017
    Applet is an extraordinary kind of program that is introduced in the site page to create the dynamic substance. It continues running inside the program and works at client side. Read

  • New Jersey It Support- Why Help Desk Is Highly Essential For Every Business
    By: Vikram Kumar | Oct 14th 2017
    As one of the online business owners, you have perhaps heard the term- Help Desk. This is such an asset that the entrepreneurs consider for the convenience of users Read

  • It Support For Nj Businesses - Why Should You Have Server Virtualization Solutions
    By: Vikram Kumar | Oct 14th 2017
    When your business has presently a traditional server system, it may be able to run only one application. Lots of companies, having this system, need to make a deployment of a number of servers Read

  • Oops Concept Forms The Basis Of Java Programming Language
    By: Infocampus | Oct 13th 2017

    OOPs concept forms the basis of Java Programming Language, I will try to answer your question in a very layman language.
    OOPs stands for Object Oriented Programming. An approach to do programming which is based on Objects (or real life objects).
    Read

  • Sending Off Harmful Adware & Spyware- Sakshi Computers
    By: Sakshi | Oct 13th 2017
    The Internet is a wonderful tool. It works like a virtual web, connecting everyone within its network from all parts of the globe. With numerous striking highlights, the Internet has turned out to be popular to the point that for all intents and purposes everybody around the globe approaches an Internet association. This po ... Read

  • Anti-virus Support New Jersey - To Protect A Computer System
    By: Vikram Kumar | Oct 13th 2017
    The present online world has allowed lots of companies to alter their business-related approaches. However, any file that you have stored on the web platform may face the risk of the virus Read

  • Offsite Data Backup Strategy In New Jersey- Protect The Lost Files
    By: Vikram Kumar | Oct 13th 2017
    An efficient management of business data is highly important to every company. As the business owners do their transactions with the help of computer systems, they have to ensure the security of all data

    Read

  • Introduction Of Java Jdbc
    By: Infocampus | Oct 12th 2017
    Before JDBC, ODBC API was the database API to relate and execute question with the database. In any case, ODBC API uses ODBC driver which is created in C lingo (i.e. organize destitute and unsecured). That is the reason Java has described its own particular API (JDBC API) that usages JDBC drivers (written in Java tongue). Read

  • Why All Businesses Have Started Depending On Computer & It Support Nj
    By: Vikram Kumar | Oct 12th 2017
    Every business owner wants to increase revenue with smarter and better innovations. Presently, information technology has caused a revolution in almost all the business sectors

    Read


Copyright © 2005-2015 eArticlesOnline, LLC - All Rights Reserved
Terms of Service | Privacy Policy