在Java数组和它们是如何储存在存储器 [英] Arrays in Java and how they are stored in memory

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

问题描述

我想了解Java中的阵列设置。你为什么要初始化数组中的每个对象的每一个空间,你已经创建数组后。它是如何存储在内存中是这样的:

I'm trying to understand the array setup in java. Why must you initialize space for each each object in the array, after you have created the array. How is it stored in memory like this:

[object][object]

或者是这样的:

[*class]->[object]  
[*class]->[object]

在换句话说,实际上是被在内存中完成。请问数组[0] =新类()只返回一个参考保留的内存位置,而类[]数组=新类[ 10] 语句创建沿10指针线,后来被新的报表分配到的东西吗?

In other words, what is actually being done in memory. Does array[0] = new class() just return a reference to a reserved location in memory, and the class[] array = new class[10] statement create something along the lines of 10 pointers, which are later assigned to by the new statements?

推荐答案

在Java软件商店阵列两种情况之一:要么基本值( INT 字符,...)或引用(又名指针)。

Arrays in Java store one of two things: either primitive values (int, char, ...) or references (a.k.a pointers).

因此​​,新的整数[10] 创建仅10 整数引用的空间。它的不可以创建10 整数对象(甚至是10 整数可用空间对象)

So, new Integer[10] creates space for 10 Integer references only. It does not create 10 Integer objects (or even free space for 10 Integer objects).

顺便说一句这也正是同样的方式,字段,变量和方法/构造函数参数的工作:他们也只存储原始值或引用

Incidentally that's exactly the same way that fields, variables and method/constructor parameters work: they too only store primitive values or references.

这篇关于在Java数组和它们是如何储存在存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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