可能重复:
我有几个与记忆有关的问题,我希望你们能回答。考虑这段代码,使用Foo表示一个包含大量方法和大量原始数据成员的大型类:
class Foo {
public:
Foo() : // Initialize all data members ...
{ }
// Lots of methods ...
~Foo() { /*Nothing needed here ... correct?*/ }
private:
int a;
int b;
char c;
double d;
据我所知,在Java中,堆栈内存保存原语和方法调用,堆内存用于存储对象。
假设我有一堂课
class A {
int a ;
String b;
//getters and setters
}
类a中的原语A将存储在哪里?
为什么堆内存存在?为什么我们不能把所有东西都存储在堆栈上呢?
当该对象被垃圾收集时,与所反对的对象关联的堆栈是否被销毁?
我理解在Java中,所有的方法调用都在堆栈上。以以下课程为例:
Class Demo
{
// Some instance variables
public Demo()
{
initialize();
}
public void initialize()
{
// Start initialization
....
// Call another method to perform some complex calculation
int resultVal = helperMethod();
我还没有找到任何可靠的文献,但我很好奇,如果在方法中创建一个对象,它会被存储在哪里?在堆栈上还是堆上的java 8中?
public class A {}
.
.
.
public class B {
public void test(){
A m = new A();
}
}
我知道通常只有本地原语、引用变量和函数调用存储在堆栈中,而对象存储在堆中。
因此,我假设以下两种情况之一都是正确的
案例1:在方法中实例化的对象像往常一样存储在堆中,引用堆栈中的对象,然后当函数完成时,对象引用超出范围,堆中的对象随后可用于垃圾收集。
案例2:在方法中实例化的对象存储在堆栈中,然
class LinkTest{
public:
LinkTest(){
}
void start(){
List list;
Node * n ;
for(int i = 0; i < 5; i++){
//using pointer
// n = new Node(i);
// list.add(n);
//not using pointer
Node n(i);
list.add(&
问题1:我有一个带有静态方法的java类:
class Test {
static public void show (String value) {
// dosomething with value
}
}
如果我想从c++打电话:
jstring value = env->NewStringUTF("Hello, World");
// ExceptionCheck after NewStringUTF.
env->CallStaticVoidMethod(the_jclass, the_jmethodID, value);
Class A {
// blah blah
}
现在,每当我们需要创建这个类的实例时,我们都会这样做:
A a = new A();
在c++中有两种方式:
1. A a(10); // Created on Stack. Assume that the constructor takes an int argument
2. A a = new A(); // Created on Heap
如何在堆栈上创建用户定义的Java对象?