Java 和 Java 中的简单变量 C++
我在一些资料中看到这样一句话:
“在Java中,简单数据类型如int和char的操作就像在C中一样”。
我想知道实际上它们在 Java 和 Java 中是不同的。 C++?
在C++中,像Java中的基元这样的简单变量也被分配了一个内存地址,因此C++中的这些基元类型也可以有一个指针。 然而,Java 中的基元不像对象那样分配内存地址。
我对么?
谢谢!
I saw this sentence in some matrials:
"In Java, simple data types such as int and char operate just as in C."
I am wondering that actually they are different in Java & C++?
In C++, simple variables like the primitives in Java are assigned a memory address as well, so these primitive types in C++ can have a pointer as well. However primitives in Java are not assigned a memory address like Objects are.
Am I correct?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
几乎。
在java中,原语也被分配了内存,但这发生在内部,你无法获得引用。
原因是为了提供内存管理的安全性。
Almost.
In java primitives are assigned memory as well, but this happens internally and you cannot get the reference.
Thre reason was to provide security on memory management.
在简单类型方面,Java 和 C++ 之间存在一些差异。 内存分配有所不同,因为 Java 隐藏了显式指针和引用的概念。 您只需创建简单类型并分配一个值。 另一个区别是 Java 对每个简单类型检查使用标准化内存占用 Java 简单类型。 另一方面,C++ 依赖于编译它的平台。 另一点(我不知道这是否有区别)是简单类型的赋值是线程安全的。 我没有用 C++ 做过任何多线程编程,所以我不确定这是否是一个考虑因素。
There are a few differences between Java and C++ when it comes to simple types. The memory allocation is different as Java hides the concept of explicit pointers and references. You just create the simple type and assign a value. Another difference is that Java uses standardized memory footprints for each simple type check Java Simple Types. C++ on the other hand is dependent on the platform that it is compiled on. Another point (and I don't know if it is a difference or not) is that assignment into simple types is Thread safe. I've not done any multi-threaded programming in C++ so I'm not certain if this is a consideration.
对于大多数基本的东西,它们的工作原理是相同的,但存在大量细节差异。
For most basic stuff, they work the same, but there is a ton of detail differences.
正确的。
在java中,你不能将引用(指针)传递给原始类型。
//事实上Java中没有指针这样的东西。
在 Java 中,原始类型只能按值传递。
有一种解决方法,但通常不建议这样做,除非您的情况需要。
您可以使用包装类。 何时使用包装类和原始类型
仅传递对象引用。
此外,基本类型在堆栈上分配,对象在堆上分配。
Correct.
In java you cannot pass a reference(pointer) to a primitive type.
//Infact there is no such thing as pointers in Java.
A primitive type can only be passed by value in Java.
There is a way around it, but it isnt usually recommended unless your situation demands it.
You can use a wrapper class. When to use wrapper class and primitive type
Only objects are passed by reference.
Also Primitive types are allocated on the stack and Objects are allocated on the heap.
基元存储在堆栈中并按值传递。
对象存储在堆上并通过引用传递。
我不知道这是否是您的要求,但我很无聊,想发布一些东西
Primitives are stored onto the Stack and are passed by value.
Objects are stored onto the heap and are passed by reference.
I've no idea if this is what your asking, but I'm bored and wish to post something