“Integer literal in Java?” 的中文翻译是 “Java中的整数字面值?”

huangapple go评论63阅读模式
英文:

Integer literal in Java?

问题

int x = 10;

这里的 10 直接赋值给变量 x,没有存储在 permgen 空间中。

int z = 2*x+y;

同样,这里的整数字面值 2 也不会存储在 permgen 空间中。

关于 Java 中字面值的存储和 permgen 空间的问题,可以帮助你理解字面值的存储方式以及 permgen 空间的用途。如果你有更多疑问,请继续提出。

英文:
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.

答案1

得分: 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.

英文:

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.

huangapple
  • 本文由 发表于 2023年4月17日 09:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定