-2

Are literal strings only stored in the string constant pool? Isn't it stored in the constant pool? Or are both stored?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ssibongee
  • 3
  • 1
  • Please elaborate a bit - what exaclty confused you? Perhaps you could quote some documentation you read, or explain with some example? – Hulk Nov 01 '20 at 08:51
  • Related: https://stackoverflow.com/questions/5777131/java-string-intern-and-literal – Hulk Nov 01 '20 at 08:52
  • Does this answer your question? [Strings are objects in Java, so why don't we use 'new' to create them?](https://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them) – Curiosa Globunznik Nov 01 '20 at 08:53
  • https://stackoverflow.com/questions/57414169/string-pool-do-string-always-exist-in-constant-pool I read in this post he says In the string pool and constant pool that's why i have some questions – ssibongee Nov 01 '20 at 09:01
  • https://stackoverflow.com/questions/57414169/string-pool-do-string-always-exist-in-constant-pool and I see i this post says like this The constant pool is a table of structures which represents various, class and interface names, field names, string constants and other constants that are referred to within the ClassFile structure and its substructures. – ssibongee Nov 01 '20 at 09:08
  • 1. The question in your title and first sentence is ambiguous. 2. What is 'it'? 3. What are 'both'? – user207421 Nov 01 '20 at 09:13

1 Answers1

0

In a comment, you added a link to question "String pool - do String always exist in constant pool?". Since you apparently didn't understand the beginning of the answer, let me recap:

  • The constant pool is part of a .class file.

  • The string pool is part of the JVM's memory for storing interned strings.

There is no "string constant pool" anywhere.

String literals are stored in the .class file in the constant pool part of the file. When a .class file is loaded into memory, the string literals are loaded into interned strings, and hence end up being stored in the string pool.

So, to answer the question: String literals are store in both the constant pool (on disk) and in the string pool (in memory).

Andreas
  • 154,647
  • 11
  • 152
  • 247