美文网首页
java中数组是对象吗?

java中数组是对象吗?

作者: 我要进大厂 | 来源:发表于2021-08-21 21:10 被阅读0次

是的。java中的数组是对象。在Java编程语言中,数组是动态创建的对象,可以赋值给Object类型的变量。Object类的所有方法都可以在数组上调用。

原文链接: https://www.geeksforgeeks.org/array-primitive-type-object-java/

An array in Java is an object. Now the question how is this possible? What is the reason behind that? In Java, we can create arrays by using new operator and we know that every object is created using new operator. Hence we can say that array is also an object. Now the question also arises, every time we create an object for a class then what is the class of array?

  • In Java, there is a class for every array type, so there’s a class for int[] and similarly for float, double etc.
  • The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable.
  • In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

For every array type corresponding classes are available and these classes are the part of java language and not available to the programmer level. To know the class of any array, we can go with the following approach:

// Java program to display class of 
// int array type
public  class Test {

 public static void  main(String[] args) {
      int [] x = new int[3];
      System.out.println(x.getClass().getName());
  }
}

Output:

[I 

“[”表示这是一个数组,而且是一维的,“I”表示数组元素是int类型的。
NOTE:[I this is the class for this array, one [ (square bracket) because it is one dimensional and I for integer data type.
Here is the table specifying the corresponding class name for some array types:-

int[]                     [I
int[][]                   [[I
double[]                  [D
double[][]                [[D
short[]                   [S
byte[]                    [B
boolean[]                 [Z

In Java programming language, arrays are objects which are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.

// Java program to check the class of 
// int array type
public class Test {
    public static void main(String[] args)
    {
        // Object is the parent class of all classes 
        // of Java. Here args is the object of String
        // class.
        System.out.println(args instanceof Object);
  
        int[] arr = new int[2];
  
        // Here arr is also an object of int class.
        System.out.println(arr instanceof Object);
    }
}

Output:

true
true

Reference : https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

相关文章

  • java中数组是对象吗?

    是的。java中的数组是对象。在Java编程语言中,数组是动态创建的对象,可以赋值给Object类型的变量。Obj...

  • 【骚全带你学Java---九、认识java数组】

    java中数组 数组是指一组数据的集合,数组中的每个数据称为元素。在Java中,数组也是Java对象。数组中的元素...

  • 引用类型数组

    数组是对象 在Java中,数组属于引用类型数据数组数据在堆中存储,数组变量属于引用类型,存储数组对象的地址信息,指...

  • 前端笔记:JavaScript

    Array对象 Array即我们所说的数组,js中的数组对象不像java中那么严谨,Array是长度自动变化的数组...

  • Java 数组

    Java 中,数组是一种引用类型。 Java 中,数组是用来存储固定大小的同类型元素。 数组对象(这里可以看成一个...

  • 20160710_Programming

    ------JAVA20160710------1、实际的数组对象存储在堆内存中,如果引用该数组对象的数组引用变量...

  • 多线程、并发及线程的基础问题

    一、Java 中能创建 volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是...

  • 面试题总结

    1、Java中能创建Volatile数组吗? 能,Java中可以创建volatile类型数组,不过只是一个指向数组...

  • JDK序列化官方指南

    在网络中数据传输都是以字节数组传输的,而在java程序中操作的是类对象。java程序中对象到对象的传递在网络中的传...

  • 一个对象的前世今生

    对象的诞生 本文讨论的对象,限于普通的Java对象,不包括数组,class对象等。在Java中,当程序执行发现ne...

网友评论

      本文标题:java中数组是对象吗?

      本文链接:https://www.haomeiwen.com/subject/ngrqiltx.html