在C ++和Java中声明对象 [英] declaring objects in C++ vs java

查看:128
本文介绍了在C ++和Java中声明对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用c ++一段时间了,我正在学习java

I have been using c++ for a while now and I am learning java,

在Java中声明对象使我感到困惑,

declaring objects in java is confusing me,

在Java中我们编写

myclass myobject = new myclass();
myobject.mymethod();

与c ++中的这段代码相同吗?

Is it same as this code in c++ ?

myclass *myobject = new myclass();
myobject->mymethod();

即是否在heap上分配了内存?如果它在堆上,为什么我们从不释放内存.我相信新关键字是一样的. 如果是这样,我们如何在堆栈上分配内存?

i.e is the memory getting allocated on heap? If it is on heap why we never free the memory. I believe the new keyword is the same. If so, how do we allocate memory on stack?

推荐答案

与c ++中的这段代码相同吗?

Is it same as this code in c++ ?

是的.一样.

即在堆上分配了内存吗?

i.e is the memory getting allocated on heap?

是的

如果它在堆上,为什么我们从不释放内存.

If it is on heap why we never free the memory.

当对象不再可访问时,允许该对象进行垃圾回收.即当没有对该对象的有效引用或(取消引用)

The object is allowed to garbage collector when it is no more reachable. i.e when there are no valid reference to that object or (de-referenced)

如果是这样,我们如何在堆栈上分配内存?

If so, how do we allocate memory on stack?

当特定线程开始执行时,与该线程相关的变量将被放置在堆栈上,并在该线程的作业完成后立即被删除.每个线程都有自己的堆栈.

When the particular thread execution starts, variables related to that thread will be placed on stack and will be remove immediately once the job of that thread finished. Every thread has it's own stack.

这篇关于在C ++和Java中声明对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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