answersLogoWhite

0

Explain strings are immutable in java with example?

Updated: 8/10/2023
User Avatar

Prashant20791com

Lvl 1
14y ago

Best Answer

Among other things, the immutability of a String give us a few guarantees:

* A String will always be Thread-safe * Duplicate Strings can be multiple pointers to the same data (reduce memory footprint) * substring method is very fast (only need a pointer and an offset, no copying) See http://mindprod.com/jgloss/immutable.html for a full description.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Whenever you try to modify a string, java will create an entirely new string with the modifications

For example:

String a="abc";

a.substring(1);

In this example code, if we were to print out variable a, the string "abc" would be printed out. When we called the method substring, the java compiler created the substring rather than modify. Thus inside the memory, there is a space for "abc", and another for "bc". In this case, the garbage collector would erase "bc" since there is no reference.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Strings are immutable objects. They cannot be modified. If you modify a String value, a new String object would be created. The existing values would not be edited. That is why use of StringBuffers is advised in cases where we need to modify String values.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain strings are immutable in java with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What mutable and immutable in java and example program?

lmutable object


How to create immutable class in java?

by making that class final


How do you campaire Strings in java?

String class in Java has an 'equals' method that can be used to compare strings.


Which classes are mutable and nonmutable?

An immutable class is any data storage class in which the data cannot be changed. The most common example of this in Java is the String class.


What are some differences between String and StringBuffer in Java?

A String in Java refers to an immutable object that holds alphanumeric values. Everytime you try to modify the value held inside the String, a new object would be created.A StringBuffer refers to an object that is built to hold alphanumeric values for modification. StringBuffers were built for handling strings that need to be modified.Functionality wise both of them are similar.StringBuffer is mutable and faster with string manipulation operations, whereas Strings are immutable and are slower than StringBuffer for string operations.1) String objects are constants and immutable whereas StringBuffer does not2) String class supports constant strings. whereas String Buffer class supports growable and modified string3) Strings once we created we cannot modify them. whereas String Buffer objects after creation also can be able to delete to append any characters to it


Explain the importance of stringbuffer class in java?

Java string types are immutable, meaning that once they are created they cannot be "grown" to accommodate longer strings. For example, the following code abandons and re-allocates the memory for resultString after every concatenation operation: resultString = "foo"; // Set resultString resultString += "bar"; // Abandon resultString, set as "foobar" resultString += "bash"; // Abandon again, set as "foobarbash" The point of stringbuilder is to have an object that can be grown as necessary and doesn't require reallocation. It is therefore more efficient then normal concatenation.


What is mutable in java?

It means that something can be changed after it is created. Immutable means that it can't be changed.


Where can one watch immutable objects?

An immutable object is classed in Java programming as an object that cannot be changed after it is made. YouTube has videos that can be viewed on this subject as well as Vimeo.


Explain need of string classes in java?

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating Strings The most direct way to create a string is to write: String greeting = "Hello world!"; In this case, "Hello world!" is a string literal-a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value-in this case, Hello world!. As with any other object, you can create String objects by using the new keyword and a constructor. The String class has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters: char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); System.out.println(helloString); The last line of this code snippet displays hello. Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.


Is String in java a class or object?

String is a pre-defined class in Java. For example: String s = new String("This is a string"); the variable s is now a String object since it was declared and initialized in the String class.


How java is different from c plus plus Explain with example?

See related links, below.


Are binary strings used in Java?

No if there was then java wouldn't have over 4 billion down loads