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

  • Federal Cyber Security And Mobility In Execution
    By: Megan Darcy | Jun 26th 2017
    As the Federal Government is growing bigger and bigger, the proneness to cyber attack of its agency systems are growing geometrically. Read

  • Check Out The Guide To Pdf Emailing Software Tool
    By: Jeremy Brown | Jun 22nd 2017
    Printing invoices, sorting them and mailing them †isn’t it a boring job, taking off too much time from productivity? Read

  • Social Media And Business
    By: HiCinko | Jun 20th 2017
    In today’s world, as we can see that technology is rising at a very rapid pace and has certainly taken hold of everyone and everything in the society in a fairly strong manner. And as we can see around ourselves, and for that we don’t need any kind of surveys or any kind of statistics, that how social media has been an ... Read

  • Benefits Of Buying Ipad Repair Parts From Wholesale Seller In Online
    By: JESSICA SMITH | Jun 16th 2017
    This article is about all cell phone repair parts.This article will tell you the benefits of buying repair parts of mobile phone instead of buy new cell phone.If you will read once , you will feel good and your thinking will be change definitely. Read

  • Cognos Online Training
    By: Leotrainings | Jun 16th 2017
    LEO trainings is best in the region in which u can change your future in your splendid profession with our SAP MM online training in Hyderabad. After of total of the way you transform into the pleasantly confirmed master inside the commercial center. Read

  • Mongodb Training In Marathahalli Bangalore
    By: First Enquiry | Jun 15th 2017
    MongoDB is a free and open-source cross-platform, report situated database that gives, elite, high accessibility, and simple adaptability. MongoDB chips away at idea of accumulation and report. Read

  • Basic Principles Of Non-destructive Testing
    By: pitekmnd | Jun 13th 2017
    NDTCS offers NDT Schools Training Center-Houston together with the internet based Online NDT Testing training,
    NDT certification, NDT auditing & NDT inspection and NDT Level III Consulting services in the field of Nondestructive Testing.
    Read

  • Why The Vital Role Of Sensors In An Aircraft Engine Monitoring And Control
    By: Joseph Polizzotto | Jun 13th 2017
    These sensors and accessories do not work alone. Through real-time monitoring and reconfiguration, air-traffic control is able to monitor the control and health of the vehicle. Read

  • Monitor Your Target’s Mobile App Through Android 007
    By: romyfernandis | Jun 12th 2017
    Previously it was not easy to track someone, but now in this era of technology everything has become simple. Now you do not need to go to any detective agency to hire an agent for keeping an eye on your target. You can spy on anyone by your own without letting him/her know through your smart phone. Yes, smart phones have ma ... Read

  • Keep An Eye On Your People From Your Phone
    By: romyfernandis | Jun 12th 2017
    Now people can keep an eye on people whom they want to. They do not need to send any detective to follow them and track their activity. Android 007 is the application which can help you in tracking the activities and location of the person you want. Read


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