Introduction to Programming (Java) • 4.1 Meaning of data abstraction

Source: Internet
Author: User
Tags java se

You has no choice about the necessity to integrateyour observations,

Your experiences, your knowledge into abstractideas, i.e., into principles.

--ayn Rand, "philosophy:who Needs It" 1974

Data Abstraction is a clear separation between the abstract characteristics of a data type and the specifics of its implementation. The "abstract feature" of the data type refers to the interface that is visible to the user code and how the data type is used. Data abstraction is the generalization of interface and realization separation principle in the type level and object technology .

This chapter introduces the Java basic data types, abstract classes, Java interfaces as data abstractions, and focuses on the role of abstract classes and interfaces. Next ChapterContinue to introduce data abstraction-as a linear table of abstract data types and its implementation. 4.1 Meaning of the data abstraction

The values stored in memory are called data or operands, and the data is categorized to make it easier for programmers to identify and manipulate them. In [2.2.2 Java data Type], a number of concepts are introduced: data types --defining a collection of values and a set of operations that handle the set of values; the types of Java languages include basic types and programmer-defined data types (reference types).

In the [3.2.2 Operator], the various operations of the basic types are described, and the 7 operations of reference types are enumerated in the meaning of [2.4.1 references]. It is important to note that all of the above descriptions are at the Java language level [rather than at the JVM level]. Or, what has been discussed is the abstract feature of the Java data Type-interface.


4.1.1 Basic types of implementations

The

Basic types of interface/abstraction features include the range of values and applicable operators for that type. Here is an example of the most commonly used int and Boolean to illustrate the meaning of data abstraction.

1. Interfaces and implementations of type int
    "Here are some considerations for int use" integer type int is the simulation of mathematical integers , the difference between an int and a mathematical integer is int is not infinite . The int type logically has 32-bit memory space, according to the [0.1.22 complement],int type's maximum (complement) is (231-1) that is 2147483647 or 0X7FFF_FFFF, or the binary 0b01111111_11111111_ 11111111_11111111. When it adds 1, it becomes the 0x8000_0000, which is the minimum value ( -231-2147483648). The calculation of an integral type is usually described as the on the face. When the calculation exceeds the maximum or minimum value, called overflow (O Verflow and underflow), when using large numbers, be careful to overflow, because there is no one or abnormal mechanism to help programmers to prevent such a program error, it is a silent killer. The following code can be used to verify:
    void Dosth () {
        int x=0x7fffffff;
        System.out.println (x + 1);
   }
In addition, what values in int can i = = I? In addition to 0, there is a minimum value of 0x8000_0000. So the semantics of if (i = = i) are different from if (i = = 0). The
operation for INT includes most operators, such as arithmetic, comparison, bitwise manipulation, assignment and sign of unary operations, + + 、--, and so on.


The above content is an int interface/abstract feature. As a user of int, it is said that the int type (logically) has a 32-bit memory space, which means that the user knows the value range of int . In fact, the range of ranges of int is specified in the JVM specification, but there is no memory space for the int type . The memory space used to store int data is freely set by the different implementations of the JVM.

In general, Word (word) is used as the basic unit of measure for data values in the JVM. The required word size is large enough to hold byte, short, int, char, float, returnvalue (a JVM data type that is not available to Java programmers for implementing the finally clause in a Java program) and reference. The two characters are sufficient to fit a long or double. In general, the size of the local pointer of the host platform is the size standard of the word. If the machine and the system platform are 32-bit, the int value and all references (reference) are assigned 32 bits of memory, and if the machine and the system platform are 64 bits, theint value may be assigned 64-bit space .

Usually the user of int does not care about the implementation of int, the memory space of int whether it is 32-bit or 64-bit, must not affect int's interface. Even if the system uses 64 bits to hold the int value, the 2147483647+1 will (and should) behave as an overflow (which becomes the minimum value).

Exercise 4-1: Explain the separation of interfaces and implementations of type int.

Exercise 4-2: Programming typesystem.primitive. Intdemo, verifying overflow and i = = I.

2. Boolean-type interface and implementation

