Object-Oriented Programming: Java complex data type usage

Source: Internet
Author: User

 

In this article, we will discussComplex data types in Java. The content involves the initialization process, default initial values, and some operations on complex data types.

Index

Similar to the definition of a simple data type, the Java Virtual Machine (JVM) also defines the index data type. The index type can be a reference variable. Because Java does not explicitly define the pointer type, the index type can be considered as a pointer to the actual value or to the actual value represented by the variable. An object can be referred to by more than one index ". The JVM never directly addresses the object but operates on the index of the object.

There are three types of indexes: class, interface, and array ). The index type can reference dynamically created class instances, common instances, and arrays. An index can also contain special values. This is a null index. The null index does not have a corresponding type at runtime, but it can be converted to any type. The default value of the index type is null.

Class

Class refers to the data type that defines methods and data. Internally, JVM usually implements class objects as a set of pointers pointing to methods and data. Variables defining the class type can only reference the class instance or null, as shown in the following code:

 
   
MyObjectAnObject = new MyObject ();// Valid

MyObjectAnotherObject = null;// Valid

MyObjectStillAnotherObject = 0;// Invalid


Interface

An interface is like a template. This template defines the methods that an object must implement, so that these methods can be referenced as interface instances. The interface cannot be instantiated. Class can implement multiple interfaces and are indexed through these interfaces. Interface variables can only be indexed to instances of classes that implement this interface. For example, if we define an interface named Comparable and a SortItem class which implements the interface Comparable, we can write the following definition code:
Comparable c = new SortItem ();

If the Comparable interface defines a method: public void compare (Comparable item), The SortItem class must provide the compare method implementation, as shown in the following code:

 
   
Public class SortItem implements Comparable

{

Public void compare (Comparable item)

{

... Method implementation here

}

< p><>

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.