数组如何在Java内部工作? [英] How array works internally in Java?

查看:125
本文介绍了数组如何在Java内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询已发布,基本上了解点

This query is posted to basically understand points like


  • 对象是类实例或数组;

  • An object is class instance or an array;

数组是 Object 类的子类;

除了原语以外的所有实例都是Java中的对象。

Everything that is instantiated other than primitive is an object in Java.

这是我对工作的理解使用Java中的数组。

Here is my understanding of working with arrays in Java.

考虑以下程序,

/* dummy.java */
class C {
    private int i; 
    public C() {
        i = 1;
        System.out.println("Am in constructor");
    }
}
public class dummy {
    public static void main(String[] args) {
        C[] c = new C[2]; // Line 11
        c[0] = new C();
        System.out.println(c);
    }
}

类型的对象[LC 在运行后的运行时创建,

An object of type class [LC is created in run-time after running,

C[] c = new C[2]; //Line 11

在上面的代码中。 class [LC Object 类的直接子类。在上面的代码中运行第12行之后,引用变量 c 指向此对象(在下面的红色边界中显示)。引用变量位于堆栈中,类型类C 的对象将进入堆中。

In the above code. class [LC is an immediate subclass of Object class. Reference variable c points to this object (shown in red boundary below) after running Line 12 in the above code. Reference variables sit in stack and an object of type class C will go in heap.

以下更改第11行& 12 在上面的代码中

For a below change of line 11 & 12 in the above code

C[][] c = new C[2][2];
c[0][0] = new C();

将表示如下所示。

我的理解正确?如果是,请详细说明运行时间类[LC ]的使用情况,以实例化一个对象?

Is my understanding correct? If yes, Can you please explain more on usage of class [LC in run time to instantiate an object?

注意: C [] .class 给出运行时的实际类型,即 class [LC

Note: C[].class gives the actual type in run-time, which is class [LC.

推荐答案

回答你的问题:是的,在Java(和C#)中,几乎所有内容都被分成多个离散的内存块,这些内存可以通过指针访问。这不仅包括你的二维数组,还包括你对象里面的任何嵌入对象。在C ++中,如果你有一个包含10个对象的数组(单维或不包含),并且每个对象包含10个嵌入对象,你可以用一块记忆。在C#和Java中,对于存储所有这些(如果所有嵌入对象都是简单对象),在单维数组的情况下,您将至少有101个内存分配,而在多维数组的情况下则更多。

To answer your question: yes, in Java (and C#), nearly everything is split into multiple discrete chunks of memory accessed by pointers. This not only include your bidimensional array but also any embedded object inside your object C. In C++, if you have an array (single dimensional or not) of 10 objects and each of these object contains 10 embedded objects, you can allocate all this with a single piece of memory. In C# and Java, you will have a minimum of 101 memory allocations for storing all this (if all of the embedded objects are simple objects) in the case of a mono-dimensional array and more in the case of a multi-dimensional array.

然而,这些内存片段的爆炸不应该被视为非常糟糕的东西,因为它可以让你免于管理自己的内存分配的困难,因为你可以拥有使用C / C ++,在大多数情况下,任何现代CPU的功能通常足以以足够的速度向前推进。

However, this explosion of pieces of memory shouldn't be seen a something very bad because it's free you of the difficulty of managing yourself the allocation of memory as you can have with C/C++ and in most cases, the power of any modern CPU is generally sufficient to pull it forward at a sufficient speed.

这篇关于数组如何在Java内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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