What the computer does is just arithmetic operations and logical operations. Thus, Boolean expressions are widely used in if-else statements,?: Operations, and loop statements. Relational operators such as 5>2, X!=y, and so on are worth a Boolean value, Boolean operators such as P && Q for logical operations. Any one non-0 integer value x, the value of the expression x!=0 is true, and any non-null reference ref, the value of the expression Ref!=null is true.

When people talk about Boolean expressions and Boolean types in Java, the Boolean interface is actually discussed. However , the Boolean type that is so important and basic in the Java language does not exist substantively. Well, the joke is a bit big, there is a section <3.3.4there is no Boolean type> in the JVM Specification version 2nd; The JVM actually defines the Boolean type, but does not provide an instruction to manipulate the Boolean. The title of the Java Virtual Machine specification (Java SE 7 Edition) is <2.3.4 Boolean type > more rigorous.

In the JVM, the boolean operation of the source code is implemented as an int type operation of the JVM. The source code of the boolean[] type, whose elements are accessed and modified using the JVM's byte-type array of baload and Bastore directives. Simply put, the implementation of the Boolean type, the JVM defines a Boolean data type, such as "Z" or "[Z" respectively, the Boolean and boolean[], also through the NewArray directive directly supports the creation of boolean[]. However, for a Boolean operation that converts to an int operation, 1 for true, 0 for false, and boolean[] as a byte array operation, the Sun JVM implements the boolean[] element as the value of 8bit, and the other JVM implementations may be in compressed form, such as a person.

The int and Boolean two examples reflect the difference between the abstract characteristics of a data type and the specifics of its implementation, which is the meaning of the data abstraction that cleanly separates the data interface from the implementation.

Exercise 4-3: Explain the differences in the Boolean type at the Java language level, the JVM specification level, and the JVM implementation level, and thus explain the value of abstraction.

Interfaces for the 4.1.2 class

For reference types, it is not necessary to involve the JVM as a basic type. There are no objects in the source code, only reference variables and reference values , and the implementation of the object references [objects on the 7.4.3 heap]. The interface and implementation of the class are detailed in the [6th Chapter package], which is outlined here only.

1. API for Classes

Parnas principle/interface and implementation separation means that the user programmer consciously ignores the implementation of the method at the module or method level. And each method has its own interface. For a user of Class A, the (method) interface defined by A is only worth paying attention to when it can be accessed. Many methods--typically private methods--are used only by a, and it is unclear to the outside world whether these methods exist or not. Therefore , in object-oriented technology, the PARNAS principle is extended to the class level.

the interface of a class is a collection of interfaces defined by a class that can be accessed by an external object. Typically, public, protected domains declared in a class are also part of the class interface.

From the above definition, as the client program and the relationship between the class, such as in or not in a package, or is not a subclass of this class , the interface of the class of this collection of the client class will be a different degree. Use the access modifier to qualify the interface of the class, which is called encapsulation and is described in detail in the [6th Chapter package].

The interface of a class is often referred to as the API for that class, which is to be distinguished from the interface type in Java. The private and Package-private methods are generally not interfaces of classes.

2. Classes and Types

In most cases, you can view class as a type. For example, the concept of a (data) type given in [2.2.2Java data type] takes a class as a class type.

The difference between a subclass (subclass) and a subtype (subtype) is described in [2.1.1 Richter substitution principle], so what is the difference between class and type (type)?

The difference is manifested in the separation of interface and implementation. type is a name that identifies the interface of the class. If an object is capable of accepting all operation requests (method calls) of an interface of the X class, the object is said to have an X type . It is because the subclasses of Java can satisfy the interface of the parent class (although it can be overwritten), so that the object of the subclass can have multiple types in the class hierarchy at the same time .

Class is the complex of interfaces and implementations. A class does not only define a type, it also defines the internal state of the object and the implementation of the method, as well as methods that are not interfaces (such as private methods). In short, a type is an interface to a class, and a class is a type + implementation.

Exercise 4-4: An object can have more than one type. These types form a ____, like a biological classification.

Exercise 4-5: Non-homogeneous objects can have the same type, this common type is called ____.

Exercise 4-6: Subclasses inherit the interface of the parent class. Please discuss this proposition.

Exercise 4-7: Explain the differences between classes (class) and types (type).




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to Programming (Java) • 4.1 Meaning of data abstraction

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.