-1
int x = 10;

Here 10 is directly assigned to variable x or 10 is stored somewhere else and then it get assigned to variable x?

I have read in one of the stackoverlow answer that Java stores literals in permgen space. So is 10 stored inside permgen space first and then get assigned to variable x?

int z = 2*x+y

Here also is integer litareal 2 stored inside permgen space?

Can anyone help me in understanding where literal of any data type stored in Java and what is permgen space?

I have try to understand but got confused.

xyz
  • 17
  • 3
  • 4
    Literals are stored in the *constant pool* and loaded from there at the byte code level. **What** problem are you trying to solve, and how does this help you solve it? – Elliott Frisch Apr 17 '23 at 02:01
  • @ElliottFrisch I am just clearing my basics. So 10 is first stored in the constant pool and from there, it is loaded to variable x, and literal 2 also stored inside the constant pool ?? – xyz Apr 17 '23 at 02:14
  • Yes. Every literal is loaded from the constant pool. The command `javap -v` can be used to examine the byte code, that's all there is at runtime so it must be in the byte code. – Elliott Frisch Apr 17 '23 at 02:17
  • @ElliottFrisch Thank you for clarification. Is there any difference between constant pool and string pool? Or both are same?? – xyz Apr 17 '23 at 02:19
  • 3
    This aren't the "basics", these are deep internals that go waaaay beyond what you normally care about when coding Java. Still relevant, but not at all basic. String pool and constant pools are VERY different things: https://stackoverflow.com/a/23253122/2442804 – luk2302 Apr 17 '23 at 02:28
  • Part of the String pool is loaded by the constant pool which contains Strings. Use `javap -v`, or just read about the [class file format](https://jcp.org/aboutJava/communityprocess/maintenance/jsr924/JVMS-SE5.0-Ch4-ClassFile.pdf). – Elliott Frisch Apr 17 '23 at 02:30
  • Unfortunately, there are a bunch of Java tutorial sites (and stack overflow Q&As) that *incorrectly* call the string pool the "string constant pool". That leads to all sorts of confusion for newbies and not-so-newbies. And of course ... "permgen" was removed in Java 7 ... a long time ago. In short: be selective in what you read!! – Stephen C Apr 17 '23 at 02:45
  • @StephenC Is there any difference between constant pool and string pool? – xyz Apr 17 '23 at 03:55
  • 1
    Yup. The constant pool is a region of a class file. The string pool is a data structure in the heap. Completely different things. And you absolutely don't need to know this stuff ... unless you are planning on modifying or debugging the JVM. – Stephen C Apr 17 '23 at 04:03
  • 1
    A normal programmer just needs to follow these rules: 1) use `String.equals` not `==`, and 2) don't use `String.intern` ... turn on the GC string deduping instead. If you do that, you need to know **nothing** about the string pool. – Stephen C Apr 17 '23 at 04:09
  • 1
    And the constant pool is only relevant if you are writing or maintaining a custom reader for ".class" files. There are probably less than 100 people on the planet who need to do that. – Stephen C Apr 17 '23 at 04:16

1 Answers1

3

Your code has to live somewhere. It starts out as a source file, which javac turns into a .class file, which then goes.. somewhere. Java is flexible enough that you can write your own loaders if you want. By default, they go directly to disk or in a jar file and a java virtual machine can load them from there. When the JVM starts, it does not load them all. It just loads the one class that you told the JVM contains the main method. However, the JVM, anytime it needs to execute code, always scans 'hey which types are required to run this code' and if that finds any types that aren't currently loaded, the JVM will first load that type (guaranteed once only), 'resolve' it (this mostly involves running all static initializers), and then continues. Hence, generally if you have a few classes they'll be loaded nearly immediately (only your main class is loaded but executing it will usually cause most of your other classes to get loaded soon after).

When you write int z = 10; That 10 is in your class file. You can run javap -v -c com.foo.YourCompileClass to see it.

The bytecode generated is one that means 'load this value from the constant pool'. At runtime, java is a stack based processor, so there is no such thing as 'variable z'. There is a position on the stack or possibly a local frame slot, depends on how javac translated your java code into bytecode.

Permgen ceased to be a thing 15 years ago. class files now basically just live in the same place any other object lives. That's where the '10' lives, directly. Same for int z = 2 * x * y; - that 2 is in the same place the IMULT instruction lives that tells the JVM to multiply the last 2 things on the stack. Code is, after all, just as much 'data' as data would be.

It's fairly clear from your question you do not perhaps have a good understanding of how JVMs / machine code works. Perhaps ask a more specific question. Why do you 'care' - are you trying to optimize for performance? These are the kinds of internals where the very point is that you don't need to care about it to write good code. It's not going to have a performance impact you could possibly measure. Knowing exactly how all this works has more or less zero effect on your skills as a java programmer.

Now if you are going to hack on the JVM itself, sure, this is good to know. That's the kind of programmer task that only seniors with 20 years of experience would ever do. If you are a beginner, this is identical to the following conversation:

xyz: Hi! I'm 16 and ready for my first driving lesson!

instructor: Great, here's the wheel, here's the gas ped...

xyz: nonono nevermind all that! Here is a picture of a carburettor and the differential axle photo from wikipedia, it was built in 1962. I think we should spend 50 hours talking about all the physics and math involved in how this differential works, because only then will I be any good at driving a car!

You see how utterly preposterous that would be. Same thing applies here. These are internal details that you do not need to know about: They do not 'affect' anything - they aren't leaky abstractions. Plenty of under the hood stuff can show up in weird ways and it can be useful to know about them (though, even there, as beginning coder I doubt that's a good place to start!) - but this? No. It won't make you a better coder. Just a more curious one.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • *"That's the kind of programmer task that only seniors with 20 years of experience would ever do."* - And most of them (us) are far too wise to do that :-) – Stephen C Apr 17 '23 at 02:56
  • So I just need to know that literal 10 is loaded from the constant pool and string literal is present in the string constant pool that's all. And all these pools are present in heap memory. I don't need to go into its internal working. Please correct me if I am wrong ?? – xyz Apr 17 '23 at 03:41
  • 2
    Yeah that sounds fine :) – rzwitserloot Apr 17 '23 at 03:50