是否每个对象常量分配内存? [英] Does each object allocate memory for constants?

查看:124
本文介绍了是否每个对象常量分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码设计中,我已经包括了很多常量。当创建一个新对象时,存储器分配给该对象的常量,或者是它永久地存储在单个实例,像一个静态变量是?在存储器方面,到底在哪做静态项目结束了?

In my code design, I've included a lot of constants. When a new object is created, is memory allocated for that object's constants, or is it stored permanently in a single instance, like a static variable is? In terms of memory storage, where exactly do static items end up?

在换句话说,如果我定义了100个对象,会不会有相同的恒定值的100份?

In other words, if I define 100 objects, will there be 100 copies of the same constant value?

如果他们是在静态存储器定义(无论是),而我希望自己是,并访问它们需要计算机切换内存页?是否有持续访问的常量,而不是变量对性能的影响?

If they are defined in static memory (wherever that is), and I would expect that they are, does accessing them require the computer to switch memory pages? Is there a performance hit for constantly accessing constants instead of variables?

感谢

战神

推荐答案

常量都在运行时没有存储位置。所有访问该常数当更换编译代码标识的字面意义的常数标识符结果。

Constants have no storage location at runtime. All access to constant identifiers results in the literal value of that constant replacing the identifier when the code is compiled.

这篇关于是否每个对象常量分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