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: https://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

  • An Overview On Ios 11 By Top Iphone App Development Companies In India
    By: Nirav Patel | Jan 6th 2018
    The finished version of the highly anticipated software iOS 11 is out now. Top-notch iPhone app development companies have already geared themselves with all the resources required to build an app for the latest iOS 11. Read

  • How Technology Impacted On Real Estate Sector
    By: hallmark builders | Jan 6th 2018
    Technology is used in different industries like hospitality, business, agriculture, communication, transportation, education and many more. As in the same way real estate sector has no exceptional with technology. There are few ways how real estate sector is depended on technology like marketing and advertising, People hunt ... Read

  • New Features In Java 8
    By: Infocampus | Jan 6th 2018
    Java 8 or JDK 1.8 is the most significant expansion of the Java language yet. Java 8’s new features such as Lambda Expressions, Stream APIs, Nashorn, Compact Profiles, new Time APIs increase the expressive power of the platform and make it easier for developers to take advantage of modern, multicore processors. This artic ... Read

  • Ios - 8 Principles Of Ios Development To Know Before Starting Your App
    By: Infocampus | Jan 6th 2018
    Building up a local iOS application can be a complex process. The developer you hire will be instrumental at all times, got the opportunity to be a professional with the correct blend of abilities to see your application through the outline and coding stages. That is the reason, before you begin searching for a designer; it ... Read

  • Understanding Python Basics
    By: Infocampus | Jan 5th 2018
    you will learn some Python basics, such as using the print function to display output. You will read about using variables and how to assign values to variables, and you will gain an understanding of their data types. By the end of this chapter, you will know how to get data into a script by using the input function and wri ... Read

  • Swift- A Powerful Open Language That Lets Everyone Build Amazing Apps.
    By: Infocampus | Jan 5th 2018
    Begin creating iOS Apps (Swift) is the ideal beginning stage for figuring out how to make applications that keep running on iPhone and iPad. View this arrangement of incremental lessons as a guided prologue to building your first application"including the instruments, significant ideas, and best practices that will facil ... Read

  • Implementing Iterable Interface In Java To Enable For-each Loop Based Iteration
    By: Infocampus | Jan 4th 2018
    how to implement java.lang.Iterable on a class to enable for-each loop based iteration through the collection of objects stored in that class. Tutorial starts off with explaining how Iterable and for-each loop are related, then explains how to implement Iterable, and finally shows a Java code example showing Iterable interf ... Read

  • Important Things You Need To Know About Selenium
    By: Infocampus | Jan 4th 2018
    Selenium can be a champion among the most able mechanical assemblies in the QA Engineers' munitions store. Shockingly, it can be dull to make, a terrible dream to keep up, and it can convey far an inordinate number of false negatives. Read

  • Swift -a New Programming Language For Os X And Ios
    By: Infocampus | Jan 3rd 2018
    With regards to speed, Swift is incredible. Swift is likewise present day with highlights like terminations, various return writes, and namespaces. Swift is totally local to both Cocoa and Cocoa Touch. Swift will be prepared for primetime out of the door," and will highlight "finish Xcode bolster." Additionally, when iOS ... Read

  • Learn Python Programming Step By Step
    By: Infocampus | Jan 3rd 2018
    we aim to teach all basic to advanced level Python programming concepts to a large no. of aspirants in the most efficient way. We've organized this course in such a manner that you can learn Python with least efforts, without investing a lot of your time and finally be able to apply this knowledge to your live projects. Read


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