Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String can't change. But int, char can change

I've read that in Java an object of type String can't change. But int and char variables can. Why is it? Can you give me an example?

Thank you. (I am a newer -_- )

like image 662
Keating Avatar asked Dec 24 '09 02:12

Keating


People also ask

Can strings be changed in C?

You need to copy the string into another, not read-only memory buffer and modify it there. Use strncpy() for copying the string, strlen() for detecting string length, malloc() and free() for dynamically allocating a buffer for the new string.

How do you fix a string that Cannot be converted to int?

The most direct solution to convert a Java string to an integer is to use the parseInt method of the Integer class: int i = Integer. parseInt(myString); parseInt converts the String to an int , and throws a NumberFormatException if the string can't be converted to an int type.

Can string values be changed?

Strings are immutable. Once you have created a string you cannot later change that string object. Java uses pass-by-value, not pass-by-reference.

Can string variables be changed in Java?

A unique thing about string objects in java is that once created, you cannot change them. By the way of explanation, you cannot change the characters that compromise a string.


1 Answers

As bzabhi said, strings are immutable in Java. This means that a string object will never change. This does not mean you can not change string variables, just that you cannot change the underlying memory representation of the string. for an example:

String str = "Hello";
str += " World!";

Following the execution of these lines, str will point to a new string in memory. The original "Hello" string still exists in memory, but most likely it will not be there for long. Assuming that there are no extenuating circumstances, nothing will be pointing at the original string, so it will be garbage collected.

I guess the best way to put this would be to say that when line 2 of the example executes, a new string in memory is created from the concatenation of the original string and the string being added to it. The str variable, which is just a reference to a memory location, is then changed to point at the new variable that was just created.

I am not particularly knowledgeable on the point, but, as I understand it, this is what happens with all "non-primitive" values. Anything that at some point derives from Object follows these rules. Primitive values, such as ints, bools, chars, floats and doubles allow the actual value in memory to be changed. So, from this:

int num = 5;
num += 2;

the actual value in memory changes. Rather than creating a new object and changing the reference, this code sample will simply change the value in memory for the num variable.

As for why this is true, it is simply a design decision by the makers of Java. I'm sure someone will comment on why this was made, but that isn't something I know.

like image 198
Seburdis Avatar answered Sep 17 '22 21:09

Seburdis