Java基本类型数组对象或没有? [英] java primitive type array object or not?

查看:82
本文介绍了Java基本类型数组对象或没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的程序:

int[] arr= new int[2];
.....
System.out.println(arr.length);

在JLS我们读到的对象在Java中的类或数组的实例。
但是,我们在这里看到原始类型的数组。因此, INT [2] 是一个对象了。但是,它是怎么实现的?这是某事象 INT =>整数 INT [] =>整数[]
别的东西?顺便说一句,它驻留?在堆或堆叠?

In JLS we read that object in java is instance of class or array. But, we see here array of primitive type. So, int[2] is an object too. But, how it was achieved? It was sth like int=>Integer, or int[]=>Integer[] or something else? And by the way, where it resides? In heap or in stack?

推荐答案

在Java中,你只有原始的或引用的对象。*

In Java you only have primitive or references to objects.*

int[] arr = { 1, 2 };
int i = arr[0];
assert arr instanceof Object; // true

改编是它有一个数组的引用 INT 作为元素。

arr is a reference to an array which has int as a element.

这是如何实现的?

INT [] 有它自己的类 INT []。类和您的 ARR 是类的一个实例。

int[] has it's own class int[].class and your arr is an instance of that class.

这是如int =>整数某物或int [] =>整数[]或别的东西吗?

It was sth like int=>Integer, or int[]=>Integer[] or something else?

整数完全不相关。转换一个 INT [] 整数[] 你必须创建一个新的数组和复制的每个元素的一个之一。

Integer is not related at all. to convert an int[] into an Integer[] you have to create a new array and copy every element one by one.

顺便提一句,它驻留?在堆或堆栈?

And by the way, where it resides? In heap or in stack?

您可以假设所有的对象都是在堆上。 Java的8可以将使用逃逸分析栈中的某个对象,但我不相信它可以为数组类型做到这一点。

You can assume all objects are on the heap. Java 8 can place some objects on the stack using Escape Analysis, however I don't believe it can do this for array types.

* 唯一的其他类型是无效这既不是原始的,也不是一个参考。它有一个包装虚空,但你不能有一个字段或类型的参数无效

* the only other type is void which is neither a primitive nor a reference. It has a wrapper Void but you can't have a field or parameter of type void.

这篇关于Java基本类型数组对象或没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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