Introduction to Programming (Java) & #183; 4.1 Meaning of data abstraction

Source: Internet
Author: User
Tags arithmetic 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 of the abstract features of data types from the detailed details of what is actually present.

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.

the next chapter goes on to introduce data abstraction-as a linear table of abstract data types and in fact present.

4.1 Meaning of the data abstraction

The values stored in memory are called data or operands, and the data is categorized for the convenience of the program apes to recognize 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 this set of values. The type of Java language contains the basic type and the program ape itself defines the data type (reference type).

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 interface/abstract characteristics of the base type contain the range of values for that type and the applicable operators . Here is an example of the most frequently used int and Boolean, explaining the meaning of the data abstraction.

1. Interfaces and implementations of type int
"Here are some considerations for using int" integer type int is the simulation of a mathematical integer . The difference between an int and a mathematical integer is that int is not infinite . The int type logically has 32-bit memory space, in accordance with [0.1.22 complement]. The maximum value (complement) of type int is (231-1) 2147483647 or 0X7FFF_FFFF, or 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 executing on the clock face of the clocks. A condition that exceeds the maximum or minimum value when calculating. Called overflow (O Verflow and underflow) when large numbers are used. Be careful to overflow. Since there is no human or unusual mechanism to help the ape prevent such a program from happening, it is a silent killer.

Available for example, the following code verifies:
void Dosth () {
int x=0x7fffffff;
SYSTEM.OUT.PRINTLN (x + 1);
}
Also, 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 unary operation, + + 、--, 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 32 bits of memory space. means that the user knows the range of the int .

In fact. The range of ranges of int is specified in the JVM specification, but no memory space of type int is defined . 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 Java program apes are not available 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 on the host platform is the size standard of the word.

Assuming that the machine and system platform are 32-bit, both int and all references (reference) are assigned 32-bit memory, assuming that the machine and system platform are 64-bit, 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.

No matter what a non-0 integer value x, the value of the expression x!=0 is true; no matter what a non-null reference ref, the value of the expression Ref!=null is true.

When people grin to discuss the Boolean expressions and Boolean types of Java, the Boolean interface is actually discussed. However, the Boolean type is so important and basic in the Java language. Does not exist in substance. Well, the joke is a little big. The 2nd edition of the JVM specification has a section <3.3.4there is No boolean type>. In fact, the JVM defines a Boolean type, but does not provide an operation Boolean instruction.

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.

Boolean[] Type of source code. Access to its elements and changes to the baload and Bastore directives that use the JVM's array of byte types.

Simply put, the Boolean type is implemented. The JVM defines a data type such as Boolean. such as "Z" or "[Z", respectively, for Boolean and boolean[]. The creation of boolean[] is also supported directly through the NewArray directive. However, the Boolean operation is converted to an int operation. 1 means true, 0 for false. and boolean[] is treated as a byte array operation. The Sun's JVM implements the boolean[] element as the value of 8bit, and other JVM implementations may take the form of compression. such as a person.

Two examples of int and Boolean. The difference between the abstract characteristics of the data type and the detailed details of the actual present is reflected. This 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, there is no need to involve the JVM as a basic type. There are no objects in the source code. There are only reference variables and reference values . Reference to the implementation of the object [object on the 7.4.3 heap]. The interface of the class is detailed in the present [6th Chapter Package], which is described only briefly here.

1. API for Classes

Parnas principle/interface and implementation separation refers to the module or method level, the user program Ape consciously ignore the implementation of the method. 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 interviewed. 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 that an external object can access, as defined by the class. Usually. The public, protected domains declared in the 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 the [6th Chapter package].

The interface of a class is often called the API for that class. This is to be different from the interface types 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], which takes a class as a class type.

The difference between subclasses (subclass) and subtypes (subtype) is described in [2.1.1 richter substitution principle]. So what is the difference between class and type (type)?

The difference table today separates interfaces from implementations. Type is a name that identifies the interface of the class.

Suppose an object can accept all operation requests (method calls) for an interface of the X class, which is said to have an X type .

It is because the subclass of Java can satisfy the interface of the parent class (although it can be overwritten), so 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 defines not only a type, but also an implementation of an object's internal state and method, as well as methods that are not interfaces (such as private methods). Simply. 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: Objects of a different kind can have the same type, and 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).




Introduction to Programming (Java) & #183; 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.