Object-Oriented Programming: Java complex data type usage
Source: Internet
Author: User
Object-Oriented Programming: Java complex data type usage-general Linux technology-Linux programming and kernel information. The following is a detailed description. In this article, we will discuss complex 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:
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
}}
Array
The Java array (array) is a dynamically created index object, which is very similar to the class. In addition, an array can only index an array instance or null, as shown in the following code:
Int [] myIntArray = new int [5]; int [] anotherIntArray = null;
Arrays are the inheritance of Object classes. In this way, all methods of Object classes can be called by arrays. An array object is composed of elements. The number of elements can also be 0. In this case, the array is empty. All arrays are numbered from 0, which means that the index number of the first element in the array is 0. All accesses to array elements will be checked at runtime. If you try to index an element whose number is less than 0 or greater than the length of the array, an ArrayIndexOutOfBoundsException exception will occur and thrown out.
The array elements are indexed by integer values, as shown in the following code:
Int [] myIntArray = {9, 5, 6}; int int1 = myIntArray [0]; int int2 = myIntArray [1]; int int3 = myIntArray [2];
The length of the array object remains unchanged. To change the length of an array variable, you must create another array and assign it to the variable, as shown in the following code:
Int [] myIntArray = {9, 5, 6}; System. out. println ("myIntArray length =" + myIntArray. length); // The output result is 3 myIntArray = new int [] {3, 6, 4, 2, 8}; System. out. println ("myIntArray length =" + myIntArray. length); // The output result is 5.
Composite data type
The Java language does not support structure (struct) or union (union) data types. Its Composite data types are constructed through classes or interfaces. classes provide data bundling and methods, while limiting access to private data of classes. For example, if the C language is used, the car can be defined using the structure as shown in the following code:
Struct Car
{
Char *
Model;
Char *
Make;
Int
Year;
Engine *
Engine;
Body *
Body;
Tire **
Tires;
}
The preceding DEMO code also implicitly uses the previously defined Engine, Body, and Tire structures. In Java, the automobile definition is shown in list.
The preceding example also assumes that we have previously defined the CarModels and CarMakes interfaces as well as the Engine, Body, Tire, DurangoEngine, DurangoBody, and GoodyearTire classes.
Composite data type Initialization
The composite data type is initialized by the code defined in the constructor. It is called when a class creation function (such as "new") is applied to a class variable. Before the constructor is called, each member of the class is initialized to its own default value or explicitly assigned initial value. The program code in listing B explains the above process.
In the code of listing B, when an instance of myClass is created using the new operator, the constructor public MyClass () is called to initialize the class itself. The initialization process is as follows:
Declare "int myInt;" and assign the default value 0 to myInt.
Declare "AnotherClass anotherClass;" and assign the default value null to anotherClass.
The Declaration "myint = 2;" in the constructor assigns a value of 2 to myInt.
Predefined Composite data types
The Java language contains a large number of predefined Composite data types. One of the String classes belongs to the java. lang package. The methods provided by the String class perform common String operations, such as length (), substring (int beginIndex), toUpperCase (), toLowerCase (), and equals. Another common Java Composite data type is the Vector class, which belongs to the java. util package. The method defined by the Vector class to perform common operations on the Extensible object array. For example, add (int index, Object element), elementAt (int index), isEmpty (), and remove (int index. These are representative examples of a small portion of predefined Composite data types. In future articles, we will further discuss pre-defined Composite data types.
Summary
In addition to simple data types, the Java language also defines three index data types discussed in this article. Java also allows developers to combine simple and indexed data types to create Composite data types. Together with Java predefined Composite data types, user-defined Composite data types provide a powerful object-oriented toolkit for programmers. In the following article, we will further discuss in detail most common complex data types and their available operations. We will start with the Java-specific collection framework.
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