Complex data type usage in Java

Source: Internet
Author: User
Tags arrays comparable

Index

As with the definition of a simple data type, the Java Virtual Machine (JVM) also defines the data type of the index (reference). An index type can refer to a variable, and since Java does not explicitly define a pointer type, the index type can be considered a pointer to the actual value or to the actual value represented by the variable. An object can be "pointed" by more than one index. The JVM does not address objects directly, but rather the index of the operations object.

The index type is divided into three types: class, Interface (interface), and array. An index type can reference dynamically created class instances, generic instances, and arrays. The index can also contain a special value, which is the null index. The null index does not have a corresponding type at run time, but it can be converted to any type. The default value for the index type is null.

Class

Class refers to the data type that defines methods and data. Internally, the JVM typically implements the class type Object as a set of pointers to methods and data. A variable that defines a class type can only reference an instance of a class or null, as shown in the following code:

MyObject anObject = new MyObject(); // 合法
MyObject anotherObject = null; // 合法
MyObject stillAnotherObject = 0; // 非法

Interface

Interface (interface) is like a template that defines the methods that an object must implement in order for these methods to be referenced as interface instances. interface cannot be instantiated. Classes can implement multiple interfaces and are indexed through the interfaces of these implementations. An interface variable can only index an instance of the class that implements the interface. For example, suppose we define an interface, the name is comparable, and also define a class Sortitem, which implements the interface comparable, then we can write the following definition code:

Comparable C = new Sortitem ();

If the comparable interface defines a method: public void Compare (comparable item), then the Sortitem class must provide an implementation of the Compare method, as shown in the following code:

public class SortItem implements Comparable
{
public void compare(Comparable item)
{
...method implementation here
}}

Array

Java Arrays (array) are dynamically created indexed objects, which are very similar to classes, and, like this, arrays can only index an instance of an array or null, as shown in the following code:

int[] Myintarray = new INT[5];

int[] Anotherintarray = null;

An array is an inheritance of the object class, so that all methods of the object class can be invoked by an array. An array object is made up of elements. The number of elements can also be 0, in which case the array is called empty. All arrays are numbered from 0, which means that the index number of the 1th element within the array is number 0. All access to an array element is checked at run time, and if an attempt to index an element with a number less than 0 or greater than the length of the array will result in an arrayindexoutofboundsexception exception and be thrown out.

The elements of an array are indexed by an integer value, as shown in the following code:

int[] myIntArray = { 9, 5, 6 };
int int1 = myIntArray[0];
int int2 = myIntArray[1];
int int3 = myIntArray[2];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.