2

From Core Java Volume I—Fundamentals by Cay S. Horstmann:

Isn’t that a lot less efficient? It would seem simpler to change the code units than to build up a whole new string from scratch. Well, yes and no. Indeed, it isn’t efficient to generate a new string that holds the concatenation of "Hel" and "p!". But immutable strings have one great advantage: The compiler can arrange that strings are shared.

To understand how this works, think of the various strings as sitting in a common pool. String variables then point to locations in the pool. If you copy a string variable, both the original and the copy share the same characters.

I don't understand what they actually mean by common pool. Is it something like a swimming pool or something else entirely?

1

2 Answers 2

1

A 'pool' is a collection or supply of something. A swimming pool is a collection of water.

See definition 4 here.

So, in your specific Java example, the strings are all collected together in a 'pool'. It's 'common' because anything that needs a string will check this shared pool for the string it needs before creating a new one, which would then be added to the pool.

'Pool' can also be used as a verb which means

to put together; contribute to a common fund, on the basis of a mutual division of profits or losses; to make a common interest of; as, the companies pooled their traffic

Here on Stack Exchange, we pool our knowledge.

'Pool' is also commonly used in gambling scenarios to refer to the common collection of money that the gamblers can win. When I buy into a $10 poker game, I put my $10 into the pool.

1

There are several other meaning of pool, most of which mean roughly "a collection of resources." You can see these other definitions here (second definition).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .