0

I have a doubt from one of the answers in stackoverflow to the following question:

How many String objects are created by the following code?

String x = new String("xyz");
String y = "abc";
x = x + y; 

In Stephen's answer he mentioned x and y are not constant variables.

My doubt- String is a final class, and it's instance will be a constant because String is an immutable class. Why is the reference variable of this constant class not a constant variable? - I do agree with Stephen though as x = x + y; points at "xyzabc" created in the heap memory.

Pranav
  • 3
  • 3
  • 1
    Well, because *immutability* applies to *instances* and not *references*. You could always make your *reference* point to another *immutable object*. If you really want to prevent re-assigning of references, then make them `final` – TheLostMind Jun 22 '17 at 05:10
  • look at this SO question may it help. [link](https://stackoverflow.com/questions/8798403/string-is-immutable-what-exactly-is-the-meaning) – always-a-learner Jun 22 '17 at 05:14
  • @TheLostMind Thanks for clearing it. So, there is no difference between the reference variable created by a constant class and a normal class? – Pranav Jun 22 '17 at 05:20
  • @Pranav - There is nothing called a constant class. We only have *immutable* instances and references that can be made *constant* – TheLostMind Jun 22 '17 at 05:24
  • @TheLostMind I meant class defined with the `final` keyword. That class will become immutable. So, nothing special will happen if I declare a variable with that immutable class name. It's the same as declaring a variable with normal class name as it's data type. – Pranav Jun 22 '17 at 05:51
  • I was wrong again - Thanks to @sweeper for rightly pointing out that making a class final doesn't make the class immutable. It is said to be immutable when the value stored by the class can't be changed. – Pranav Jun 22 '17 at 06:15

1 Answers1

0

There are a few concepts that you need to understand.

Marking a class as final does not make it immutable. It just makes it un-inheritable.

JLS §8.1.1.2

A class can be declared final if its definition is complete and no subclasses are desired or required.

It is a compile-time error if the name of a final class appears in the extends clause (§8.1.4) of another class declaration; this implies that a final class cannot have any subclasses.

A class is said to be immutable when the values that it stores cannot be changed after initialisation.

A constant variable is a variable marked with final.

JLS §4.12.4

A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

x and y here are not constants because they are not marked final. That's it.

"But strings can't change, so they are constants, right?" you might ask.

String objects themselves can't change, but string variables can. I'll show you:

String s = "Hello";
s = "Goodbye";

The variable's value is changed so that it refers to another string. The original string "Hello" is not changed.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313